forked from ab-anand/Automation-Bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtubeTrailers.py
58 lines (51 loc) · 1.41 KB
/
youtubeTrailers.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
import requests
import os.path
import sys
import re
def previousTrailers(lang):
if os.path.exists(lang+".txt"):
file = open(lang+".txt","r")
else:
file = open(lang+".txt","w+")
fileUrls = file.readlines()
file.close()
print "Previous " + lang +" Movie Trailers"
prevFound = False
for line in fileUrls:
prevFound = True
print line.replace('\n','')
if(prevFound == False):
print "No previous " + lang + " Movie Trailers\n"
def newTrailers(lang, url, trailerUrl):
if os.path.exists(lang+".txt"):
file = open(lang+".txt","r")
else:
file = open(lang+".txt","w+")
fileUrls = file.readlines()
file.close()
found = False
for line in fileUrls:
if str(url) in line:
found = True
if not found:
file = open(lang+".txt","a")
file.write(trailerUrl + url + "\n")
file.close()
print trailerUrl + url
return 1
def trailers(baseUrl, lang):
html = requests.get(baseUrl + lang).text
results = re.findall(r'href=\"\/watch\?v=(.{11})', html)
print("Latest " + lang + " Movie Trailers")
newFound = False
for num in xrange(0,len(results),2):
val = newTrailers(lang, results[num], trailerUrl)
if(val == 1):
newFound = True
if(newFound == False):
print "No new " + lang + " Movie Trailers"
if __name__ == '__main__':
baseUrl = "https://www.youtube.com/results?search_query=latest+trailers+"
trailerUrl = "https://www.youtube.com/watch?v="
previousTrailers(sys.argv[1])
trailers(baseUrl, sys.argv[1])