-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
241 lines (223 loc) · 6.42 KB
/
client.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
import socket
import sys
import thread
import time
import os
import urllib
import base64
from Tkinter import *
import threading
import re
import youtubedl
import readline
COMMANDS=['extra']
RE_SPACE = re.compile('.*\s+$', re.M)
class Completer(object):
def _listdir(self, root):
"List directory 'root' appending the path separator to subdirs."
res = []
for name in os.listdir(root):
path = os.path.join(root, name)
if os.path.isdir(path):
name += os.sep
res.append(name)
return res
def _complete_path(self, path=None):
"Perform completion of filesystem path."
if not path:
return self._listdir('.')
dirname, rest = os.path.split(path)
tmp = dirname if dirname else '.'
res = [os.path.join(dirname, p)
for p in self._listdir(tmp) if p.startswith(rest)]
# more than one match, or single match which does not exist (typo)
if len(res) > 1 or not os.path.exists(path):
return res
# resolved to a single directory, so return list of files below it
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
# exact file match terminates this completion
return [path + ' ']
def complete_extra(self, args):
"Completions for the 'extra' command."
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete(self, text, state):
"Generic readline completion entry point."
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in COMMANDS][state]
# account for last argument ending in a space
line.insert(0,"extra")
if RE_SPACE.match(buffer):
line.append('')
# resolve command to the implementation function
cmd = line[0].strip()
if cmd in COMMANDS:
impl = getattr(self, 'complete_extra' )
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in COMMANDS if c.startswith(cmd)] + [None]
return results[state]
comp=Completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(comp.complete)
RETPORT=21455
host=str(sys.argv[1])
sfname=[]
namelist=[]
s=socket.socket()
listempty=threading.Semaphore(value=1)
listempty.acquire()
down=0
sock=socket.socket()
class App():
def init(self):
self.root = Tk()
self.movs=[]
self.cmovs=[]
self.mylist=Listbox(self.root)
self.scrollbar=Scrollbar(self.root)
self.scrollbar.pack(side=RIGHT,fill=Y)
self.mylist.pack(side=LEFT,fill=BOTH)
self.mylist.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.configure(command=self.mylist.yview)
self.root.resizable(True,True)
self.upd()
self.root.mainloop()
def upd(self):
if(self.cmovs!=self.movs):
print "ADDING!!!!!\n"
cur=self.cmovs
self.mylist.delete(0,END)
self.movs=[]
for i in cur:
self.mylist.insert(END,i)
self.movs.append(i)
self.scrollbar.configure(command=self.mylist.yview)
self.root.after(1000,self.upd)
def askupd(self,nlst):
self.cmovs=nlst
app=App()
def updgui():
global app
app.init()
def uploadsongs():
global sfname
global namelist
while True:
if len(sfname)!=0:
listempty.release()
listempty.acquire()
msg=sfname[0]
inf="FILESENDINGBEGIN:"+os.path.basename(namelist[0])+"BEGIN:"+str(base64.b64encode(open(msg).read()))+"FILESENDINGEND";
s.send("BEGINCOM")
s.send(inf)
s.send("ENDCOM")
namelist=namelist[1:]
sfname=sfname[1:]
if len(sfname)!=0:
listempty.release()
else:
listempty.acquire();
def addsong(fname,sname):
global sfname
global namelist
sfname.append(fname)
namelist.append(sname)
listempty.release()
def download_youtube(link):
global sem
if(len(link)==0):
return;
# print "DOWNLOADING " + link
# os.system("python youtube-dl "+link)
fname=re.findall("\?v=(.*?)\&",link+"&")[0]
print os.path.join(os.getcwd(),fname+".mp4")
if not os.path.isfile(os.getcwd()+"/"+fname+".mp4") :
fd=youtubedl.FileDownloader({"outtmpl":u'%(id)s.%(ext)s'})
for e in youtubedl.gen_extractors():
fd.add_info_extractor(e)
fd.download([link])
if(len(re.findall("http",link))==0):
link="http://"+link
nl=re.findall("span id=\"eow-title\".*?title=\"(.*?)\"",urllib.urlopen(link).read())
if(len(nl)==0):
nl="nameless"
else:
nl=nl[0]
# print "NAME=====================================+++>" +nl[0]
addsong(os.path.join(os.getcwd(),fname+".mp4"),nl)
def download(link,tmp):
global songlist
global namelist
global down
global sem
print len(link)
if(len(re.findall("cache",os.getcwd()))==0):
os.chdir("cache_folder")
while(down>=5):
continue
down+=1
if len(re.findall("youtube",link))!=0:
download_youtube(link)
elif(len(link)>4):
if(link[-4:]==".mp3" and len(re.findall(link,"www"))!=0):
loc=urllib.urlretrieve(link)[0]
move(loc,"./")
addsong((str(os.getcwd())+os.path.basename(loc)),os.path.basename(loc))
else:
if(os.path.isfile(link)):
addsong(link,link)
down-=1
def updateplaylist():
global host
global sock
# s2=socket.socket()
# s2.bind(("0.0.0.0",RETPORT))
sock.listen(5)
while True:
c,addr=sock.accept()
print "RETURNED!!!!!\n"
msg=c.recv(10000)
movs=msg.split('\n')
app.askupd(movs)
print msg
port=int(sys.argv[2])
print "Connecting to ",host,port
s.connect((host,port))
thread.start_new_thread(updgui,())
thread.start_new_thread(uploadsongs,())
s.send("BEGINCOM");
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind(('',0))
RETPORT=sock.getsockname()[1]
print "RETPORT!!!!! ",RETPORT
s.send(str(sock.getsockname()[1]))
s.send("ENDCOM");
thread.start_new_thread(updateplaylist,())
while True:
msg=raw_input("Client >> ")
msg=msg.rstrip()
msg=msg.lstrip()
if len(msg)>8:
if(msg[0]=='\''):
msg=msg[1:]
if(msg[-1]=='\''):
msg=msg[:-1]
print msg
# if not os.path.isfile(msg):
# s.send(msg)
# else:
thread.start_new_thread(download,(msg,1234))
# inf="FILESENDINGBEGIN:"+os.path.basename(msg)+"BEGIN:"+str(base64.b64encode(open(msg).read()))+"FILESENDINGEND";
# inf="FILESENDINGBEGIN:"+os.path.basename(msg)+"BEGIN:"+str(open(msg).read())+"FILESENDINGEND";
# print len(inf)
s.close()