-
Notifications
You must be signed in to change notification settings - Fork 1
/
tester.py
365 lines (298 loc) · 8.92 KB
/
tester.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from bs4 import BeautifulSoup
import opener, configs, re, sys, socket, random, math,time, multiprocessing, threading
from urlparse import urlparse
from base64 import standard_b64decode
import MySQLdb as mdb
from datetime import datetime
from multiprocessing import Pool
socket.setdefaulttimeout(15)
WORKER = 15
THREAD_NUM = 20
TEST = 0
def get_series_id_in_database(name,link,d,con):
cur=con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM `vs_series` WHERE `series_name`=%s",name)
res=cur.fetchone()
if res:
return res['series_id']
else :
sql='''INSERT ignore INTO `vs_series`(`series_name`, `imdb_link`, `series_release_date`) VALUES (%s,%s,%s)'''
args=(name,link,d)
cur.execute(sql,args)
return get_series_id_in_database(name,link,d,con)
def set_season_episode(series_id,episode_link,season,episode,con):
cur=con.cursor()
cur.execute('SELECT * FROM `vs_series_links` WHERE `series_id`=%s AND `link_url`=%s',(series_id,episode_link))
res=cur.fetchone()
if res:
return
else :
sql='''INSERT ignore INTO `vs_series_links`(`series_id`, `season`, `episode`, `link_url`) VALUES (%s,%s,%s,%s)'''
args=(series_id,season,episode,episode_link,)
cur.execute(sql,args)
def i_have_got_series_episode_url(name,series_id,url,season,episode,con):
print '\n\nName: %s\nSeason: %s .Episode: %s' %(name,season,episode)
data=opener.fetch(url)['data']
soup=BeautifulSoup(data)
l=soup.find_all('a')
reg=re.compile(r'.*?url=(.+?)&domain.*')
reg2=re.compile(r'.*external.php.*')
for i in l:
if not i.has_key('href'):
continue
ref=i['href']
parsed=urlparse(ref)
try:
t1=parsed[2]
if not reg2.match(t1):
continue
m=reg.match(parsed[4])
final_url=standard_b64decode(m.group(1))
set_season_episode(series_id,final_url,season,episode,con)
except:
pass
def i_have_got_series_name((url,name,con)):
#~ print 'Series:%s#%s' %(name,url)
data=opener.fetch(url)['data']
soup=BeautifulSoup(data)
released_date=datetime.today()
try:
l=soup.select('.movie_info > table ')
l=(l[0].find_all('tr'))
l=l[1].find_all('td')[1].text
released_date=datetime.strptime(l,'%B %d, %Y')
except:
pass
imdb_link="-1"
try:
imdb_link=soup.select('.mlink_imdb')[0].find_all('a')
imdb_link=imdb_link[0].get('href')
except:
pass
series_id_in_database=get_series_id_in_database(name,imdb_link,released_date,con)
l=soup.find_all('a')
t1=url;
t1=t1.replace('http://www.1channel.ch/watch','tv')
t1='/'+t1+"/season-(\d+)-episode-(\d+).*"
reg=re.compile(t1)
for i in l:
if not i.has_key('href'):
continue
m=reg.match(i.get('href'))
if m:
episode_link="http://www.1channel.ch"+m.group(0)
season=m.group(1)
episode=m.group(2)
i_have_got_series_episode_url(name,series_id_in_database,episode_link,season,episode,con)
def i_have_got_page_number(url):
data=opener.fetch(url)['data']
#~ url_to = '%s.html'%i
#~ f=open(url_to,'w')
#~ f.write(data)
#~ return
soup=BeautifulSoup(data)
l=soup.find_all('a')
reg=re.compile(r'.*/watch-\d+-(.*)')
for i in l:
if not i.has_key('href'):
continue;
if not i.has_key('title'):
continue;
link =i.get('href')
m=reg.match(link)
if m:
series_name=i.get('title')
if 'Watch' in series_name:
series_name=series_name[6:-7].strip()
series_link="http://www.1channel.ch"+m.group(0)
i_have_got_series_name(series_link,series_name)
def get_page_count_and_go_deeper(url):
#~ data=opener.fetch(url)['data']
#~ soup=BeautifulSoup(data)
#~ l=soup.select('.pagination > a ')
#~ ref = l[len(l)-1]['href']
#~ reg=re.compile(r'.*?page=(\d+).*?')
#~ page_count=1
#~ m=reg.match(ref)
#~ if m:
#~ page_count=int(m.group(1))
#~ print page_count
#~
#~
#~ for i in range(1,page_count+1):
#~ new_url=url+"=&page="+str(i)
#~ print new_url
new_url=url
i_have_got_page_number(new_url)
def get_all_series_links(pages):
(all_series, counter)=([], 0)
for url in pages:
counter+=1
if TEST:
if counter==2:
break
data=opener.fetch(url)['data']
#~ url_to = '%s.html'%i
#~ f=open(url_to,'w')
#~ f.write(data)
#~ return
soup=BeautifulSoup(data)
l=soup.find_all('a')
reg=re.compile(r'.*/watch-\d+-(.*)')
for i in l:
if not i.has_key('href'):
continue;
if not i.has_key('title'):
continue;
link =i.get('href')
m=reg.match(link)
if m:
series_name=i.get('title')
if 'Watch' in series_name:
series_name=series_name[6:-7].strip()
series_link="http://www.1channel.ch"+m.group(0)
all_series.append((series_name, series_link))
return all_series
def get_all_series_links_thread(tot):
print 'inside a thread@@@@@@@@@@@@@!!!!!!!!!!!!'
counter=0
for url in tot:
print 'getting from: %s' % url
counter+=1
if TEST:
if counter==2:
break
data=opener.fetch(url)['data']
#~ url_to = '%s.html'%i
#~ f=open(url_to,'w')
#~ f.write(data)
#~ return
soup=BeautifulSoup(data)
l=soup.find_all('a')
reg=re.compile(r'.*/watch-\d+-(.*)')
for i in l:
if not i.has_key('href'):
continue;
if not i.has_key('title'):
continue;
link =i.get('href')
m=reg.match(link)
if m:
series_name=i.get('title')
if 'Watch' in series_name:
series_name=series_name[6:-7].strip()
series_link="http://www.1channel.ch"+m.group(0)
all_series.append((series_name, series_link))
# starting multiprocess
def start_multiprocessing((series_name, series_link)):
con=mdb.connect(configs.HOST,configs.USER,configs.PASS,configs.DB)
print 'Process started: %s' % multiprocessing.current_process()
i_have_got_series_name((series_link,series_name,con))
print 'Process ended: %s' % multiprocessing.current_process()
con.close()
def generate_all_the_main_page_name():
l=[]
l.append('http://www.1channel.ch/?letter=123&tv')
#~ if TEST:
#~ return l
for i in range(ord('a'),ord('z')+1):
l.append('http://www.1channel.ch/?letter='+str(chr(i))+'&tv')
#generating all pages with page number
all_pages = []
for url in l:
page_count=1
data=opener.fetch(url)['data']
soup=BeautifulSoup(data)
l=soup.select('.pagination > a ')
if len(l) != 0:
ref = l[len(l)-1]['href']
reg=re.compile(r'.*?page=(\d+)')
#~ print url, l[len(l)-1]['href']
m=reg.match(ref)
if m:
page_count=int(m.group(1))
#~ print page_count
for i in range(1,page_count+1):
all_pages.append(url+"=&page="+str(i))
return all_pages
def generate_main_pages_thread(url):
print url
page_count=1
data=opener.fetch(url)['data']
soup=BeautifulSoup(data)
l=soup.select('.pagination > a ')
if len(l) != 0:
ref = l[len(l)-1]['href']
reg=re.compile(r'.*?page=(\d+)')
#~ print url, l[len(l)-1]['href']
m=reg.match(ref)
if m:
page_count=int(m.group(1))
#~ print page_count
for i in range(1,page_count+1):
tot.append(url+"=&page="+str(i))
def chunks(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
if __name__=='__main__':
#~ print generate_all_the_main_page_name()
#~ i_have_got_series_name("http://www.1channel.ch/watch-9460-2020","hello")
#~ i_have_got_page_number('http://www.1channel.ch/?letter=123&tv&page=1')
#~ get_page_count_and_go_deeper('http://www.1channel.ch/?letter=123&tv')
#Generating all the page links using threading
pages = []
tot=[]
pages.append('http://www.1channel.ch/?letter=123&tv')
for i in range(ord('a'),ord('z')+1):
pages.append('http://www.1channel.ch/?letter='+str(chr(i))+'&tv')
thread_pages=[]
for i in range(len(pages)):
threadp = threading.Thread(target=generate_main_pages_thread, args=(pages[i],))
threadp.start()
thread_pages.append(threadp)
for thread in thread_pages:
thread.join()
print 'initial Threading done PAGES FOUND %s ' % len(tot)
#~ tot = generate_all_the_main_page_name()
random.shuffle(tot)
# Generating all the series links using threading
all_series=[]
if len(tot)>THREAD_NUM:
thread_numbers=THREAD_NUM
else:
thread_numbers=1
chunk_length = int(math.ceil(len(tot)/THREAD_NUM))
if chunk_length == 0:
chunk_length = 1
thread_chunks=chunks(tot, chunk_length )
threads=[]
print 'Pages split into: %s' % len(thread_chunks)
for i in range(thread_numbers):
#~ get_all_series_links_thread(thread_chunks[i])
#~ continue
thread = threading.Thread(target=get_all_series_links_thread, args=(thread_chunks[i],))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print 'threading done'
print 'TOTAL SERIES %s' % len(all_series)
#~ all_series=get_all_series_links(tot)
random.shuffle(all_series)
#default encoding ASCII error proned
all_series = [(s.encode('utf-8'),i) for s,i in all_series ]
print all_series[0]
#~ sys.exit()
print 'Total pages to loop over: %s' % len(tot)
print 'Total Series to loop over: %s' % len(all_series)
#~ print tot
#~ print len(tot)
#~ sys.exit()
# Starting multiprocessing
p=Pool(processes=WORKER)
chunk_size = int(math.ceil(len(all_series)/WORKER))
print 'Chunk Size set to: %s' % chunk_size
try:
p.map(start_multiprocessing , all_series)
except:
print 'Process Allocation Error^^^^^^^^^^^^^^^^^'
#~ p.map_async(get_page_count_and_go_deeper,tot)