IT.Tux.Droid 05.04.2011 20:12

СкриптыProstoScript для ProstoPleer’a.

prostoscript.py – умеет создавать плейлисты и/или полностью скачивать музыку из Ваших плейлистов на ProstoPleer’е и топа радиостанций ProstoPleer’а.
  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
# coding: utf-8

######################################################
# #
# ProstoScript для ProstoPleer'a =) #
# Code by IT.Tux.Droid <http://it-tux-droid.ru/> #
# #
######################################################

import pycurl, StringIO, re, os, progressbar
from optparse import OptionParser

data = StringIO.StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.WRITEFUNCTION, data.write)

parser = OptionParser()
parser.add_option('-m', '--mode', dest='mode', metavar='MODE', default='0',
help='select mode: 0 - create playlist, 1 - download songs, 2 - create playlist and download songs')
parser.add_option('-u', '--url', dest='url', metavar='URL',
help='url to playlist or radio: http://prostopleer.com/#/top/msk/nasheradio')
(options, args) = parser.parse_args()

def progress(download_t, download_d, upload_t, upload_d):
if (download_t != 0) and (download_d != download_t):
bar.update((int(download_d)*100)/int(download_t))

def error(text):
z = len(text.decode('utf-8'))
print ' ????????????????'+'?'*z
print ' ? '+text+' ?'
print ' ????????????????'+'?'*z

def download_mp3(folder, song, url):
global bar
print '? '+song
if not os.path.exists(folder):
os.makedirs(folder)
if not os.path.exists(folder+'/'+song+'.mp3'):
bar = progressbar.ProgressBar(maxval=100, widgets=<'? ?', progressbar.Bar(left='<', marker='=', right='>')>).start()
data.truncate(0)
curl.setopt(pycurl.URL, 'http://prostopleer.com/download/'+url)
curl.setopt(curl.PROGRESSFUNCTION, progress)
curl.setopt(curl.NOPROGRESS, 0)
try:
curl.perform()
except:
print '? ?<connection error="error">'
return False
bar.finish()
mp3 = open(folder+'/'+song+'.mp3', 'w')
mp3.write(data.getvalue())
mp3.close()
else:
print '? ?<you already="already" have="have" it="it">'

# Create .m3u playlist and/or download songs from your playlist.
# Example: "playlist('list346790Ntwn', 2)".
def playlist(p, m):
data.truncate(0)
curl.setopt(pycurl.URL,'http://prostopleer.com/'+p)
curl.setopt(curl.NOPROGRESS, 1)
try:
curl.perform()
except:
error('Connection error!')
return False

list_name = re.findall("class=\"results\"> <h2>(<^<>*)</h2>", data.getvalue())<0><20:-2>
musics = re.findall("file_id=\"(<^\">*)\" track_id=\"(<^\">*)\" singer=\"(<^\">*)\" song=\"(<^\">*)\" link", data.getvalue())

z = len(list_name.decode('utf-8'))

if m in <0, 2>:
m3u = open('playlist <'+list_name+'>.m3u', 'w')
for music in musics:
m3u.write('#EXTINF: 0, '+music<2>+' - '+music<3>+'\nhttp://prostopleer.com/download/'+music<0>+'\n')
m3u.close()
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'
print ' ? Playlist from playlist ('+list_name+') was successfully created ?'
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'

if m in <1, 2>:
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'
print '?? Start download songs from your playlist ('+list_name+') ?'
print '??????????????????????????????????????????????????????'+'?'*z+'?'
for music in musics:
download_mp3('playlist <'+list_name+'>', music<2>+' - '+music<3>, music<0>)
print '??????????????????????????????????????????????????????'+'?'*z+'?'
print '?? Songs from playlist ('+list_name+') was successfully downloaded ?'
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'

# Create .m3u playlist and/or download songs from radio.
# Example: "radio('top/msk/nasheradio', 2)".
def radio(r, f):
data.truncate(0)
curl.setopt(pycurl.URL,'http://prostopleer.com/'+r)
curl.setopt(curl.NOPROGRESS, 1)
try:
curl.perform()
except:
error('Connection error!')
return False

radio_name = re.findall("radio-name\">(<^<>*)</span>", data.getvalue())<0>
musics = re.findall("file_id=\"(<^\">*)\" singer=\"(<^\">*)\" song=\"(<^\">*)\" link", data.getvalue())

z = len(radio_name.decode('utf-8'))

if f in <0, 2>:
m3u = open('radio <'+radio_name+'>.m3u', 'w')
for music in musics:
m3u.write('#EXTINF: 0, '+music<1>+' - '+music<2>+'\nhttp://prostopleer.com/download/'+music<0>+'\n')
m3u.close()
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'
print ' ? Playlist from radio ('+radio_name+') was successfully created ?'
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'

if f in <1, 2>:
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'
print '?? Start download songs from radio ('+radio_name+') ?'
print '??????????????????????????????????????????????????????'+'?'*z+'?'
for music in musics:
download_mp3('radio <'+radio_name+'>', music<1>+' - '+music<2>, music<0>)
print '??????????????????????????????????????????????????????'+'?'*z+'?'
print '?? Songs from radio ('+radio_name+') was successfully downloaded ?'
print ' ?????????????????????????????????????????????????????'+'?'*z+'?'

if options.mode in <'0', '1', '2'>:
if options.url != None:
u = re.findall('(list.*)', options.url)
if u:
playlist(u<0>, int(options.mode))
else:
u = re.findall('(top/.*)', options.url)
if u:
radio(u<0>, int(options.mode))
else:
error('Error URL!')
else:
error('Enter URL!')
else:
error('Error MODE!')

curl.close()
</you></connection>


Зависимости: python, python-pycurl, python-progressbar.
$ sudo apt-get install python python-pycurl python-progressbar

PS: буду рад предложениям и замечаниям ;)
PPS: нет, 60 рублей в месяц совсем не много, но скрипт я все-же написал =)


Тэги: python script
+ 2 -
Похожие Поделиться

knicefire 06.04.2011 10:17 #
хороший стиль кода (на мой взгляд)