-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_downloaded.py
More file actions
76 lines (61 loc) · 2.21 KB
/
Copy pathprocess_downloaded.py
File metadata and controls
76 lines (61 loc) · 2.21 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
""" Processes the already downloaded files (see download.py) and saves
results to reports/all_posts.csv and ./all_posts.json
Raises:
Exception: _description_
Returns:
_type_: _description_
"""
from json import dump, load
import html2text
from download import POSTS_FILE, CATEGORIES_FILE
CSV_OUTPUT = "reports/all_posts.csv"
JSON_OUTPUT = "all_posts.json"
def get_post_categories(index_list, categories_dict):
cat_strings = [categories_dict.get(str(index), "Python Practice") for index in index_list]
return cat_strings
def get_word_count(post):
html = post["content"]["rendered"]
text = html2text.html2text(html)
HEURISTIC_ADJUSTMENT_BASED_ON_WP_COUNT = .965
return int(len(text.split()) * HEURISTIC_ADJUSTMENT_BASED_ON_WP_COUNT)
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_categories():
with open(CATEGORIES_FILE, "r") as f:
return load(f)
def get_posts():
with open(POSTS_FILE, "r") as f:
return load(f)
def get_massaged_posts():
categories_dict = get_categories()
posts = get_posts()
return massage_downloaded_posts(posts, categories_dict)
def save_posts_json(posts):
with open(JSON_OUTPUT, mode='wt') as f:
dump(posts, f)
def save_csv(csv):
with open(CSV_OUTPUT, "wt") as f:
f.write(csv)
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
if __name__ == "__main__":
posts = get_massaged_posts()
save_posts_json(posts)
csv = get_csv(posts)
save_csv(csv)