forked from timkeller/bettercine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
147 lines (111 loc) · 4.53 KB
/
fetch.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
from bs4 import BeautifulSoup
from datetime import datetime
import requests
import re
import os
base_url = "http://www.sterkinekor.mobi"
class Movie:
movie_id = ""
title = ""
genres = []
age = []
comingsoon = ""
image = ""
def __init__(self, movie_id, title):
self.movie_id = movie_id
self.title = title.lstrip('(3D)').strip()
def getHtml(url):
r = requests.get(base_url + url, headers={'User-agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.25 (KHTML, like Gecko) Chrome/12.0.706.0 Safari/534.25'})
return r
def getIMDbRating(title):
r = requests.get("http://www.imdb.com/xml/find?xml=1&nr=1&q=%s" % title).text.strip()
imdb_id = BeautifulSoup(r).find("resultset").imdbentity['id']
r = requests.get("http://www.imdb.com/title/%s/" % imdb_id).text.strip()
rating = BeautifulSoup(r).find("div", class_="star-box-giga-star").text.strip()
return rating
def getRegions():
pass
def getCinemas(region_id):
pass
def getMovieList(region_id=4, cinema_id=164, verbose=False):
if (verbose):
print "Getting list for Region %s, Cinema %s" % (region_id, cinema_id)
movies = []
start_page = 1
end_page = 1
region = region_id
cinema = cinema_id
thecount = 0
page = start_page
r = getHtml("/movies/page:%s/region:%s|%s" % (page, region, cinema))
html = r.text
soup = BeautifulSoup(html).find('div', class_="page-counter")
end_page = int(soup.string.split()[-1])
for page in xrange(start_page, end_page+1):
r = getHtml("/movies/page:%s/region:%s|%s" % (page, region, cinema))
html = r.text
soup = BeautifulSoup(html).find_all('tr', class_="list-item")
for items in soup:
thecount += 1
movie = items.find_all("td")
image = movie[0].img['src']
movie_id = re.findall(r"^/movie/(.*)/(?:.)", movie[1].a["href"], re.I)[0]
title = movie[1].a.find('span', class_="movie-title").string
genres = [genre.strip() for genre in movie[1].find_all('span')[1].string.split(",")]
age = [a.strip() for a in movie[1].find_all('span')[2].string.split(" ")]
comingsoon = ""
if len(movie[1].find_all('span')) >= 4:
comingsoon = movie[1].find_all('span')[3].string
m = Movie(movie_id, title)
m.image = image
m.genres = genres
m.age = age
m.comingsoon = comingsoon
if (verbose):
print "%s of %s: %s" % (thecount, len(soup), title)
m.imdb = getIMDbRating(m.title)
m.showtimes = getShowtimes(movie_id, cinema)
movies.append(m)
return movies
def getShowtimes(movie_id, cinema_id):
showtimes = []
search_url = "/book/select/performance/%s/%s" % (movie_id, cinema_id)
html = getHtml(search_url).text
soup = BeautifulSoup(html).find("div", class_="performance-selection").find_all("p")
for day in soup:
dayshowtimes = []
date = day.strong.string
times = day.find_all("a")
for showtime in times:
date_object = datetime.strptime("%s %s %s" % (date, str(datetime.now().year), showtime.string), '%a %d %b %Y %H:%M')
dayshowtimes.append((date_object,showtime['href'].split("?")[0]))
showtimes.append({'day':date, 'showtimes':dayshowtimes})
return showtimes
def main():
region_id = 4
cinema_id = 164
movies = getMovieList(region_id, cinema_id, False)
fdir = os.path.dirname(os.path.realpath(__file__))
stream = file(fdir+'/movies.html', 'w')
stream.write("<html><body>")
stream.write("<h1>Now Showing at %s %s</h1>" % (region_id, cinema_id))
stream.write("<ul>")
for m in movies:
stream.write("<li><a href='#%s'>%s</a></li>" % (m.title, m.title))
stream.write("</ul><hr>")
for movie in movies:
stream.write( "<h1><a name='%s'></a>%s</h1>" % (movie.title, movie.title))
stream.write( "<h2>%s/10</h2>" % (movie.imdb))
stream.write( "<ul>")
for theday in movie.showtimes:
stream.write("<li>%s<ul>" % (theday['day']))
for showtime in theday['showtimes']:
stream.write( "<li><a href='%s'>%s</a></li>" % ((base_url + showtime[1]), showtime[0].strftime("%A, %d %B %I:%M%p")))
stream.write("</ul></li>")
stream.write( "</ul>" )
stream.write( "<hr>" )
stream.write("<small>Rendered by BetterCine</small>")
stream.write("</body></html>")
stream.close()
if __name__ == "__main__":
main()