-
Notifications
You must be signed in to change notification settings - Fork 1
/
logparser.py
62 lines (46 loc) · 1.43 KB
/
logparser.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
60
61
62
#!/usr/bin/python
""" Log parser. """
from HTMLParser import HTMLParser
import urllib
class DailyParser(HTMLParser):
"""
HTML parser for the donations log of Wikimedia France
Attributes:
status (int): status variable of the parser.
donations (list data.Donation): list of donations read.
"""
START_PARSER = 0
FOUND_DONATION_TABLE = 1
READ_HOURS = 2
READ_DONATOR = 3
READ_DONATION = 4
END_OF_DONATION_TABLE = 5
def __init__(self):
super(DailyParser, self).__init__()
self.status = DailyParser.START_PARSER
self.donations = []
def handle_starttag(self, tag, attrs):
pass
def handle_endtag(self, tag):
pass
def handle_data(self, data):
pass
class LogParser:
def __init__(self):
self.parser = DailyParser()
@staticmethod
def daypage(day):
"""Returns the page content containing the donations from a specific
day.
Args:
day (datetime.date): day to fetch donation.
Returns:
str: page content with the donation of the day specified as args.
"""
url_args = day.strftime("%Y-%m-%d")
url = "https://dons.wikimedia.fr/journal/%s" % url_args
return urllib.urlopen(url).read()
def fetchday(self, day):
""" Returns donations from a day. """
day_content = self.daypage(day)
self.parser.feed(day_content)