-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
183 lines (152 loc) · 5.53 KB
/
Copy pathplot.py
File metadata and controls
183 lines (152 loc) · 5.53 KB
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
from gevent import monkey
monkey.patch_all()
from flask import Flask, jsonify, request
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from flask_cors import CORS
import urllib
import re
import gevent
import time
from utils import abbrev_to_integer
app = Flask(__name__)
CORS(app)
@app.route('/search')
def get_scraped_data():
searchword = request.args.get('serie', '')
searchurl = urllib.parse.quote(searchword)
page = urlopen(Request(
url=f'https://www.imdb.com/find/?q={searchurl}&ref_=nv_sr_sm',
headers={'User-Agent': 'Mozilla/5.0'}
))
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
section = soup.find(attrs={"data-testid": "find-results-section-title"})
div_list = section.contents[1]
ul = div_list.contents[0]
items = ul.contents
results = []
k=1
for item in items:
print(f'---------- {k} ----------')
title = item.find('a')
title_string = title.string
href = title['href']
title_id = href.split('/')[2]
print(f'Title: {title_string}')
print(f'Id: {title_id}')
description = []
for li in item.find_all('li'):
print(li.string)
description.append(li.string)
k += 1
results.append({
"title": title_string,
"description": description,
"title_id": title_id
})
print("")
return jsonify(results)
class ScrapingWorker(gevent.Greenlet):
def __init__(self, season, title_id, episode_data):
gevent.Greenlet.__init__(self)
self.season = season
self.title_id = title_id
self.episode_data = episode_data
def _run(self):
print(f'Started season {self.season}')
episodes = scrape_episodes(self.title_id, self.season)
self.episode_data.append(episodes)
def scrape_episodes(title_id, season):
page = urlopen(Request(
url=f'https://www.imdb.com/title/{title_id}/episodes/?season={season}',
headers={'User-Agent': 'Mozilla/5.0'}
))
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
episode_list = soup.find_all(class_="episode-item-wrapper")
# For some reason, imdb is sometimes returning the old site version
old = False
if len(episode_list) == 0:
old = True
if old:
episode_list = soup.find_all(class_="list_item")
episodes = []
episode_number = 1
for episode in episode_list:
if old:
title = episode.find("strong")
else:
title = episode.find("h4")
link = title.find("a")
episode_url = link['href']
episode_name = link.string
if old:
episode_rating = episode.find(class_="ipl-rating-star__rating")
total_votes = episode.find(class_="ipl-rating-star__total-votes")
else:
episode_rating = episode.find(attrs={"data-testid": "ratingGroup--container"})
episode_rating = episode_rating.find(class_="ratingGroup--imdb-rating").get_text().split("/")[0]
total_votes = abbrev_to_integer(episode.find(class_="ipc-rating-star--voteCount").text)
if episode_rating != None and total_votes != None:
if old:
episode_rating = episode_rating.string
total_votes = int(total_votes.string.strip('()').replace(',', ''))
print(f'Episode name: {episode_name}')
print(f'Episode rating: {episode_rating}')
print(f'Season: {season}')
print(f'Episode number: {episode_number}')
print(f'Total votes: {total_votes}')
print("")
episode_obj = {
"name": episode_name,
"rating": float(episode_rating),
"season": season,
"ep_number": episode_number,
"total_votes": total_votes
}
episodes.append(episode_obj)
episode_number += 1
return episodes
@app.route('/list-episodes/<title_id>')
def get_title_episodes(title_id):
# Retrieving how many seasons the title has
page = urlopen(Request(
url=f'https://www.imdb.com/title/{title_id}/?ref_=fn_al_tt_1',
headers={'User-Agent': 'Mozilla/5.0'}
))
html = page.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
seasons = soup.find(id='browse-episodes-season')
if seasons == None:
seasons = soup.find(attrs={"data-testid": "episodes-browse-episodes"})
seasons = seasons.find(string=re.compile("season", re.IGNORECASE))
else:
seasons = seasons['aria-label']
seasons = int(re.search("\d+", seasons).group())
episode_data = []
threads = []
start = time.time()
for season in range(1, seasons+1):
t = ScrapingWorker(season, title_id, episode_data)
threads.append(t)
for t in threads:
t.start()
gevent.joinall(threads)
end = time.time()
print("Tempo decorrido (segundos):")
print(end-start)
episode_data = [season for index, season in enumerate(episode_data) if len(season) > 0]
# Sorting the result by season
def sortFunc(season):
return season[0]['season']
if len(episode_data) > 1:
episode_data.sort(key=sortFunc)
# Flattening the matrix to an array
flat_episode_data_list = []
for sublist in episode_data:
for item in sublist:
flat_episode_data_list.append(item)
return jsonify(flat_episode_data_list)
if __name__ == '__main__':
app.run()