-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
executable file
·147 lines (118 loc) · 4.64 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"usage: {} <url>"
import sys
import Ice
Ice.loadSlice('downloader.ice')
import Downloader
import IceStorm
import binascii
class ProgressEventI(Downloader.ProgressEvent):
def notify(self, clipData, current=None):
print("[INFO] Estado de la descarga:", clipData.status)
class Client(Ice.Application):
def menu(self):
print('\033[1m')
print("Seleccione una opción:")
print("1-Descargar una canción")
print("2-Mostrar las canciones del servidor")
print("3-Copiar una canción del servidor")
print("4-Salir")
print('\033[0m')
opcion = input()
return opcion
def get_topic_manager(self):
key = 'IceStorm.TopicManager.Proxy'
proxy = self.communicator().propertyToProxy(key)
if proxy is None:
print ("property", key, "not set")
return None
print("Using IceStorm in: '%s'" % key)
return IceStorm.TopicManagerPrx.checkedCast(proxy)
def receive(self,transfer, destination_file):
BLOCK_SIZE = 10240
'''
Read a complete file using a Downloader.Transfer object
'''
with open(destination_file, 'wb') as file_contents:
remoteEOF = False
while not remoteEOF:
data = transfer.recv(BLOCK_SIZE)
# Remove additional byte added by str() at server
if len(data) > 1:
data = data[1:]
data = binascii.a2b_base64(data)
remoteEOF = len(data) < BLOCK_SIZE
if data:
file_contents.write(data)
transfer.end()
def run(self, argv):
contador = 0
topic_mgr = self.get_topic_manager()
if not topic_mgr:
print (': invalid proxy')
return 2
ic = self.communicator()
servant = ProgressEventI()
adapter = ic.createObjectAdapter("ProgressAdapter")
subscriber = adapter.addWithUUID(servant)
topic_name = "ProgressTopic"
qos = {}
try:
topic = topic_mgr.retrieve(topic_name)
except IceStorm.NoSuchTopic:
topic = topic_mgr.create(topic_name)
topic.subscribeAndGetPublisher(qos, subscriber)
base = self.communicator().stringToProxy(argv[1])
sf = Downloader.SchedulerFactoryPrx.checkedCast(base)
if not sf:
raise RuntimeError("Invalid proxy")
while(True):
opcion = self.menu()
if(opcion == '1'):
contador = contador + 1
print("[INFO] Especifique la dirección url del vídeo: ")
url = input()
nombre = 'Servidor ' + str(contador)
adapter.activate()
dl = sf.make(nombre)
dl.addDownloadTask(url)
topic.unsubscribe(subscriber)
print()
print('\033[36m' + "[INFO] Descarga completada correctamente, puedes encontrar tu canción en el servidor." + '\033[0m')
print()
sf.kill(nombre)
elif(opcion == '2'):
contador = contador + 1
print("[INFO] Canciones disponibles en el servidor: ")
nombre = 'Servidor ' + str(contador)
dl = sf.make(nombre)
songs = dl.getSongList()
for i in songs:
print('[♫ ]', i)
sf.kill(nombre)
elif(opcion == '3'):
contador = contador + 1
print('[INFO] Indique el nombre específico de la canción.')
print('[INFO] Asegúrese de poner el mismo nombre que el que aparece en la lista de canciones:')
cancion = input()
nombre = 'Servidor ' + str(contador)
dl = sf.make(nombre)
try:
transfer = dl.get(cancion)
self.receive(transfer,cancion)
print()
print('\033[36m' + "[INFO] " + cancion + " recibida correctamente." + '\033[0m')
print()
except Exception as e:
print("[ERROR] Esa canción no está disponible en el servidor.")
elif(opcion == '4'):
return 0;
else:
print("[INFO] Selecciona una opcion válida:")
return 0
if len(sys.argv) != 3:
print(__doc__.format(__file__))
sys.exit(1)
app = Client()
sys.exit(app.main(sys.argv))