-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog-info.py
executable file
·48 lines (41 loc) · 1.28 KB
/
blog-info.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
#!/usr/bin/env python3
#
# blog-posts.py
# Counts the posts in my jekyll blog.
#
# Brandon Amos
# 2013.08.10
import operator
import sys
import urllib.request
from bs4 import BeautifulSoup
import re
import html.parser; h = html.parser.HTMLParser()
def getContent(url):
response = urllib.request.urlopen(url)
html = response.read().decode("utf8")
if not html: sys.exit(42)
return h.unescape(html)
def numPostsFromArchives(archiveContent):
soup = BeautifulSoup(archiveContent)
mainCol = soup.find('div', attrs={'id': 'col1'})
return len(mainCol.find_all('li'))
def parseTags(tagContent):
soup = BeautifulSoup(tagContent)
mainCol = soup.find('div', attrs={'id': 'col1'})
headers = mainCol.find_all('h3')
tags = {}
for header in headers:
tp = re.match("(\S*) - (\d*) Posts?", header.text)
tags[tp.group(1)] = int(tp.group(2))
sortedTags = sorted(tags.items(), key=operator.itemgetter(1), reverse=True)
return [x[0] for x in sortedTags]
if __name__=='__main__':
archiveContent = getContent('http://bamos.github.io/archives/')
tagContent = getContent('http://bamos.github.io/tags/')
print(
"{} posts across the following tags, listed by highest frequency.".format(
numPostsFromArchives(archiveContent)
)
)
print(", ".join(parseTags(tagContent)))