[Tutor] Wondering about a project

Alan Gauld alan.gauld at btinternet.com
Tue Feb 3 13:12:42 CET 2015


On 02/02/15 18:42, dw wrote:

> geturl=""

You don't really need this since you assign it inside the loop.

> while True:
>      call(["clear"])
>      geturl= input("Enter Youtube URL ")
>      if len(geturl)==0:
>          break
>
>      def unquote(geturl):
>          return re.compile('%([0-9a-fA-F]{2})',re.M).sub(lambda m:
>          chr(int(m.group(1),16)), geturl)

Its not a good idea to define a functin inside a loop.
Everytime the loop is executed you redefine the function all over again, 
which is a waste. Keep the function definitions out of
your main code.

>      print("Download %s?" %geturl)
>      answer= raw_input("Press ENTER to continue: ")

If you are using Python2.7 then you should also use
raw_input above to get the url.

>      call(["youtube-dl", geturl])
>
> #get the youtube video file names
>      YTfile=glob.glob('./*.mp4')
>      print("%s Youtube files: "%(YTfile))
>
> #clean up the file names
>      for x in range(len(YTfile)):
> 	YTfile[x] = YTfile[x][2:]
>          print("%s: "+str(x)+" "+YTfile[x]+'\n')

this would be prettier using enumerate:

for x, file in enumerate(YTFile):
     YTFile[x] = file[2:]
     print("%s:%d" % (file, x))

> #now play the youtube video
>      #for x in range(len(YTfile)):

Again this would be prettier using normal python style
for file in YTFile:

>      call(["gnome-mplayer", YTfile[0]])
>
> #Decide to save the file or delete it
>      dofile = raw_input('Save or Delete: S or D ')
>      if dofile=='S':

You will get annoyed always having to press shift key to
get caps. You should check for one case after forcing
conversion:

if dofile.upper() == 'S':


Although after writing a GUI that will be less of an
issue because you'll be using a dialog to get the choice.

> #save the file by moving it into folder
>          for x in range(len(YTfile)):
>              shutil.move(YTfile[x],'/home/user/Documents/YOUTUBEFILES')

Again, don't use indexing, Let python do the work, its more reliable.

for file in YTFile:
     shutil.move(file, 'your/path')

> #delete the file
>      else:
>          os.remove(YTfile[0])
>          print("Deleted %s "%YTfile[0])
>          time.sleep( 3 )

Note that this else will delete the file even if the user presses 's' 
instead of 'S' unless you do the upper conversion I show above.
It will also delete the file if the user happens to hit return
before 's'...
I'd recommend an explicit test for 'D' here.
But again, not an issue in a GUI version.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list