-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyko-gui.py
184 lines (139 loc) · 6.42 KB
/
pyko-gui.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
#!/usr/bin/env python
# makes it possible to run the file as a script invoking the interpreter implicitly, e.g. in a CGI context.
__author__ = "Daniel Koifman, Alexander Putilin"
__copyright__ = "Copyright 2014, Daniel Koifman, Alexander Putilin"
__credits__ = ["Daniel Koifman, Alexander Putilin"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "Daniel Koifman"
__email__ = "[email protected]"
__status__ = "Development"
# Written by Daniel Koifman(A.K.A HeliosHype) and Alexander Putilin
# You are allowed to freely use, edit, modify and distribute this software, just make sure to give proper credit :)
# Also, if you want to contribute to the code, fork it. Once you're done, send in a pull request.
# Github: https://github.com/Koifman/PyKo-GUI
# Please make sure do properly document your contribution!
from Tkinter import *
from tkFileDialog import *
from ttk import *
from bs4 import BeautifulSoup
from threading import Thread
from tkMessageBox import *
import requests, pafy, os
class Application(Frame):
def __init__(self, master=None):
self.killDownload = False
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def setFlag(self):
self.killDownload = True
def progressCallback(self, totalBytes, bytesDownloaded, ratio, rate, eta):
self.progress.config(maximum=totalBytes, value=bytesDownloaded)
self.label3.config(text="Download rate: %.0f KBPS ETA: %.0f Seconds" % (rate, eta))
if totalBytes == bytesDownloaded:
self.label3.config(text="Download finished successfully!")
if self.killDownload:
self.progress.config(value=0)
self.label3.config(text="Download aborted!")
self.label4.config(text="You can still download another song.")
self.killDownload = False
raise Exception("Aborted Download")
def downloadSong(self, soup_url):
link_to_download = ""
# stream_url = ""
source_code2 = requests.get(soup_url, verify=False)
plain_text2 = source_code2.text
soup2 = BeautifulSoup(plain_text2)
selected_item = self.lb.get(ACTIVE)
for title in soup2.findAll('a', {'title': selected_item}):
link_to_download = "http://www.youtube.com" + title.get("href")
# stream_url = title.get("href").split("=")[1]
video = pafy.new(link_to_download)
# stream = pafy.new(stream_url)
best = video.getbest(preftype="mp4")
best.download(quiet=True, filepath=self.pathName, callback=self.progressCallback)
def downloadSongList(self, links_list):
counter = 1
for link in links_list:
video = pafy.new(link)
best = video.getbest(preftype="mp4")
self.label4.config(text="Current song: %s\n\t\t\t Finished %d / %d" % (video.title, counter, len(links_list)))
best.download(quiet=False, filepath=self.pathName, callback=self.progressCallback)
counter += 1
def filedialog(self):
getUser = os.getenv('USERNAME')
#Need to change this line to support Linux
self.pathName = askdirectory(initialdir="C:\\Users\\" + getUser + "\\Desktop", title="Where do you want to save the song?")
self.v.set(self.pathName)
def fileDialogList(self):
getUser = os.getenv('USERNAME')
#Need to change this line to support Linux
pathName2 = Open(initialdir="C:\\Users\\" + getUser + "\\Desktop", title="Where is the list located?", filetypes=[('text files', '.txt')])
fl = pathName2.show()
xlist = []
if fl != '':
with open(fl, 'r') as fin:
for line in fin:
xlist.append(line)
t = Thread(target=self.downloadSongList, args=(xlist,))
t.start()
def begin_query(self, s1, s2, list):
if s1 == "" and s2 == "":
showerror("Error", "Can't query an empty string!")
else:
rsn = s1.replace(" ", "+")
ran = s2.replace(" ", "+")
self.url = "https://www.youtube.com/results?search_query=%s+%s" % (rsn, ran)
self.slist = list
source_code = requests.get(self.url, verify=False)
plain_text = source_code.text
self.soup = BeautifulSoup(plain_text)
for SongTitle in self.soup.findAll('a', {'class': 'yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link '}):
self.slist.append(SongTitle.string)
self.main()
def createWidgets(self):
label1 = Label(text="Name of song: ").place(x=60, y=7)
label2 = Label(text="Name of artist: ").place(x=60, y=38)
self.label3 = Label(text="")
self.label3.place(x=120, y=350)
self.label4 = Label(text="")
self.label4.place(x=30, y=370)
entry1 = Entry(width=20)
entry1.place(x=180, y=6.5)
entry2 = Entry(width=20)
entry2.place(x=180, y=37.5)
self.v = StringVar()
self.entry3 = Entry(width=40, textvariable=self.v, state='readonly')
self.entry3.place(y=280, x=60)
songs_list = []
b1 = Button(text="Search", command=lambda: self.begin_query(entry1.get(), entry2.get(), songs_list))
b1.place(x=330, y=5)
b2 = Button(text="Save as...", command=self.filedialog)
b2.place(y=277.5, x=310)
b3 = Button(text="Download", command=lambda: Thread(target=self.downloadSong, args=(self.url,)).start())
b3.place(y=35, x=330)
b4 = Button(text="Kill Download", command=self.setFlag)
b4.place(y=70, x=327)
self.lb = Listbox(width=50)
self.lb.place(x=60, y=100)
self.scrollbar = Scrollbar()
self.scrollbar.place(x=365, y=100, height=165)
self.lb.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.lb.yview)
self.progress = Progressbar(orient="horizontal", length=330, mode="determinate")
self.progress.place(y=310, x=60)
menubar=Menu(root)
filemenu=Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Import List",command=self.fileDialogList)
root.config(menu=menubar)
def main(self):
for item in self.slist:
self.lb.insert(END, item)
root = Tk()
app = Application(master=root)
root.iconbitmap('icon.ico')
root.title("PyKo GUI")
root.geometry("450x450+500+300")
app.mainloop()