4

I'm trying to use the pytube library to download a bunch of links I have on a .csv file.

EDIT:

WORKING CODE:

   import sys
reload(sys)
sys.setdefaultencoding('Cp1252')

import os.path

from pytube import YouTube
from pprint import pprint

import csv
with open('onedialectic.csv', 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            try:
                yt = YouTube(row[1])
                path = os.path.join('/videos/',row[0])
                path2 = os.path.join(path + '.mp4')
                print(path2)
                if not os.path.exists(path2) :
                                print(row[0] + '\n')
                                pprint(yt.get_videos())
                                yt.set_filename(row[0])
                                video = yt.get('mp4', '360p')
                                video.download('/videos')
            except Exception as e:
                print("Passing on exception %s", e)
                continue
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
TheOlDirtyBastard
  • 167
  • 1
  • 3
  • 12

1 Answers1

5

To install it you need to use

pip install pytube

and then in your code run

from pytube import YouTube

I haven't seen any code examples of using this with csv though, are you sure it's supported?

You can download via command line directly using e.g.

$ pytube -e mp4 -r 720p -f Dancing Scene from Pulp Fiction http://www.youtube.com/watch?v=Ik-RsDGPI5Y

-e, -f and -r are optional, (extension, filename and resolution)

However for you I would suggest maybe the best thing is to put them all in a playlist and then use Jordan Mear's excellent Python Youtube Playlist Downloader

On a footnote, usually all [external] libraries need to be imported. You can read more about importing here, in the python online tutorials

You could maybe do something like this:

import csv
from pytube import YouTube

vidcsvreader = csv.reader(open("videos.csv"), delimiter=",")

header1 = vidcsvreader.next() #header


for id, url in vidcsvreader:
    yt = url  #assign url to var

    #set resolution and filetype
    video = yt.get('mp4', '720p')

    # set a destination directory for download
    video.download('/tmp/')

    break  
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • Thank you for the reply! I do have it installed but still when I try running it as you've mentioned directly via command line I get this error 'usage: pytube [-h] [--extension EXT] [--resolution RES] [--path PATH] [--filename FILENAME] url pytube: error: unrecognized arguments: from Pulp Fiction http://www.youtube.com/watch?v=Ik-RsDGPI5Y' – TheOlDirtyBastard Jul 25 '16 at 03:26
  • Yeah, got it working now. Had to add the ' and ' between Dancing Scene from Pulp Fiction. Maybe this is what I'm missing adding on my code, I'll try it out. – TheOlDirtyBastard Jul 25 '16 at 03:36
  • I'll do that but it's not working yet! How can I show you how my code looks right now ? I'm pretty sure it's just a small thing missing now! :/ – TheOlDirtyBastard Jul 25 '16 at 03:44
  • @TheOlDirtyBastard put it the 'from pytube import YouTube' at the top of your code – Rachel Gallen Jul 25 '16 at 03:50
  • I would like to take this to chat but I can't because I'm on 6 rep. :/ Oh well, I've edited my code on my main page. Thing is I don't need to use pytube import YouTube because I'm using the command line and calling it using subprocess and call. – TheOlDirtyBastard Jul 25 '16 at 03:50
  • I've added it but I'm getting this error now ' File "running.py", line 1 pytube import YouTube ^ SyntaxError: invalid syntax' – TheOlDirtyBastard Jul 25 '16 at 03:51
  • 1,365 videos to be exact! – TheOlDirtyBastard Jul 25 '16 at 03:54
  • after proper installation you should do a for loop to loop through the csv and forget about the subprocess thing .. an example of looping through a csv is outlined here http://stackoverflow.com/questions/9564322/loop-through-rows-of-one-csv-file-to-find-corresponding-data-in-another Also here http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python/ – Rachel Gallen Jul 25 '16 at 04:03
  • Thank you! I'll be doing the same in a bit. – TheOlDirtyBastard Jul 25 '16 at 04:19
  • @TheOlDirtyBastard how are you getting on? any progress? – Rachel Gallen Jul 26 '16 at 20:41
  • yes! I've already figured it out and got it all working! I'll edit my code in a sec :) Thank you for all the help! – TheOlDirtyBastard Jul 26 '16 at 21:14
  • @TheOlDirtyBastard no probs always happy to help :) – Rachel Gallen Jul 27 '16 at 21:41