2

Okay, I was trying to make a simple script to download youtube videos using pafy. Currently I have a problem with the global variable video which I use to store what pafy.new('url') returns. Here's the two functions I use:

video = {};

def downloadVideo():

    options = {};
    options['initialdir'] = 'C:\\';
    options['mustexist'] = False;
    options['title'] = 'Download folder';

    dir_path = tkinter.filedialog.askdirectory(**options);
    global video;
    video.getbest(preftype="mp4").download(quiet=True, filepath=dir_path);

def get():
    url = url_entry.get();

    if url == '':
        return

    global video;
    video = pafy.new(url);

    # Some code to display video info

First I use get() function to get the video from url_entry which is a tkinter Entry widget. So far so good, but when I call downloadVideo() I get this error:

AttributeError: 'NoneType' object has no attribute 'download'

Anil_M
  • 10,893
  • 6
  • 47
  • 74
DimChtz
  • 4,043
  • 2
  • 21
  • 39
  • Python 2 or 3? Rather than using globals, change `get` to `return video` at the end, and change `downloadVideo` to take a parameter: `downloadVideo(video)`. That way you don't have to worry about local vs. global and you can find other issues. I'm guessing the code that calls `get` and `downloadVideo` modifies `video` so that `getbest(...)` returns None, whence the error. – cxw Jul 20 '16 at 16:45
  • @cxw Python 3. Btw, the code that calls `get()` and `downloadVideo()` doesn't modify `video`. – DimChtz Jul 20 '16 at 16:50
  • OK, thanks. I stand by my first comment: don't use globals. Other than that, I'm not seeing any obvious problems. If you turn off the globals and still have problems, update the question with the new code and give me a shout. – cxw Jul 20 '16 at 16:54
  • @cxw Thanks man I 'll give it a try – DimChtz Jul 20 '16 at 16:58

1 Answers1

2

Found the problem in this line:

video.getbest(preftype="mp4").download(quiet=True, filepath=dir_path);

This:

video.getbest(preftype="mp4")

actually returned a NoneType object since it didn't contain any mp4 stream. So, it's not exactly a problem, it's just something I should check before calling download(). Now I just get all the streams video.streams and download what I need or just let it download the best available video.getbest().download().

DimChtz
  • 4,043
  • 2
  • 21
  • 39