-
Notifications
You must be signed in to change notification settings - Fork 0
/
recording-finish.py
113 lines (97 loc) · 4.52 KB
/
recording-finish.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python3
import sys
import time
import requests
import os
import re
import json
import subprocess
import yaml
from xml.etree import ElementTree
from modules import episode_and_season
from modules import sonarr_functions
from modules import subtitle_verify
config = yaml.safe_load(open(os.path.split(sys.argv[0])[0]+"/config.yml"))
title = sys.argv[1]
subtitle_input = sys.argv[2]
filepath = sys.argv[3]
filename = sys.argv[4]
date_epoch = sys.argv[5]
error = sys.argv[6]
#Convert tvheadend's time since epoch airdate to %Y-%m-%d format
airdate = time.strftime('%Y-%m-%d', time.localtime(int(date_epoch)))
#User parameters from config file
pushbullet_api = config["pushbullet"]["api"]
output = config["sonarr"]["output"]
tvdb_api = config["tvdb"]["api"]
sonarr_api = config["sonarr"]["api"]
sonarr_ip = config["sonarr"]["host"]
sonarr_port = config["sonarr"]["port"]
print ("*********************************BEGIN*********************************")
print ("Command run: \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"." %(sys.argv[0], sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6]))
#Check if subtitle contains multiple episodes. If yes, split subtitle into list
subtitle = subtitle_verify.verify(subtitle_input)
#Get list of tvdb_id's for a given title
tvdb_id_list = episode_and_season.get_tvdb_id(title)
#Get episode info (season number, episode number, tvdb_id of show, etc.)
try:
episode_info = episode_and_season.get_episode_by_subtitle(tvdb_api, title, subtitle, tvdb_id_list)
except NameError as err:
requests.post("https://api.pushbullet.com/v2/pushes", data = {"type":"note", "body":err.args, "title":"Tvheadend"}, headers={"Access-Token":pushbullet_api})
print(err.args)
try:
episode_info = episode_and_season.get_episode_by_airdate(tvdb_api, title, airdate, tvdb_id_list)
except NameError as err:
requests.post("https://api.pushbullet.com/v2/pushes", data = {"type":"note", "body":err.args, "title":"Tvheadend"}, headers={"Access-Token":pushbullet_api})
print(err.args)
quit()
else:
print ("Episode info found by airdate:")
print (episode_info)
requests.post("https://api.pushbullet.com/v2/pushes", data = {"type":"note", "body":"Episode found by airdate. %s" %(episode_info), "title":"Tvheadend"}, headers={"Access-Token":pushbullet_api})
tvdb_id = episode_info[0][3]
else:
print ("Episode info found by subtitle:")
print (episode_info)
tvdb_id = episode_info[0][3]
#Look up episode in Sonarr and quit if episode is already downloaded.
try:
sonarr_id = sonarr_functions.get_sonarr_id(sonarr_ip, sonarr_port, sonarr_api, tvdb_id)
except NameError as err:
requests.post("https://api.pushbullet.com/v2/pushes", data = {"type":"note", "body":err.args, "title":"Tvheadend"}, headers={"Access-Token":pushbullet_api})
print(err.args)
quit()
file_info = []
for element in episode_info: #For each episode in episode list, look up sonarr episode ID.
file_info.append (sonarr_functions.get_episode_id(sonarr_ip, sonarr_port, sonarr_api, sonarr_id, element))
for index in file_info: #Check if file has been downloaded. If yes, cancel recording. If no, prevent download.
if index[1]:
#file_info[hasFile] is True, cancel recording
print ("Sonarr has already downloaded this episode. Exiting.")
quit()
elif not index[1]:
#file_info[hasFile] is False, unmonitor that episode in Sonarr.
print ("Episode found in Sonarr database, has not been downloaded yet.")
#Determine video resolution height from ffprobe of recording file
jsonout = json.loads(subprocess.check_output(["/usr/bin/ffprobe", "-i", filepath, "-v", "quiet", "-of", "json", "-show_entries", "stream=height"]).decode("utf-8"))
height = str(jsonout["streams"][0]["height"])
print ("Video resolution height is \"%s\"." % height)
#Determine quality from resolution height
if height == "480":
quality = "SDTV"
else:
quality = "HDTV"
#Produce season/episode number in the format of S00E00 from episode_info
episode = ""
for element in episode_info:
season = "S%0.2d" % int(element[1])
episode += "E%0.2d" % int(element[2])
#Generate filename from all attributes and sanitize non-alphanumeric characters
fileout = re.sub("[^0-9a-zA-Z]+", ".", title+"."+season+episode+"."+subtitle_input+"."+height+"p"+"."+quality)+".x262-Antenna.mkv"
print ("Recording filename is %s" % fileout)
#Create symlink to output location
os.symlink(filepath, output+fileout)
#Start Sonarr processing
print ("Start Sonarr processing.")
sonarr_functions.scan(sonarr_ip, sonarr_port, sonarr_api, output+fileout)
print ("********************************FINISH********************************")