-
Notifications
You must be signed in to change notification settings - Fork 0
/
downtify.py
272 lines (218 loc) · 9.1 KB
/
downtify.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
<<<<<<< HEAD
=======
# HOW TO SET INTERVALS
# def hello():
# print "hello, world"
#
# t = Timer(30.0, hello)
# t.start() # after 30 seconds, "hello, world" will be printed
>>>>>>> 79557e0ff509315f3a436d4377dd90a65d1cdc97
import sys
sys.path.append('/Users/andresbravogorgonio/Documents/Proyectos/Downtify/SRC/downtify/')
from Tkinter import *
import urllib2, urllib
import webbrowser
import os
import time
import string
from HTMLParser import HTMLParser
import gdata.youtube.service
import macOSclipboard
import growlNotify
from threading import Timer
class SpiderParser(HTMLParser):
def __init__(self, filename):
HTMLParser.__init__(self)
#f = open( filename ).read()
self.readedInfo = ""
self.feed(open( filename ).read())
def handle_starttag(self, tag, attrs):
if tag == 'h1' and attrs:
for attribute in attrs:
if (attribute[0] == 'title'): #&& (attribute[0] == 'title')
self.readedInfo = attribute[1]
if tag == 'a' and attrs:
for attribute in attrs:
if (attribute[0] == 'title'): #&& (attribute[0] == 'title')
self.readedInfo += " - " + attribute[1]
def getParsedInfo(self):
print "sending the info from the parser "+ self.readedInfo
return self.readedInfo
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
scrollbar = Scrollbar(master, orient=VERTICAL)
self.list = Listbox(frame, yscrollcommand=scrollbar.set)
scrollbar.config(command=self.yview)
scrollbar.pack(side=RIGHT, fill=Y)
self.list.pack(side=LEFT, fill=BOTH, expand=1)
scrollbar2 = Scrollbar(master, orient=VERTICAL)
self.list2 = Listbox(frame, yscrollcommand=scrollbar.set)
scrollbar2.config(command=self.yview)
scrollbar2.pack(side=RIGHT, fill=Y)
self.list2.pack(side=LEFT, fill=BOTH, expand=1)
self.button_get_info = Button(frame, text="GET SONGS", fg="red", command=self.getSongsInfo)
self.button_get_info.pack(side=RIGHT)
self.button_get_info = Button(frame, text="ISPECT THE CLIPBOARD", fg="red", command=self.getSpotifyLinks)
self.button_get_info.pack(side=RIGHT)
#self.configuration()
#Start the ClipBoard inspect with the
self.getSpotifyLinks()
def configuration(self):
#Reader read configuration file
self.archive = "spotify-urls.txt"
#Parsing urls from archive
f = open('spotify-urls.txt')
self.urls = []
for line in f.readlines():
#line_readed = line.partition(':')
self.urls += [ line ]
self.list.insert(END, line)
print line
f.close()
def getSpotifyLinks(self):
#Parsing urls from archive
f = macOSclipboard.paste()
if f.find('http://open.spotify.com') != -1 :
growlNotify.gNotify('Tratando los enlaces del portapapeles','Downtify')
self.urls = []
for line in f.split('\n'):
if line.find('http://open.spotify.com') == 0 :
self.urls += [ line ]
self.list.insert(END, line)
print line
#TODO: we have to manage better the cliboard when we finish to parse
macOSclipboard.copy('')
#Automatizate the process
self.getSongsInfo()
else :
print "No spotify urls in clipboard"
t = Timer(1.0, self.getSpotifyLinks)
t.start()
def getSongsInfo(self):
#Reader read configuration file
print "Trying to get the songs info"
#Check internet connection
if self.test_connection():
self.songs = []
#self.getSpotifyUrl('test') #BORRAME test
i = 0
for url in self.urls :
# getting the urls
filename = self.getSpotifyUrl(url, i)
# parsing the html
songInfo = self.parseHtmlArchive(filename)
# youtube query json
self.SearchAndPrint(songInfo)
i = i + 1
#songInfo = #TODO: parse the archive to get the info
self.songs += [ songInfo ]
self.list2.insert(END, songInfo)
#wait 3 secconds
print "Wait for 3 secconds..."
time.sleep(3)
def getSpotifyUrl(self, url, i):
#webbrowser.open(picture_page) # test
# open the web page picture and read it into a variable
url = url.partition('\n')[0] + '/index.html'
print "Trying to open : "+ url
opener1 = urllib2.build_opener()
page1 = opener1.open(url)
my_html = page1.read()
# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = "eraseme-"+str(i)+".html"
print filename # test
fout = open(filename, "wb")
fout.write(my_html)
fout.close()
return filename
# was it saved correctly?
# test it out ...
# webbrowser.open(filename)
def getYoutubeResults(self, query , i):
#webbrowser.open(picture_page) # test
# open the web page picture and read it into a variable
youtubeGdataurl = "http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&q="
youtubeQuery = self.parseYoutubeQuery(query)
url = youtubeGdataurl + youtubeQuery
print "Trying to open : "+ url
opener1 = urllib2.build_opener()
page1 = opener1.open(url)
my_html = page1.read()
# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = "eraseme-youtube-"+str(i)+".json"
print filename # test
fout = open(filename, "wb")
fout.write(my_html)
fout.close()
return filename
# was it saved correctly?
# test it out ...
# webbrowser.open(filename)
def parseYoutubeQuery(self, query):
queryPartition = query.split(' ')
youtubeQuery = ""
for part in queryPartition :
youtubeQuery += part
youtubeQuery += "%20"
return youtubeQuery
def parseHtmlArchive (self, filename):
archiveparser = SpiderParser(filename)
parsedInfo = archiveparser.getParsedInfo()
print "gettin the info from the parser"+ parsedInfo
growlNotify.gNotify('Getting song: '+ parsedInfo ,'Downtify' )
return parsedInfo
def yview(self, *args):
apply(self.list.yview, args)
def test_connection(self):
if os.system("ping -c 2 www.google.com"):
growlNotify.gNotify('No tienes conexion a internet ;(')
return False
else:
return True
#YOUTUBE DEFS
def PrintEntryDetails(self, entry):
#print 'Video title: %s' % entry.media.title.text
#print 'Video published on: %s ' % entry.published.text
#print 'Video description: %s' % entry.media.description.text
#print 'Video category: %s' % entry.media.category[0].text
#print 'Video tags: %s' % entry.media.keywords.text
print "Coping to clipboard the video url :"+entry.media.player.url
macOSclipboard.copy(entry.media.player.url);
#print 'Video flash player URL: %s' % entry.GetSwfUrl()
#print 'Video duration: %s' % entry.media.duration.seconds
# non entry.media attributes
#print 'Video geo location: %s' % entry.geo.location()
#print 'Video view count: %s' % entry.statistics.view_count
#print 'Video rating: %s' % entry.rating.average
# show alternate formats
# for alternate_format in entry.media.content:
# if 'isDefault' not in alternate_format.extension_attributes:
# print 'Alternate format: %s | url: %s ' % (alternate_format.type,
# alternate_format.url)
# show thumbnails
# for thumbnail in entry.media.thumbnail:
# print 'Thumbnail url: %s' % thumbnail.url
def PrintVideoFeed(self, feed):
for entry in feed.entry:
self.PrintEntryDetails(entry)
return 1
def SearchAndPrint(self, search_terms):
yt_service = gdata.youtube.service.YouTubeService()
query = gdata.youtube.service.YouTubeVideoQuery()
query.vq = search_terms
query.orderby = 'viewCount'
query.racy = 'include'
feed = yt_service.YouTubeQuery(query)
self.PrintVideoFeed(feed)
root = Tk()
app = App(root)
root.mainloop()
# http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&q=search%20query%20here