Skip to content

Instantly share code, notes, and snippets.

@kiproping
Created June 22, 2017 10:27
Show Gist options
  • Save kiproping/00f82a65d4e905ed0d59fbab33e7b4c1 to your computer and use it in GitHub Desktop.
Save kiproping/00f82a65d4e905ed0d59fbab33e7b4c1 to your computer and use it in GitHub Desktop.
Update to dl.py with db connect and status updates
#!/usr/bin/python
#Returns mysql cursor
import MySQLdb
class DBconnect():
def __init__(self, name="cc", host="localhost", user="cc", passwd="cc"):
self.name = name
self.host = host
self.user = user
self.passwd = passwd
def connect(self):
db = MySQLdb.connect(
self.host,
self.user,
self.passwd,
self.name,
charset="utf8",
use_unicode=True
)
return db
@staticmethod
def get_undownloaded_songs(limit):
db = DBconnect()
cursor = db.connect().cursor()
cursor.execute(
"""
SELECT id, file_url FROM songs where status = 0 limit %s;
""",
(limit,)
)
downloaded_songs = [[int(id), file_url.encode('utf-8')] for id, file_url in cursor.fetchall()]
return downloaded_songs
@staticmethod
def set_song_status(status, id):
db = DBconnect()
cursor = db.connect().cursor()
cursor.execute(
"""
UPDATE songs set status = %s where id = %s;
""",(status, id)
)
from subprocess import *
import time
import dbconnect
path = "songs.lst"
sleepaftersong = 15.5 #seconds sleep after each song
sleepaftermax = 700 #seconds sleep after max consecutive songs
maxdownload = 40 #Downloads before long wait
statusdld = 1
statuserr = 2
def main():
count = 0
url = ""
undled = dbconnect.DBconnect.get_undownloaded_songs(maxdownload)
for urlid in undled:
if (count > 0 and count%maxdownload == 0): #Skip waiting at 0
time.sleep(sleepaftermax)
try :
check_output(["wget -c --user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36' --content-disposition -P songs/ "+ urlid[1] ], shell=True)
dbconnect.DBconnect.set_song_status(statusdld, urlid[0])
time.sleep(sleepaftersong)
except CalledProcessError as e:
print e
dbconnect.DBconnect.set_song_status(statusdld, urlid[2])
time.sleep(sleepaftersong)
count += 1
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment