-
Notifications
You must be signed in to change notification settings - Fork 1
/
crunchyroll_ratings.py
executable file
·241 lines (180 loc) · 7.54 KB
/
crunchyroll_ratings.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
import requests
import HTMLParser
import re
class AnimeInfo(object):
def __init__(self, title, description):
self.title = title
self.description = description
self.averageRating = 0
self.numFiveStarRating = 0
self.numFourStarRating = 0
self.numThreeStarRating = 0
self.numTwoStarRating = 0
self.numOneStarRating = 0
def __str__(self):
string = "Title: " + self.title
string = string + "\nDescription: " + self.description
string = string + "\n Average Rating: " + str(self.averageRating)
string = string + "\n Number of Five Star Rating: " + \
str(self.numFiveStarRating)
string = string + "\n Number of Four Star Rating: " + \
str(self.numFourStarRating)
string = string + "\n Number of Three Star Rating: " + \
str(self.numThreeStarRating)
string = string + "\n Number of Two Star Rating: " + \
str(self.numTwoStarRating)
string = string + "\n Number of One Star Rating: " + \
str(self.numOneStarRating)
return string
def getCsv(self):
# csv
return ",".join([self.title,
str(self.averageRating),
str(self.numFiveStarRating),
str(self.numFourStarRating),
str(self.numThreeStarRating),
str(self.numTwoStarRating),
str(self.numOneStarRating)]) + "\n"
class CrunchyrollRatingHtmlParser(HTMLParser.HTMLParser):
def __init__(self):
# needs old style super
HTMLParser.HTMLParser.__init__(self)
self.inH3Tag = False
self.inRatingsSection = False
self.ratingCountRegexp = re.compile("\((?P<count>\d+)\)")
self.inRatingLi = False
self.averageRating = 0
self.ratings = []
def handle_starttag(self, tag, attrs):
if tag == "h3":
self.inH3Tag = True
if self.inRatingsSection:
if tag == "span":
for attr in attrs:
if attr[0] == "content":
self.averageRating = float(attr[1])
if tag == "li":
if (len(attrs) > 0 and len(attrs[0]) > 0 and
attrs[0][0] == "class" and
(
attrs[0][1] == "5-star cf" or
attrs[0][1] == "4-star cf" or
attrs[0][1] == "3-star cf" or
attrs[0][1] == "2-star cf" or
attrs[0][1] == "1-star cf"
)):
self.inRatingLi = True
def handle_endtag(self, tag):
if tag == "h3":
self.inH3Tag = False
# assumes that ratings section is followed by a
# javascript tag...crappy logic =[
if self.inRatingsSection and tag == "script":
self.inRatingsSection = False
if tag == "li" and self.inRatingLi:
self.inRatingLi = False
print "exiting rating li"
def __getCount(self, data):
m = re.search(self.ratingCountRegexp, data)
# return tuple of (foundData, count)
if m is not None:
return (True, m.group("count"))
else:
return (False, 0)
def handle_data(self, data):
if self.inH3Tag and data == "User Ratings":
self.inRatingsSection = True
if self.inRatingsSection:
if self.inRatingLi:
countTuple = self.__getCount(data)
if countTuple[0]:
self.ratings.append(int(countTuple[1]))
class CrunchyrollratingHtmlParser(HTMLParser.HTMLParser):
def __init__(self):
# needs old style super
HTMLParser.HTMLParser.__init__(self)
self.__inScript = False
self.allAnimeTitlesAndDescriptionString = ""
def handle_starttag(self, tag, attrs):
if tag == "script":
self.__inScript = True
def handle_endtag(self, tag):
if tag == "script":
self.__inScript = False
def handle_data(self, data):
if self.__inScript:
# Expect long data only for
# the anime title & description
# javascript block
if len(data) > 100000:
self.allAnimeTitlesAndDescriptionString = data
def createBasicAnimeInfoList(animeTitleAndDescStr):
titleAndDescList = [ x.strip()
for x in animeTitleAndDescStr.split("\n") ]
basicAnimeInfoList = []
for titleAndDesc in titleAndDescList:
if titleAndDesc != "":
m = re.search("\"name\":\"(?P<title>.+)\", \
\"description\":\"(?P<description>.+)\",",
titleAndDesc,
re.VERBOSE)
if m is not None:
# TODO: The description could be sanitized, but I'll just cope.
basicAnimeInfoList.append(AnimeInfo(m.group("title"),
m.group("description")))
return basicAnimeInfoList
def getAllBasicAnimeInfo():
animeUrl = "http://www.crunchyroll.com/videos/anime/alpha?group=all"
response = requests.get(animeUrl)
encoding = response.encoding
allAnimeHtml = response.text
parser = CrunchyrollratingHtmlParser()
parser.feed(allAnimeHtml)
return createBasicAnimeInfoList(
parser.allAnimeTitlesAndDescriptionString)
def updateInfoWithRatings(animeInfo):
print "Getting ratings for", animeInfo.title
cleanedTitle = animeInfo.title
# properly encode the URL
cleanedTitle = cleanedTitle.decode('unicode-escape').encode('utf-8')
cleanedTitle = re.sub("[^A-Za-z0-9\s-]", "", cleanedTitle)
cleanedTitle = cleanedTitle.replace(" ", "-")
# replace multiple dashes with a single dash
cleanedTitle = re.sub("[-]+", "-", cleanedTitle)
titleUrl = "http://www.crunchyroll.com/" + cleanedTitle
response = requests.get(titleUrl)
if response.status_code != 200:
print "\n\n======================================================\n\n"
print "Could not get html for ", titleUrl
print "Original title was: ", animeInfo.title
print "Cleaned title was: ", cleanedTitle
print "\n\n======================================================\n\n"
return
encoding = response.encoding
ratingHtml = response.text
parser = CrunchyrollRatingHtmlParser()
parser.feed(ratingHtml)
animeInfo.averageRating = parser.averageRating
if (len(parser.ratings) > 0):
orderedRatings = list(reversed(parser.ratings))
animeInfo.numOneStarRating = orderedRatings[0]
animeInfo.numTwoStarRating = orderedRatings[1]
animeInfo.numThreeStarRating = orderedRatings[2]
animeInfo.numFourStarRating = orderedRatings[3]
animeInfo.numFiveStarRating = orderedRatings[4]
else:
print "No rating info for", animeInfo.title
return
def main():
# Get all anime basic info
allAnimeInfo = getAllBasicAnimeInfo()
# Get ratings for each anime
for animeInfo in allAnimeInfo:
updateInfoWithRatings(animeInfo)
with open("ratings.csv", "a") as f:
f.write(animeInfo.getCsv())
# terrible code that
f.close()
if __name__ == "__main__":
main()