3 Deepvid Copyright Mikolaj 'lich' Halber 2016
5 from urllib
import request
, error
13 def baseinit(basefile
='deepvid.db'):
14 conn
= sdb
.connect(basefile
)
16 curr
.execute("CREATE TABLE IF NOT EXISTS Videos(ID TEXT, Title TEXT, Views INT, Link TEXT)")
18 def baseadd(data
, basefile
='deepvid.db'):
19 conn
= sdb
.connect(basefile
)
21 data
.append('https://youtu.be/' + data
[0])
22 curr
.execute("INSERT INTO Videos VALUES (?, ?, ?, ?)", tuple(data
))
25 def random_char(length
):
26 """Pretty simple function for getting random strings"""
27 return ''.join(random
.choice(string
.ascii_letters
) for x
in range(length
))
29 def links(key_loc
, iterations
, no_next_page
):
30 """Gets links for videos under 20 views"""
34 with
open(key_loc
, 'r') as myfile
:
35 key
= myfile
.read().replace('\n', '')
38 for just_dont_use_this_var
in range(0, iterations
):
39 #does the search $iterations number of times
40 if not next_page
or no_next_page
:
41 #check if next page was chosen
42 query
= random_char(4)
44 if query
in old_queries
:
47 old_queries
.append(query
)
49 nxt_pg_tok
= "&pageToken={}".format(data
["nextPageToken"])
50 link
= "https://www.googleapis.com/youtube/v3/search" \
51 "?part=snippet&type=video&q={}&key={}&order=date&maxResults=50{}".format( \
52 query
, key
, nxt_pg_tok
)
54 req
= request
.urlopen(link
).read().decode('utf-8')
55 data
= json
.loads(req
)
56 except error
.HTTPError
:
58 if "nextPageToken" in data
:
59 next_page
= 1 #check for next page token
62 if data
["pageInfo"]["totalResults"] == 0:
64 for i
in data
["items"]:
65 if i
["id"]["videoId"] in good_vids
:
66 continue #throw away videos that were already found
67 v_link
= "https://www.googleapis.com/youtube/v3/videos" \
68 "?part=statistics&id={}&key={}".format(i
["id"]["videoId"], key
)
69 v_req
= request
.urlopen(v_link
).read().decode('utf-8')
70 v_data
= json
.loads(v_req
)
71 if int(v_data
["items"][0]["statistics"]["viewCount"]) <= 20:
72 good_vids
.append(v_data
["items"][0]["id"])
73 baseadd([i
["id"]["videoId"], i
["snippet"]["title"], \
74 int(v_data
["items"][0]["statistics"]["viewCount"])])
81 print(links(argv
[1], 150, True))
82 if __name__
== '__main__':