-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_posts.py
More file actions
92 lines (68 loc) · 2.42 KB
/
Copy pathget_posts.py
File metadata and controls
92 lines (68 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
from json import dump
from pprint import pp
import html2text
BLOG_URL = "https://codesolid.com"
def posts_url(url, page):
"""Returns the URL and params for a single page of posts"""
url = f"{url}/wp-json/wp/v2/posts?orderby=date&order=desc"
params = {"page": page}
return url, params
def get_blog_categories(url):
"""Get the current blog categories and convert it to an id-based dictionary"""
url = f"{url}/wp-json/wp/v2/categories"
r = requests.get(url)
if r.status_code != 200:
raise Exception("Unable to get post categories")
json = r.json()
return {item["id"]: item["name"] for item in json}
def get_posts(url):
all_posts = []
page = 1
while True:
url, params = posts_url(url, page)
page = page + 1
r = requests.get(url, params=params)
if r.status_code != 200:
break
json = r.json()
all_posts = all_posts + json
return all_posts
def get_post_categories(index_list, categories_dict):
cat_strings = [categories_dict[index] for index in index_list]
return cat_strings
# with open('all_posts_ordered.json', mode='wt') as f:
# dump(posts, f)
def get_word_count(post):
html = post["content"]["rendered"]
text = html2text.html2text(html)
return len(text.split())
def massage_downloaded_posts(posts, categories):
massaged = []
for post in posts:
post_categories_as_list = get_post_categories(post["categories"], categories)
post_categories = ";".join(post_categories_as_list)
this_post = {
"url": post["link"],
"date": post["date"],
"categories": post_categories,
"title": post["title"]["rendered"],
"word_count": get_word_count(post)
}
massaged.append(this_post)
return massaged
def get_massaged_posts():
categories_dict = get_blog_categories(BLOG_URL)
posts = get_posts(BLOG_URL)
return massage_downloaded_posts(posts, categories_dict)
def make_csv(l):
l.join
def get_csv(massaged_posts):
csv = "title,url,date,categories,word_count\n"
for post in massaged_posts:
# tokens = [post["title"], {post["url"], {post["date"]},
csv += f'\"{post["title"]}\",\"{post["url"]}\",\"{post["date"]}\",\"{post["categories"]}\",\"{post["word_count"]}\"\n'
return csv
posts = get_massaged_posts()
csv = get_csv(posts)
print(csv)