Python: YouTube Offline | rate my code?
2012-08-06 10:04:59 UTC

I've been learning python on and off for a while now from stuff on the net and this is the first time I've actually completed a project. So was wondering if anyone with some real experience would be willing to look at the code and tell me where I should start improving(in terms of my knowledge) and what I should do with the code(continue to develop or start from scratch).

Language: Python 2.7

Description of the program:
It uses a command line interface that I made from scratch with what I think is called a functional approach and with only a few side effects. It's just under 400 lines and is a single file as I thought it was small enough. It's called YouTube Offline as it downloads and organizes content from youtube, either whole playlists or single videos. It uses youtube-dl to download which is a free open source module available here:
Although the UI is not graphical but through the shell(windows so far) it's still very user friendly and displays a static screen with a prompt, also allowing a few simple commands explained by typing help.

You can keep/use the source if you find it useful but will need to get download youtube-dl and python first as I haven't made an executable.


Three answers:
2012-08-06 12:45:56 UTC
I haven't run it, just looked at the code. Pretty nice. I like the way you handle the print_screen output.

One thing you might try is importing as a module, rather than running it through os.system(). Look at the very bottom of the source and there's a very small main() function that you can duplicate, except for exiting on an error. Add "import youtube_dl" at the top, then call youtube_dl._real_main() and catch the same exceptions (also with "youtube_dl." module name prefixes, except for KeyboardInterrupt) and do your own error message and recovery actions instead of sys.exit().
2012-08-07 05:33:02 UTC
It's nice for a first project.

However, I didn't like the code very much. The driving function accesses some "data[comm_data[0]][comm_data[1]]", initialized gods know where, which I'm to believe is a playlist.

If I want to use some of your code, e.g. to find out if a certain user exists, or to get his playlist, I hardly can: I can't see a decent API for this. I'd want to be able to do "for clip in playlist.clips: ... clip.title ..." instead of doing something with "data[mumbo][jumbo[1]][2][i][0]" as if I were programming punch cards in the '60s.

I suggest you completely decouple the interface from the API. (And instead of print_screen() in the API implementation do logger.log() so programmers would be able to turn it off / redirect it / whatever.)

BTW, you don't have to do HTML parsing: Youtube has data API that returns JSON or XML:

All in all, the code isn't bad. You have motivation/interest, and that's the most important thing.
2016-12-08 13:13:40 UTC
Rate My Code

This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.