-
Notifications
You must be signed in to change notification settings - Fork 0
/
url-launcher.py
executable file
·232 lines (188 loc) · 7.49 KB
/
url-launcher.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
#!/usr/bin/env python
# Author: jon bradley
# Date: Nov 14, 2014
# Desc: url-launcher and ytube downloader gui with tkinter.
# deps: (requests, pafy) easy to add them with pip.
# Todo:
# - check to see if the file has alreayd been downloaded. *done*
# - play the video with mplayer etc. *done*
# - fix the wavfile sound, probably in a try/accept block *done*
# - stop using external commands 'qiv' and 'play'.
# I use it in .Xdefaults with urxvt's .url-launcher, so urxvt
# displays the link in any text on the screen that starts with
# http then if you click on that in urxvt, it runs whatever..
# .Xdefaults line looks like this:
# URxvt.url-launcher: /home/kreator/bin/url-launcher.py
# sure you could make up more creative ways to use the script.
# Enjoy.
import pafy
import sys
import locale
import sh
import re
import requests
from PIL import ImageTk, Image
from StringIO import StringIO
import os
import Tkinter as tk
import tkMessageBox
soundfile = "/home/kreator/.i3/pop.wav"
downloadpath = "/home/kreator/Desktop/Youtube/Youtube-url-grabber"
if len(sys.argv) < 2:
print("Usage: {0} <URL>".format(sys.argv[0]))
sys.exit(1)
# this is for setting up the thousands
locale.setlocale(locale.LC_ALL, "")
# grab the url, put it in variable called url
url = sys.argv[1]
# play the sound to know we pressed a link
try:
sh.play("-q", soundfile)
except:
print("Playing {0} failed.".format(soundfile))
def runImageViewer():
print("Image Viewer...")
r = requests.get(url)
i = Image.open(StringIO(r.content))
i.save('/tmp/myNewPic.png')
sh.qiv('--scale_down','/tmp/myNewPic.png', _bg=True)
def runAddToMpd():
print("Add To MPD...")
def runBrowser():
print("Opening in browser...")
sh.chromium(url)
# the callback function for creating new video obj.
def videoInfoStats(stuff):
print(stuff)
# this is commented beause the Label is not created yet.
#progressvar.set("{0}".format(stuff))
#root.update_idletasks()
# the callback function for downloading stats progress.
def dlStats(total, recvd, ratio, rate, eta):
#print(recvd, ratio, rate, eta)
progressvar.set("{0:,} bytes RX\n{1:.2f}k/sec\neta: {2:.2f}s".format(recvd, rate, eta))
root.update_idletasks()
def downloadVideo(v):
if(os.path.exists(downloadpath) == False):
sh.mkdir('-p',downloadpath)
progressvar.set("creating direcotr {0}".format(downloadpath))
root.update_idletasks()
dlfilename = "{0}/{1}.{2}".format(downloadpath, v.title.replace('/','_').encode("ascii","ignore"), v.extension)
if(os.path.exists(dlfilename)):
statinfo = os.stat(dlfilename)
progressvar.set("File already exists:\nyt: {0:,} bytes\nlo: {1:,} bytes".format(v.get_filesize(),statinfo.st_size))
root.update_idletasks()
else:
try:
v.download(filepath=dlfilename, quiet=True, callback=dlStats)
except IOError, e:
tkMessageBox.showwarning("url-launcher","IOError: {0}".format(e))
def hiresDownload():
v = hires
downloadVideo(v)
def loresDownload():
v = lores
downloadVideo(v)
def playVideo():
if(os.path.exists(downloadpath) == False):
sh.mkdir('-p',downloadpath)
progressvar.set("Creating Directory:\n{0}".format(downloadpath))
root.update_idletasks()
hidlpath = "{0}/{1}.{2}".format(downloadpath, hires.title.replace('/','_').encode("ascii","ignore"), hires.extension)
lodlpath = "{0}/{1}.{2}".format(downloadpath, lores.title.replace('/','_').encode("ascii","ignore"), lores.extension)
if(os.path.exists(hidlpath)):
# play the hires video with mplayer
sh.mplayer('-really-quiet',hidlpath, _bg=True)
elif(os.path.exists(lodlpath)):
# play the lores video with mplayer
sh.mplayer('-really-quiet',lodlpath, _bg=True)
elif(os.path.exists("{0}.temp".format(hidlpath))):
# play if there is a hi res 'temp' file
sh.mplayer('-really-quiet',"{0}.temp".format(hidlpath), _bg=True)
elif(os.path.exists("{0}.temp".format(lodlpath))):
# play if there is a hi res 'temp' file
sh.mplayer('-really-quiet',"{0}.temp".format(lodlpath), _bg=True)
else:
progressvar.set("Video does not exist!")
root.update_idletasks()
def runYoutubeDownloader():
# I need to figure out how I can
# title this window in Tk for i3
# to be able to pick it up and
# make it a floating window.
print("Youtube Downloader...")
# make these global so the button callbacks work.
# there should be a better way than these globals but it works now.
global hires
global lores
global progressvar
global root
root = tk.Tk()
root.wm_title("url-launcher")
tf = tk.Frame(root)
bf = tk.Frame(root)
tf.pack(side=tk.TOP)
bf.pack(side=tk.BOTTOM)
textbox = tk.Text(tf, width=40, height=10, state='normal')
textbox.grid(row=0, column=1, rowspan=2)
print("ok.. text box should be there")
video = pafy.new(url.split('&')[0], callback=videoInfoStats)
textbox.insert('end',"{0}\n".format(video.title.encode("ascii","ignore")))
textbox.insert('end'," ======================================\n")
textbox.insert('end'," Author: {0}\n".format(video.author))
textbox.insert('end',"Published: {0}\n".format(video.published))
textbox.insert('end'," Duration: {0}\n".format(video.duration))
textbox.insert('end'," ======================================\n")
for s in video.streams:
if(s.extension == "webm"):
hires = s
elif(s.extension == "3gp"):
lores = s
else:
print("skipping {0} format.".format(s.extension))
#tkMessageBox.showwarning("url-launcher","Skipping {0} format.".format(s.extension))
textbox.insert('end',"hires [{1}]: {0:,} bytes\n".format(hires.get_filesize(), hires.resolution))
textbox.insert('end',"lores [{1}]: {0:,} bytes\n".format(lores.get_filesize(), lores.resolution))
# grab the thumbnail jpg.
req = requests.get(video.thumb)
photo = ImageTk.PhotoImage(Image.open(StringIO(req.content)))
# now this is in PhotoImage format can be displayed by Tk.
plabel = tk.Label(tf,image=photo)
plabel.image = photo
plabel.grid(row=0, column=2)
# a label to display downloading progress.
progressvar = tk.StringVar(root)
progressvar.set("download status")
dlabel = tk.Label(tf, textvariable=progressvar)
dlabel.grid(row=1, column=2)
bthi = tk.Button(bf, text='HiRes', command=hiresDownload, fg='red')
btlo = tk.Button(bf, text='LoRes', command=loresDownload, fg='red')
btpl = tk.Button(bf, text='Play', command=playVideo, fg='red')
btqu = tk.Button(bf, text='Quit!', command=root.destroy, fg='red')
bthi.pack(side=tk.LEFT)
btlo.pack(side=tk.LEFT)
btpl.pack(side=tk.LEFT)
btqu.pack(side=tk.LEFT)
# start the main loop
root.mainloop()
root.destroy()
def main():
vidregex = re.compile('https?://(www\.)?youtu', re.IGNORECASE)
jpgregex = re.compile('https?://\S+\.jpg$', re.IGNORECASE)
pngregex = re.compile('https?://\S+\.png$', re.IGNORECASE)
m3uregex = re.compile('https?://\S+\.m3u$', re.IGNORECASE)
plsregex = re.compile('https?://\S+\.pls$', re.IGNORECASE)
if (vidregex.match(url)):
runYoutubeDownloader()
elif (jpgregex.match(url)):
runImageViewer()
elif (pngregex.match(url)):
runImageViewer()
elif (m3uregex.match(url)):
runAddToMpd()
elif (plsregex.match(url)):
runAddToMpd()
else:
runBrowser()
if __name__ == '__main__':
main()