HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Downloading content from YouTube

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
contentyoutubedownloadingfrom

Problem

This downloads content from YouTube either as whole playlists or single videos. I find it very useful but would like to know if it's something close to a program that others could maintain. I also wanted to show it off a bit and get some feedback as I've been learning on my own from the net and would be helpful to know if I'm on a right track.

It works through the shell (Windows for now) and doesn't have a GUI. Because I made a custom command line interface it's very user-friendly so anyone can use it and has a few simple commands which are explained by typing "help". It also downloads the videos using an opensource command line script/program called youtube-dl.

Unfinished project outline

```
import HTMLParser
import urllib2
import os

help_str = '''
* YouTube Offline can be used without these commands.

For single video downloads enter a valid YouTube username.
The users playlists will be shown if any in order by numbers
starting from 1. Select a playlist or video item by simply
entering the items number. When a video number is selected
the download will begin automatically.

Or add multiple items(playlists or videos) to the job queue
with these commands and use the start command to begin downloads.

List of commands:

add Add playlist or videoitem to job queue.
eg. add 12
adds the 12th item in the list to job queue

start Executes tasks in job queue if any.

clear / clear all Clears last job from job queue or clears all jobs.

cancel Navigate backwards through prompts.

up Scroll up / previous page

Scroll down / next page

...hit to exit help...
'''

youtube_header = ['','',
' __ __ ______ __ ____ ________',
' \ \/ /__ __ _/_ __/_ __/ / ___ / __ \/ _/ _/ (_)__ ___ ',
' \ / _ \/ // // / / // / _ \/ -_) / /_/ / _/ _/ / / _ \/ -_)',
' /_/\___/\_,_//_/ \_,_/_.__/\__/ \____/_//_//_/_/_//

Solution

One suggestion I would make is to reduce the length of your functions, and reduce the amount of nesting / indentation. This will make your code easier to follow, and easier to maintain.

As one example, you've got a function to respond to user commands. I would probably approach this by defining a new function to handle each command. You can then describe a mapping from command to function, and call the appropriate function.

e.g.

# Define some functions to handle various user commands.
def start():
    # do something
    pass

def add_item(item):
    # do something with the item
    pass

def show_help():
    # display help message
    pass

# Map from command to the associated function.
COMMANDS = { 'help': show_help,
             'add': add_item,
             'start': start,
           } # etc

# Given user input, split it into a command and its arguments, then call
# the appropriate function.
def handle_command(user_input):
    command = user_input.split()[0]
    arguments = user_input.split()[1:]

    try:
        COMMANDS[command](*arguments)
    except KeyError:
        # command not recognised, so show help.
        show_help()

Code Snippets

# Define some functions to handle various user commands.
def start():
    # do something
    pass

def add_item(item):
    # do something with the item
    pass

def show_help():
    # display help message
    pass

# Map from command to the associated function.
COMMANDS = { 'help': show_help,
             'add': add_item,
             'start': start,
           } # etc

# Given user input, split it into a command and its arguments, then call
# the appropriate function.
def handle_command(user_input):
    command = user_input.split()[0]
    arguments = user_input.split()[1:]

    try:
        COMMANDS[command](*arguments)
    except KeyError:
        # command not recognised, so show help.
        show_help()

Context

StackExchange Code Review Q#14383, answer score: 2

Revisions (0)

No revisions yet.