[Tutor] Wondering about a project
Dave Angel
davea at davea.name
Tue Feb 3 14:28:43 CET 2015
On 02/03/2015 07:12 AM, Alan Gauld wrote:
> On 02/02/15 18:42, dw wrote:
>
You forgot to tell us the Python version you're targeting. From various
clues in your code, I have to assume you're using 2.7, but if I'm wrong,
there are other comments I should have made.
>> geturl=""
>
> You don't really need this since you assign it inside the loop.
>
>> while True:
>> call(["clear"])
>> geturl= input("Enter Youtube URL ")
Need to use raw_input() for version 2.7. As it stands, you're requiring
the user to enter quotes around his response, and taking the risk that
if he doesn't, or if they're unmatched, he might be doing some
unexpected things.
>> 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))
>>
You need to decide here whether there might be more than one file to
play and/or delete. If your design says there's just one file, then you
should ensure that, perhaps by deleting the others before the loop
below, or by printing an error message. I'll assume you're designing
for the possibility of multiple files on that Youtube page. I don't
know youtube-dl well enough to know if that's possible.
>> #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
That's now "videos", plural.
>> #for x in range(len(YTfile)):
>
> Again this would be prettier using normal python style
> for file in YTFile:
for myfile in YTFile:
call(["gnome-mplayer", myfile)
Besides which, if you had uncommented the range thing, you'd need to
change the [0] below to [x]. Otherwise you'd be playing the first file
multiple times.
>
>> 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':
Here again, your design has to decide whether it'll be all or nothing.
I'll assume you'll be either saving them all, or deleting them all.
>
> 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
save the files by moving them 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 )
You're only deleting the first one. Need a loop here as well.
>
> 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.
>
>
--
DaveA
More information about the Tutor
mailing list