Created
May 9, 2014 21:12
-
-
Save pingiun/9217193bde53ab59abb8 to your computer and use it in GitHub Desktop.
This is a simple python program that allows you to input a list of video names and automatically search all those videos on YouTube and get the YouTube urls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# geturls.py by Jelle Besseling | |
# This file is licenced under the MIT Licence (MIT) | |
# The MIT License (MIT) | |
# Copyright (c) 2014 Jelle Besseling | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
# | |
# | |
# Instructions to get a required API key: | |
# 1. Follow the getting started instructions on this page: https://developers.google.com/youtube/v3/getting-started | |
# 2. Go to your new project>APIs & auth>Credentials and replace {API KEY HERE} with your acuired API key. | |
# 3. That's it, have fun getting huge lists of urls. | |
import sys | |
import getopt | |
import os | |
import requests | |
def main(argv): | |
inputfile = '' | |
outputfile = '' | |
api_url = 'https://www.googleapis.com/youtube/v3/search?part=id&maxResults=1&q={}&fields=items%2CpageInfo&key={}' | |
api_key = '{API KEY HERE}' | |
output = '' | |
short = False | |
#Parse the command line arguments | |
try: | |
opts, args = getopt.getopt(argv,'hsi:o:k:f:',['input=','output=','apikey=','keyfile=','short']) | |
except getopt.GetoptError: | |
print('geturls.py -i <inputfile> -o <outputfile>') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print("""General usage: | |
geturls.py -i <inputfile> [-o <outputfile>] [-k <apikey> or -f <keyfile>] -s | |
short option: long option: | |
-i --input : Specifies the input file | |
-o --output : Specifies the output file | |
-k --apikey : Sets the API key, overwrites api_key in geturls.py | |
-f --keyfile : Specifies an API key file | |
-s --short : Enables short URLS (Without http://youtube.com...)""") | |
sys.exit() | |
elif opt in ('-s', '--short'): | |
short = True | |
elif opt in ('-i', '--input'): | |
inputfile = arg | |
elif opt in ('-o', '--output'): | |
outputfile = arg | |
elif opt in ('-k', '--apikey'): | |
api_key = arg | |
elif opt in ('-f', '--keyfile'): | |
try: | |
with open(arg) as f: | |
api_key = f.read() | |
except FileNotFoundError: | |
print('Error: No such file:', arg) | |
#An input file is required | |
if not inputfile: | |
print('Error: Please give a valid inputfile') | |
sys.exit(2) | |
print('Input file is', inputfile) | |
try: | |
with open(inputfile) as f: | |
errors = 0 | |
for i,line in enumerate(f): | |
try: | |
r = requests.get(api_url.format(line, api_key)) | |
json = r.json() | |
except requests.exceptions.ConnectionError: | |
print('Error: No internet connection') | |
errors = -1 | |
break | |
#Check for a YouTube error. If one occurs the process stops. | |
if 'error' in json: | |
if json['error']['errors'][0]['reason'] == 'keyInvalid': | |
print('Error: Invalid API key') | |
errors = -1 | |
break | |
else: | |
print('Error:', json) | |
errors = -1 | |
break | |
#Notify the user if a video couldn't be found | |
elif json['pageInfo']['totalResults'] == 0: | |
print('Error: Couldn\'t get url for: ' + line) | |
errors += 1 | |
else: | |
if short: | |
output += json['items'][0]['id']['videoId'] + '\n' | |
else: | |
output += "http://youtube.com/watch?v=" + json['items'][0]['id']['videoId'] + '\n' | |
if errors == -1: | |
print("Getting URLs failed") | |
else: | |
print('{} of {} urls get'.format(i+1-errors, i+1)) | |
#Output file defaults to "<inputfile> urls.txt" | |
if not outputfile: | |
outputfile = os.path.splitext(inputfile)[0] + ' urls.txt' | |
with open(outputfile, 'w') as f: | |
f.write(output) | |
except FileNotFoundError: | |
print('Error: No such file:', inputfile) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment