-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoplayerfunc.py
146 lines (118 loc) · 4.71 KB
/
autoplayerfunc.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import json
import time
import datetime
import requests
from nextpvrinfo import *
FileConfig = 'autoplayer_config.json'
"""Config File filename"""
FileStatus = 'autoplayer_status.json'
"""Status File filename"""
Config_json = {}
"""Current Config"""
def fnGetCurrentWeekday():
"""Takes the Current DateTime, and returns the weekday"""
currentdatetime = datetime.datetime.now()
return (currentdatetime.weekday()) + 1
def fnConvertTime(timetext):
""" Convert Time to Number
timetext: Time as Text ie '12:30'
:return: Integer of hours and minutes
"""
timetextsplit = timetext.split(':')
return (int(timetextsplit[0])*60) + int(timetextsplit[1])
def fnConvertPlaytime(timeint):
""" Convert Playtime to seconds
timeint: Play time in milliseconds
:return: timeint / 1000
"""
return int(timeint / 1000)
def fnLoadConfig():
"""Reads the main config file into a global object"""
global Config_json
global FileConfig
try:
with open(FileConfig) as fp:
Config_json = json.load(fp)
if not 'manual' in Config_json:
Config_json['manual'] = {}
if not 'mode' in Config_json['manual']:
Config_json['manual']['mode'] = 0
except Exception as ex:
print("autoplayerfunc.fnLoadConfig() ", ex)
Config_json = { "sources": [], "schedules": [], "reloadtimeout": 300, "manual": { "mode": 0} }
Config_json['dt'] = int(time.time())
def fnSaveConfigSetMode(newmode):
"""Save the New Mode to config file"""
global Config_json
global FileConfig
try:
with open(FileConfig) as fp:
Config_json = json.load(fp)
if not 'manual' in Config_json:
Config_json['manual'] = {}
if not 'mode' in Config_json['manual']:
Config_json['manual']['mode'] = 0
Config_json['manual']['mode'] = newmode
with open(FileConfig, 'w') as fp:
json.dump(Config_json, fp, indent=4)
except Exception as ex:
print("autoplayerfunc.fnSaveConfigSetMode() ", ex)
Config_json = { "sources": [], "schedules": [], "reloadtimeout": 300, "manual": { "mode": 0} }
Config_json['dt'] = int(time.time())
def fnGetCurrentSchedule():
""" Takes the Current DateTime, and finds the current Source ID
:return: Current SourceID or 0
"""
global Config_json
currentdatetime = datetime.datetime.now()
currentweekday = fnGetCurrentWeekday()
currenttimeint = fnConvertTime(currentdatetime.strftime('%H:%M'))
if (currentweekday == 7):
currentweekday = 0
for scheduleitem in Config_json['schedules']:
if currentweekday in scheduleitem['day']:
if currenttimeint >= fnConvertTime(scheduleitem['start']) and currenttimeint < fnConvertTime(scheduleitem['stop']):
return scheduleitem['source']
return 0
def fnGetSource(sourceid):
""" Finds the Source Object from the Config List
sourceid: Source ID to lookup (int)
:return: Source Object or None
"""
global Config_json
for sourceitem in Config_json['sources']:
if sourceitem['id'] == sourceid:
return sourceitem
return None
def fnGetSourceProgrammeTitle(sourceid):
""" Gets the Programme Title from NextPvr
sourceid: SourceID to lookup from
:return: Will return the lookup from system or empty string
"""
_Source = fnGetSource(sourceid)
if (_Source is not None and 'programme' in _Source):
try:
if 'nextpvr' in _Source['programme']:
# Get Existing NextPvr SID
pvrSid = NextpvrSid(hostip=_Source['programme']['nextpvr']['hostip'], hostport=_Source['programme']['nextpvr']['hostport'])
cSid = pvrSid.GetSid()
# Get NextPvr Info
pvrInfo = NextpvrInfo(hostip=_Source['programme']['nextpvr']['hostip'], hostport=_Source['programme']['nextpvr']['hostport'], pin=_Source['programme']['nextpvr']['pin'], sid=cSid)
responseJSON = pvrInfo.GetChannelCurrent(_Source['programme']['nextpvr']['channel_id'])
cSid = pvrInfo.sid
pvrInfo = None
# Save Sid
pvrSid.SaveSid(cSid)
pvrSid = None
if responseJSON[0]['channel']['listings'][0]['name'] == 'To Be Announced':
return ""
else:
return responseJSON[0]['channel']['listings'][0]['name']
else:
response = requests.get(_Source['programme'])
responseJSON = json.loads(response.text)
return responseJSON
except Exception as ex:
print("fnGetSourceProgrammeTitle(" + str(sourceid) + ") ", ex)
return ""