Commit 23c9b979 authored by Lysander Trischler's avatar Lysander Trischler

Use youtube-dl to get video information

YouTube changed their API so that we now always get

    {"b'status": ['fail'], 'errorcode': ["2'"], 'reason': ['Invalid

when fetching the video information such as title and uploader. In order
to future-proof the code, use youtube-dl to get those information.
parent 3ef18b72
......@@ -23,18 +23,18 @@ Usage: yfav [ VIDEO_ID ... ]
__author = "Lysander Trischler"
__copyright__ = "Copyright 2012-2015, Lysander Trischler"
__copyright__ = "Copyright 2012-2018, Lysander Trischler"
__license__ = "WTFPL"
__maintainer__ = "Lysander Trischler"
__email__ = ""
__version__ = "1.0.0"
__version__ = "1.0.1"
import os.path
import os
import time
import configparser
import urllib.parse
import urllib.request
import json
import subprocess
me = os.path.basename(__file__)
......@@ -94,23 +94,16 @@ class Video(object):
if not self.title or not
def oneof(self, *keys):
for key in keys:
val = self.get(key, None)
if val:
return val[0]
return None
# three tries to get the information successfully from the API
for i in range(2):
info = Video.get_info(
if not self.title:
# sometimes the keys look like a broken Python 2
# byte string literal
self.title = oneof(info, 'title', "b'title")
self.title = info.get('title', None)
if not = oneof(info, 'author', "b'author") = info.get('uploader', None)
# refetch data from API if sth. went wrong
if self.title and
......@@ -122,10 +115,8 @@ class Video(object):
def get_info(cls, video_id):
info = urllib.request.urlopen('' % video_id)
data = urllib.parse.parse_qs(str(
return data
stdout = subprocess.check_output(['youtube-dl', '--ignore-config', '-j', video_id])
return json.loads(stdout.decode('utf-8'))
def extract_video_id(cls, video_id):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment