Skip to content

Instantly share code, notes, and snippets.

@aleksandarristic
Last active May 3, 2020 10:25
Show Gist options
  • Save aleksandarristic/61ad253b6266dbe61e63f8d9c98a41d9 to your computer and use it in GitHub Desktop.
Save aleksandarristic/61ad253b6266dbe61e63f8d9c98a41d9 to your computer and use it in GitHub Desktop.
termux dloader
#!/usr/bin/env python
import os
import sys
class Choice(object):
def __init__(self, config, message):
self.config = config
self.message = message
self.possible_choices = self.config.keys()
self.selection_done = False
self.choice = None
def select(self):
while not self.selection_done:
self.choice = None
self.print_choices()
self.choice = input('> ')
if self.choice not in self.possible_choices:
print('ERROR: please select one of the options before proceeding!')
else:
self.selection_done = True
def print_choices(self):
print(self.message)
for k, v in self.config.items():
print("\t%s) %s" % (k, v.get('message')))
def command(self):
if self.selection_done:
return self.config.get(self.choice).get('command')
return None
choices = {
'a': {
'message': 'Download Audio (mp3) with YouTube-DL',
'command': 'youtube-dl -o "/data/data/com.termux/files/home/storage/downloads/music/%(title)s.%(ext)s" -x --audio-format mp3 -i'
},
'v': {
'message': 'Download Video with YouTube-DL',
'command': 'youtube-dl -o "/data/data/com.termux/files/home/storage/downloads/video/%(title)s.%(ext)s" -i'
},
'd': {
'message': 'Download file with Wget',
'command': 'wget -P /data/data/com.termux/files/home/storage/downloads/'
},
'r': {
'message': 'Download files from path recursively with Wget',
'command': 'wget --recursive --no-parent -P /data/data/com.termux/files/home/storage/downloads/'
},
'q': {
'message': 'Quit - do nothing',
'command': None
}
}
chooser = Choice(choices, "What do you want to do?")
current_path = os.getcwd()
url = sys.argv[1]
chooser.select()
if chooser.command() is not None:
command = '%s %s' % (chooser.command(), url)
print('[DEBUG] running command %s' % command)
os.system(command)
print('Bye!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment