-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSyncThread.py
More file actions
38 lines (32 loc) · 1.37 KB
/
SyncThread.py
File metadata and controls
38 lines (32 loc) · 1.37 KB
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
#-*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
import urllib2, json
class SyncThread(QThread):
api_link = "https://api.vk.com/method/"
change_status = pyqtSignal(QString, int)
sync_complete = pyqtSignal()
def __init__(self, token, path, parent = None):
QThread.__init__(self, parent)
self.token = token
self.path = path
def run(self):
response = urllib2.urlopen("%saudio.get?access_token=%s" % (self.api_link, self.token), "r")
audios = json.loads(response.read())
audios = audios['response']
i = 1
for audio in audios:
self.change_status.emit(u"Скачивание файла %d \ %d" % (i, len(audios)), round(float(i) / float(len(audios)) * 100))
filename = u"%s/%s - %s.mp3" % (self.path, audio['artist'][:25], audio['title'][:25])
#filename = filename.encode('utf-8')
filename = filename.replace('|','_').replace('"','_').replace('?','_')
print filename
if not os.path.exists(filename):
mp3 = open(filename + u'.tmp','wb')
remote_mp3 = urllib2.urlopen(audio['url'])
mp3.write(remote_mp3.read())
mp3.close()
os.rename(filename + u'.tmp', filename)
i += 1
self.sync_complete.emit()