Skip to content

Instantly share code, notes, and snippets.

@4piu
Created January 4, 2020 08:16
Show Gist options
  • Save 4piu/69e3ea904dc1800b2ce3b8df0fbee7d8 to your computer and use it in GitHub Desktop.
Save 4piu/69e3ea904dc1800b2ce3b8df0fbee7d8 to your computer and use it in GitHub Desktop.
Batch create download task for khinsider using csv. https://github.com/obskyr/khinsider
#!/usr/bin/env python
# https://github.com/obskyr/khinsider
# csv format
# "album forder name", "album-id", " ext1,ext2"
# example
# "Dance Dance Revolution - Disney Eurobeat - Disc 1","dance-dance-revolution-disney-eurobeat-disc-1","flac"
# "Dance Dance Revolution - Disney Eurobeat - Disc 2","dance-dance-revolution-disney-eurobeat-disc-2","mp3"
# "Dance Dance Revolution - Disney Eurobeat - Disc 3","dance-dance-revolution-disney-eurobeat-disc-3-2001","flac,m4a,mp3"
import sys, os
from queue import Queue
from threading import Thread
import csv
import khinsider
class DownloadThread(Thread):
def __init__(self, queue, threadId):
Thread.__init__(self)
self.queue = queue
self.threadId = threadId
def run(self):
while True:
trackId, directory, formatOrder = self.queue.get()
try:
khinsider.download(trackId, path=directory, makeDirs=True, formatOrder=formatOrder, verbose=True)
except:
print('[ERROR] Something went wrong: ' + str(sys.exc_info()[1]))
finally:
self.queue.task_done()
if __name__ == '__main__':
if not (len(sys.argv) == 3 or len(sys.argv) == 4):
print('Usage: batch.py list.csv <parallel> [directory]')
exit()
csvFilePath = sys.argv[1]
parallel = int(sys.argv[2])
directory = sys.argv[3] if len(sys.argv) == 4 else './download'
if not directory.endswith('/'):
directory += '/'
if os.path.isfile(csvFilePath):
queue = Queue()
for i in range(parallel):
worker = DownloadThread(queue, i)
worker.daemon = True
worker.start()
with open(csvFilePath) as csvFile:
for row in csv.reader(csvFile):
trackId = row[1]
dir = directory + row[0]
formatOrder = row[2].replace(' ', '').split(',')
queue.put((trackId, dir, formatOrder))
queue.join()
print('All job finished')
exit()
else:
print('File does not exist')
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment