printing subtitle metadata on success

This commit is contained in:
Paweł Płazieński 2016-02-20 16:44:44 +01:00
parent 3174cf0b09
commit 0e3e1021e2

26
napi
View file

@ -9,7 +9,7 @@ import sys
import urllib import urllib
import os import os
import xml.etree.ElementTree as ET from xml.etree.ElementTree import XML
import base64 import base64
import subprocess import subprocess
@ -50,14 +50,23 @@ for movie_filename in args:
"client" : "NapiProjekt", "client" : "NapiProjekt",
"downloaded_subtitles_lang" : language "downloaded_subtitles_lang" : language
} }
try:
request_data = urllib.urlencode(request_data) request_data = urllib.urlencode(request_data)
response = urllib.urlopen(base_url, request_data) response = urllib.urlopen(base_url, request_data)
response_xml = ET.XML(response.read()) response_data = response.read()
content = response_xml.find("subtitles").find("content").text response_xml = XML(response_data)
except: if response_xml.find('status').text != 'success':
print "%s: No subtitles" % (movie_filename,) print "%s: No subtitles found" % (movie_filename,)
sys.exit(2) continue
subtitle_xml = response_xml.find("subtitles")
subtitle_uploader = subtitle_xml.find('uploader').text
subtitle_author = subtitle_xml.find('author').text
subtitle_date = subtitle_xml.find('upload_date').text
content = subtitle_xml.find("content").text
decoded_content = base64.b64decode(content) decoded_content = base64.b64decode(content)
with open(input_filename, "w") as input_file: with open(input_filename, "w") as input_file:
input_file.write(decoded_content) input_file.write(decoded_content)
@ -81,6 +90,7 @@ for movie_filename in args:
os.remove(subtitle_filename) os.remove(subtitle_filename)
sys.exit(3) sys.exit(3)
print "%s: Subtitles fetched successfully to %s" % (movie_filename, subtitle_filename) print "%s: downloaded subtitles: uploader '%s', author '%s', date '%s'" % (movie_filename,
subtitle_uploader, subtitle_author, subtitle_date)
os.remove(input_filename) os.remove(input_filename)