-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme_generator.py
65 lines (53 loc) · 2.93 KB
/
readme_generator.py
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Create README.md for each city on GitHub
import os
import glob
import json
class GenREADME(object):
"""Generate README.md for each city"""
def __init__(self, username, continent, city):
self.root = 'https://github.com/{}/your-next-city/blob/master'.format(username)
self.continent = continent
self.city = city
self.u_city = city.replace(' ', '_')
def create_template(self, file):
self.template = file
### Start writing README.md
self.template.write('# {}\n\n'.format(self.city))
self.template.write('#![{}]({}/{}/{}/{}.png)\n\n'.format(self.u_city, self.root, self.continent, self.u_city, self.u_city))
self.template.write('Visualize top 20 related news categories from 2007 to 2016\n\n')
self.template.write('# Data\n\n')
self.template.write('The [data]({}/{}/{}/data) folder contains 10 JSON files that I scraped from [The Guardian](https://www.theguardian.com/):\n\n'.format(self.root, self.continent, self.city))
self.template.write('| File Name | Num of Documents | Num of sentences that mentioned `{}` |\n'.format(self.city))
self.template.write('| ------------- |:-------------:|:-----:|\n')
self.template.write('{}\n\n'.format(self._count_docs_sents()))
self.template.write('Trends related to {} from 2007 to 2016\n\n'.format(self.city))
self.template.write('![2007-2016_{}_Top_20_News_Categories]'.format(self.city))
self.template.write('({}/{}/{}/fig/2007-2016_{}_Top_20_News_Categories.png)\n\n'.format(self.root, self.continent, self.city, self.city))
for year in range(2007, 2017):
self.template.write('![{}_{}_Top_20_News_Categories]'.format(year, self.city))
self.template.write('({}/{}/{}/fig/{}_{}_Top_20_News_Categories.png)\n\n'.format(self.root, self.continent, self.city, year, self.city))
def _count_docs_sents(self):
lines = ''
total_docs = 0
total_sents = 0
paths = glob.glob("{}/{}/data/*.json".format(self.continent, self.city))
for path in paths:
with open(path, encoding='utf-8') as f:
news_one_year = json.load(f)
total_docs += news_one_year['docs']
total_sents += news_one_year['sentences']
lines += '| `{}` | {} | {} |\n'.format(os.path.basename(path), news_one_year['docs'], news_one_year['sentences'])
lines += '| `ALL` | {} | {} |'.format(total_docs, total_sents)
return lines
if __name__ == '__main__':
username = 'letitbevi'
continent = 'Asia'
with open('{}/cities.txt'.format(continent)) as f:
cities = f.read().splitlines()
for city in cities:
u_city = city.replace(' ', '_')
fopen = open('{}/{}/README.md'.format(continent, u_city), 'w')
readme = GenREADME(username, continent, city)
readme.create_template(fopen)