Commit b8e1180d authored by Lysander Trischler's avatar Lysander Trischler

Try three times to get the meta information

parent 81b96104
......@@ -17,7 +17,7 @@
#
"""
yfav – publish your favorite YouTube videos via an Atom feed
yfav – publish your favorite YouTube videos via Atom feed
Usage: yfav [ VIDEO_ID ... ]
"""
......@@ -37,6 +37,8 @@ import configparser
import urllib.parse
import urllib.request
me = os.path.basename(__file__)
class Config(configparser.ConfigParser):
def __init__(self, fp):
configparser.ConfigParser.__init__(self)
......@@ -92,11 +94,31 @@ class Video(object):
self.updated = now()
if not self.title or not self.author:
info = Video.get_info(self.id)
if not self.title:
self.title = info.get('title', ['<Unknown title>'])[0]
if not self.author:
self.author = info.get('author', ['<Unknown author>'])[0]
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(self.id)
if not self.title:
# sometimes the keys look like a broken Python 2
# byte string literal
self.title = oneof(info, 'title', "b'title")
if not self.author:
self.author = oneof(info, 'author', "b'author")
# refetch data from API if sth. went wrong
if self.title and self.author:
break
sys.stderr.write("%s:\n%r" % (me, info))
else:
sys.stderr.write("%s: Cannot fetch metadata from YT API, tried 3 times!\n" % me)
@classmethod
......
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