1
|
#!/usr/bin/env python
|
2
|
|
3
|
'''THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
4
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
5
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
6
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
7
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
8
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
9
|
OTHER DEALINGS IN THE SOFTWARE.'''
|
10
|
|
11
|
import json
|
12
|
import md5
|
13
|
from os import makedirs,walk,path,stat
|
14
|
from pprint import pprint
|
15
|
from MediaInfoDLL import MediaInfo,Stream
|
16
|
from time import gmtime,strftime,strptime
|
17
|
from collections import OrderedDict
|
18
|
|
19
|
basedir = '/data2/tvheadend'
|
20
|
confdir = basedir + '/conf'
|
21
|
chanconfdir = confdir + '/epggrab/xmltv/channels'
|
22
|
dvrconfdir = confdir + '/dvr/log'
|
23
|
recdir = basedir + '/recordings'
|
24
|
recdir = '/data2/media/TV/recordings'
|
25
|
tempdir = '/tmp/dvr'
|
26
|
|
27
|
channelDefault = '6a175a7e089130b4b7909ef42a040e84'
|
28
|
channelNameDefault = 'Cartoonito'
|
29
|
|
30
|
channelfiles = []
|
31
|
for (dirpath, dirnames, filenames) in walk(chanconfdir):
|
32
|
channelfiles.extend(filenames)
|
33
|
break
|
34
|
|
35
|
d_channeldata = dict()
|
36
|
|
37
|
for channelfile in channelfiles:
|
38
|
with open('%s/%s' % (chanconfdir,channelfile)) as j_channel:
|
39
|
channeldata = json.load(j_channel)
|
40
|
d_channeldata[channeldata['name']] = channeldata
|
41
|
|
42
|
movies = list()
|
43
|
for (dirpath, dirnames, filenames) in walk(recdir):
|
44
|
for filename in filenames:
|
45
|
if filename.split('.')[-1] == 'mkv':
|
46
|
movies.append( path.join(dirpath,filename) )
|
47
|
|
48
|
for movieFile in movies:
|
49
|
movie = MediaInfo()
|
50
|
movie.Open(movieFile)
|
51
|
duration = movie.Get(Stream.Video,0,"Duration")
|
52
|
startDate = movie.Get(Stream.General,0,"DATE_BROADCASTED")
|
53
|
if startDate == '':
|
54
|
startDate = strftime('%Y-%m-%d %H:%M:%S',gmtime(float(stat(movieFile)[9])))
|
55
|
channel = movie.Get(Stream.General,0,"TVCHANNEL")
|
56
|
if channel == '':
|
57
|
channel = channelNameDefault
|
58
|
summary = movie.Get(Stream.General,0,"SUMMARY")
|
59
|
title = movie.Get(Stream.General,0,"Title")
|
60
|
if title == '':
|
61
|
title = path.splitext(path.basename(movieFile))[0]
|
62
|
movie.Close()
|
63
|
|
64
|
movieDict = OrderedDict()
|
65
|
startTime = strftime('%s',strptime(startDate,'%Y-%m-%d %H:%M:%S'))
|
66
|
stopTime = str( int('0'+startTime) + ( int('0'+duration) / 1000 ) )
|
67
|
movieDict['start'] = startTime
|
68
|
movieDict['start_extra'] = '0'
|
69
|
movieDict['stop'] = stopTime
|
70
|
movieDict['stop_extra'] = '0'
|
71
|
try:
|
72
|
movieDict['channel'] = d_channeldata[channel]['channels'][0]
|
73
|
except KeyError:
|
74
|
movieDict['channel'] = ''
|
75
|
movieDict['channelname'] = channel
|
76
|
movieDict['title'] = { 'ita': title }
|
77
|
movieDict['description'] = { 'ita': summary }
|
78
|
movieDict['pri'] = 2
|
79
|
movieDict['retention'] = 0
|
80
|
movieDict['container'] = -1
|
81
|
movieDict['config_name'] = "fb4a4ca99788b954709fc0a407206be5"
|
82
|
movieDict['creator'] = "rebuild"
|
83
|
movieDict['filename'] = movieFile
|
84
|
movieDict['errorcode'] = 0
|
85
|
movieDict['errors'] = 0
|
86
|
movieDict['dvb_eid'] = 0
|
87
|
movieDict['noresched'] = False
|
88
|
movieDict['autorec'] = ""
|
89
|
movieDict['timerec'] = ""
|
90
|
movieDict['content_type'] = 0
|
91
|
movieDict['broadcast'] = 00000000
|
92
|
|
93
|
#outFileName = dvrconfdir + '/' + md5.md5(movieDict).hexdigest()
|
94
|
if not path.exists(tempdir):
|
95
|
makedirs(tempdir)
|
96
|
outFileName = '/tmp/dvr/' + md5.md5(str(movieDict)).hexdigest()
|
97
|
with open(outFileName,'wb') as a:
|
98
|
json.dump(movieDict,a,indent=8)
|
99
|
a.write('\n')
|