Skip to content

Instantly share code, notes, and snippets.

@zahardzhan
Created September 29, 2011 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahardzhan/1250086 to your computer and use it in GitHub Desktop.
Save zahardzhan/1250086 to your computer and use it in GitHub Desktop.
My first Python script
#!/usr/bin/python
# Script for downloading music from sites:
# www.mnogomp3.net
# www.tenshi.spb.ru
import getopt
import sys
import os
import re
verbose = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "verbose"])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
if o in ("-v", "--verbose"):
verbose = True
# Checkout arguments, passed with command...
if len(args) == 0:
print "Error: No file with artists and albums."
sys.exit(0)
elif len(args) > 1:
print "Error: Too many options."
sys.exit(0)
elif not os.path.exists(args[0]):
print "Error: No such file."
sys.exit(0)
else:
artalbfile = open(args[0])
def preplistfromfile(somefile):
list = somefile.readlines()
for i in xrange(0, len(list)):
list[i] = list[i].rstrip()
return list
artalburlist = preplistfromfile(artalbfile)
for artalburl in artalburlist:
artalb = artalburl.replace("http://www.mnogomp3.net/mp3/", "").split("/")
artist = artalb[0].replace("_", " ").title()
album = artalb[1].replace("_", " ").title()
del artalb
if not (os.path.exists(artist) and os.path.isdir(artist)):
os.mkdir(artist)
os.chdir(artist)
if not (os.path.exists(album) and os.path.isdir(album)):
os.mkdir(album)
os.chdir(album)
if not os.path.exists("sourcepage"):
os.system("wget " + artalburl + " -O sourcepage")
os.system("recode -f UTF8..ascii sourcepage")
if not os.path.exists("urlist"):
os.system("touch urlist")
os.system('cat sourcepage | grep ">mp3<" | sed "s/\[.*href=\\"//" | sed "s/\\".*\]//" | sed "s/^ /http:\/\/www.mnogomp3.net/g" | sed "s/\\".*//g" > urlist')
if not os.path.exists("donelist"):
os.system("touch donelist")
urlistfile = open("urlist")
urlist = preplistfromfile(urlistfile)
urlistfile.close()
donelistfile = open("donelist", 'r')
donelist = preplistfromfile(donelistfile)
donelistfile.close()
for mp3url in urlist:
if mp3url in donelist:
continue
wgetresult = os.system("wget -c --progress=dot --read-timeout=20 --tries=inf " + mp3url)
# failure > 1, success == 0
if wgetresult == 0:
donelistfile = open("donelist", 'a')
donelistfile.write(mp3url + "\n")
donelistfile.close()
os.chdir(os.pardir)
os.chdir(os.pardir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment