Project

General

Profile

RE: recreate dvr log » rdl.4.1.py

Rebuild DVR logs - TVH 4.1 - Roberto Resecco, 2015-10-30 18:27

 
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 = '/data2/media/TV/recordings'
24
tempdir = '/tmp/dvr'
25

    
26
channelDefault = '6a175a7e089130b4b7909ef42a040e84'
27
channelNameDefault = 'Cartoonito'
28

    
29
channelfiles = []
30
for (dirpath, dirnames, filenames) in walk(chanconfdir):
31
 channelfiles.extend(filenames)
32
 break
33

    
34
d_channeldata = dict()
35

    
36
for channelfile in channelfiles:
37
 with open('%s/%s' % (chanconfdir,channelfile)) as j_channel:    
38
  channeldata = json.load(j_channel)
39
  d_channeldata[channeldata['name']] = channeldata
40

    
41
movies = list()
42
for (dirpath, dirnames, filenames) in walk(recdir):
43
 for filename in filenames:
44
  if filename.split('.')[-1] == 'mkv':
45
   movies.append( path.join(dirpath,filename) )
46

    
47
for movieFile in movies:
48
 movie = MediaInfo()
49
 movie.Open(movieFile)
50
 duration = movie.Get(Stream.Video,0,"Duration")
51
 startDate = movie.Get(Stream.General,0,"DATE_BROADCASTED")
52
 if startDate == '':
53
  startDate = strftime('%Y-%m-%d %H:%M:%S',gmtime(float(stat(movieFile)[9])))
54
 channel = movie.Get(Stream.General,0,"TVCHANNEL")
55
 if channel == '':
56
  channel = channelNameDefault
57
 summary = movie.Get(Stream.General,0,"SUMMARY")
58
 title = movie.Get(Stream.General,0,"Title")
59
 if title == '':
60
  title = path.splitext(path.basename(movieFile))[0]
61
 movie.Close()
62
 
63
 movieDict = OrderedDict()
64
 startTime = strftime('%s',strptime(startDate,'%Y-%m-%d %H:%M:%S'))
65
 stopTime = str( int('0'+startTime) + ( int('0'+duration) / 1000 ) )
66
 movieDict['enabled'] = 'true'
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'] = channelDefault
75
 movieDict['channelname'] = channelNameDefault
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['files'] =  list()
84
 filesDict = dict()
85
 filesDict['filename'] = movieFile
86
 movieDict['files'].append(filesDict)
87
 movieDict['errorcode'] =  0
88
 movieDict['errors'] =  0
89
 movieDict['dvb_eid'] =  0
90
 movieDict['noresched'] =  False
91
 movieDict['autorec'] =  ""
92
 movieDict['timerec'] =  ""
93
 movieDict['content_type'] =  0
94
 movieDict['broadcast'] =  00000000
95

    
96
 #outFileName = dvrconfdir + '/' + md5.md5(movieDict).hexdigest()
97
 if not path.exists(tempdir):
98
  makedirs(tempdir)
99
 outFileName = tempdir + '/' + md5.md5(str(movieDict)).hexdigest()
100
 with open(outFileName,'wb') as a:
101
  json.dump(movieDict,a,indent=8)
102
  a.write('\n')
(1-1/2)