| 1 | #!/usr/bin/python
|
|---|
| 2 | # alias.py by Ambrosa http://www.ambrosa.net
|
|---|
| 3 | # this module is used for copy epg from a channel to another
|
|---|
| 4 |
|
|---|
| 5 | __author__ = "ambrosa http://www.ambrosa.net"
|
|---|
| 6 | __copyright__ = "Copyright (C) 2008-2011 Alessandro Ambrosini"
|
|---|
| 7 | __license__ = "CreativeCommons by-nc-sa http://creativecommons.org/licenses/by-nc-sa/3.0/"
|
|---|
| 8 |
|
|---|
| 9 | import os
|
|---|
| 10 | import sys
|
|---|
| 11 | import ConfigParser
|
|---|
| 12 |
|
|---|
| 13 | # import CrossEPG functions
|
|---|
| 14 | import crossepg
|
|---|
| 15 |
|
|---|
| 16 | # location of local python modules under "scripts/lib" dir.
|
|---|
| 17 | # add it to sys.path()
|
|---|
| 18 | crossepg_instroot = crossepg.epgdb_get_installroot()
|
|---|
| 19 | if crossepg_instroot == False:
|
|---|
| 20 | sys.exit(1)
|
|---|
| 21 | libdir = os.path.join(crossepg_instroot , 'scripts/lib')
|
|---|
| 22 | sys.path.append(libdir)
|
|---|
| 23 |
|
|---|
| 24 | # import local modules
|
|---|
| 25 | import sgmllib
|
|---|
| 26 | import scriptlib
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | class main:
|
|---|
| 30 |
|
|---|
| 31 | # main config file
|
|---|
| 32 | CONF_CONFIGFILENAME = "alias.conf"
|
|---|
| 33 |
|
|---|
| 34 | # log text
|
|---|
| 35 | CONF_LOG_SCRIPT_NAME = "ALIAS EPG"
|
|---|
| 36 | CONF_LOG_PREFIX = "ALIAS "
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | def log(self,s,video=0):
|
|---|
| 40 | self.logging.log(self.CONF_LOG_PREFIX + str(s))
|
|---|
| 41 | if video == 1:
|
|---|
| 42 | self.log2video(str(s))
|
|---|
| 43 |
|
|---|
| 44 | def log2video(self,s):
|
|---|
| 45 | self.logging.log2video_status(str(s))
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | def __init__(self,confdir,dbroot):
|
|---|
| 50 |
|
|---|
| 51 | # initialize logging
|
|---|
| 52 | self.logging = scriptlib.logging_class()
|
|---|
| 53 | # write to video OSD the script name
|
|---|
| 54 | self.logging.log2video_scriptname(self.CONF_LOG_SCRIPT_NAME)
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | CONF_FILE = os.path.join(confdir,self.CONF_CONFIGFILENAME)
|
|---|
| 58 | if not os.path.exists(CONF_FILE) :
|
|---|
| 59 | self.log("ERROR: %s not present" % CONF_FILE)
|
|---|
| 60 | sys.exit(1)
|
|---|
| 61 |
|
|---|
| 62 | config = ConfigParser.ConfigParser()
|
|---|
| 63 | #config.optionxform = str # needed to return case sensitive index
|
|---|
| 64 | config.read(CONF_FILE)
|
|---|
| 65 |
|
|---|
| 66 | # reading [aliases] section
|
|---|
| 67 | temp=config.items("aliases");
|
|---|
| 68 |
|
|---|
| 69 | self.CHANNELLIST = {}
|
|---|
| 70 | # create a dictionary (Python array) with index = channel ID
|
|---|
| 71 | for i in temp:
|
|---|
| 72 | self.CHANNELLIST[i[0]] = unicode(i[1],'utf-8')
|
|---|
| 73 |
|
|---|
| 74 | if len(self.CHANNELLIST) == 0 :
|
|---|
| 75 | self.log("ERROR: [aliases] section empty ?")
|
|---|
| 76 | sys.exit(1)
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | # ----------------------------------------------------------------------
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | def do_epg_alias(self):
|
|---|
| 83 | self.log("--- START PROCESSING ---")
|
|---|
| 84 | self.log("Loading lamedb indexed by channel name")
|
|---|
| 85 | lamedb = scriptlib.lamedb_class()
|
|---|
| 86 |
|
|---|
| 87 | self.log("Initialize CrossEPG database")
|
|---|
| 88 | crossdb = scriptlib.crossepg_db_class()
|
|---|
| 89 | crossdb.open_db()
|
|---|
| 90 |
|
|---|
| 91 | total_events = 0
|
|---|
| 92 |
|
|---|
| 93 | for src_channel in self.CHANNELLIST :
|
|---|
| 94 |
|
|---|
| 95 | dst_channels = self.CHANNELLIST[src_channel].split(',')
|
|---|
| 96 |
|
|---|
| 97 | if src_channel.count('-') == 1 :
|
|---|
| 98 | src_chname = src_channel.split('-')[0].strip(' \n\r').lower()
|
|---|
| 99 | src_provname = src_channel.split('-')[1].strip(' \n\r').lower()
|
|---|
| 100 |
|
|---|
| 101 | src_sidbyname = []
|
|---|
| 102 | src_sidprovidbyname = lamedb.get_sidprovid_byname(src_chname)
|
|---|
| 103 | for p in src_sidprovidbyname:
|
|---|
| 104 | if p[1] == src_provname :
|
|---|
| 105 | # a channel can have zero or more SID (different channel with same name)
|
|---|
| 106 | # return the list [ 0e1f:00820000:0708:00c8:1:0 , 1d20:00820000:2fa8:013e:1:0 , ..... ]
|
|---|
| 107 | # return [] if channel name is not in lamedb
|
|---|
| 108 | src_sidbyname = p[0]
|
|---|
| 109 | ch_sid = lamedb.convert_sid(src_sidbyname)
|
|---|
| 110 | if len(ch_sid) == 0 :
|
|---|
| 111 | self.log("SID \"%s\" invalid, try next" % src_sidbyname)
|
|---|
| 112 | continue
|
|---|
| 113 |
|
|---|
| 114 | src_epgdb_channel = crossepg.epgdb_channels_get_by_freq(ch_sid[2],ch_sid[1],ch_sid[0]);
|
|---|
| 115 | if not src_epgdb_channel :
|
|---|
| 116 | self.log('Source channel "%s" with SID "%s" has not entry in epgdb, try next' % (src_channel,src_sidbyname) )
|
|---|
| 117 | continue
|
|---|
| 118 | else:
|
|---|
| 119 | break
|
|---|
| 120 |
|
|---|
| 121 | # if not sid then exit, go ahead with next src_channel
|
|---|
| 122 | if len(src_sidbyname) == 0:
|
|---|
| 123 | self.log('Source channel "%s" has not SID in lamedb, skip it' % src_channel)
|
|---|
| 124 | continue
|
|---|
| 125 |
|
|---|
| 126 | elif src_channel.count('-') == 2 :
|
|---|
| 127 | tmp = src_channel.split('-')
|
|---|
| 128 | src_sidbyname = "%s:xxxxxxxx:%s:%s:x:x" %(tmp[0].strip(' \n\r'),tmp[1].strip(' \n\r'),tmp[2].strip(' \n\r'))
|
|---|
| 129 |
|
|---|
| 130 | else:
|
|---|
| 131 | self.log("Channel source \"%s\" invalid" % src_channel)
|
|---|
| 132 | continue
|
|---|
| 133 |
|
|---|
| 134 | # convert "0e1f:00820000:0708:00c8:1:0" to sid,tsid,onid
|
|---|
| 135 | # return the list [sid,tsid,onid]
|
|---|
| 136 | src_sid = lamedb.convert_sid(src_sidbyname)
|
|---|
| 137 | if len(src_sid) == 0 :
|
|---|
| 138 | self.log("SID \"%s\" invalid, try next" % src_sidbyname)
|
|---|
| 139 | continue
|
|---|
| 140 |
|
|---|
| 141 | src_epgdb_channel = crossepg.epgdb_channels_get_by_freq(src_sid[2],src_sid[1],src_sid[0]);
|
|---|
| 142 | if not src_epgdb_channel :
|
|---|
| 143 | self.log('Source channel "%s" with SID "%s" has not entry in epgdb, skip it' % (src_channel,src_sidbyname) )
|
|---|
| 144 | continue
|
|---|
| 145 |
|
|---|
| 146 | self.log('Source channel "%s" with SID "%s" found in epgdb, using it' % (src_channel,src_sidbyname) )
|
|---|
| 147 |
|
|---|
| 148 | for dst in dst_channels:
|
|---|
| 149 | dst = dst.strip(' \n\r').lower()
|
|---|
| 150 | dst_sidbyname = lamedb.get_sid_byname(dst)
|
|---|
| 151 | if len(dst_sidbyname) == 0:
|
|---|
| 152 | self.log(' dest. channel "%s" has not SID in lamedb, skip it' % dst)
|
|---|
| 153 | continue
|
|---|
| 154 |
|
|---|
| 155 | for dsid in dst_sidbyname:
|
|---|
| 156 |
|
|---|
| 157 | # convert "0e1f:00820000:0708:00c8:1:0" to sid,tsid,onid
|
|---|
| 158 | # return the list [sid,tsid,onid]
|
|---|
| 159 | dst_sid = lamedb.convert_sid(dsid)
|
|---|
| 160 |
|
|---|
| 161 | if dst_sid == src_sid:
|
|---|
| 162 | # skip if source=destination
|
|---|
| 163 | self.log(' dest. channel "%s" is eq. to source, skip it' % dst)
|
|---|
| 164 | continue
|
|---|
| 165 |
|
|---|
| 166 | num_events = 0
|
|---|
| 167 | self.log(' copying EPG data from "%s" to "%s" sid "%s")' % (src_channel,dst,dsid) )
|
|---|
| 168 | self.log2video("%s -> %s (%d/%d)" % (src_channel,dst,num_events,total_events))
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | # add channel into db
|
|---|
| 172 | # doesn't matter if the channel already exist... epgdb do all the work
|
|---|
| 173 | # this make a reference to the dest. channel
|
|---|
| 174 | crossdb.add_channel(dst_sid)
|
|---|
| 175 |
|
|---|
| 176 | title = src_epgdb_channel.title_first;
|
|---|
| 177 |
|
|---|
| 178 | while (title != None) :
|
|---|
| 179 | #print str(title.start_time)
|
|---|
| 180 | #print str(title.length)
|
|---|
| 181 | #print str(crossepg.epgdb_read_description(title))
|
|---|
| 182 | #print str(crossepg.epgdb_read_long_description(title))
|
|---|
| 183 | #print "-----------------------------------"
|
|---|
| 184 |
|
|---|
| 185 | e_starttime = int(title.start_time)
|
|---|
| 186 | e_length = int(title.length)
|
|---|
| 187 | e_title = crossepg.epgdb_read_description(title).encode('utf-8')
|
|---|
| 188 | e_summarie = crossepg.epgdb_read_long_description(title).encode('utf-8')
|
|---|
| 189 | e_countrycode = "%c%c%c" % (title.iso_639_1, title.iso_639_2, title.iso_639_3)
|
|---|
| 190 |
|
|---|
| 191 | # add_event(start_time , duration , title , summarie , ISO639_language_code , strings_encoded_with_UTF-8)
|
|---|
| 192 | crossdb.add_event(e_starttime, e_length, e_title, e_summarie, e_countrycode, True )
|
|---|
| 193 | num_events += 1
|
|---|
| 194 | title = title.next
|
|---|
| 195 |
|
|---|
| 196 | if (num_events % 25) == 0:
|
|---|
| 197 | self.log2video("%s -> %s (%d/%d)" % (src_channel,dst,num_events,total_events))
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | total_events += num_events
|
|---|
| 201 | self.log(" copied %d events" % num_events)
|
|---|
| 202 |
|
|---|
| 203 | self.log2video("end: copied %d events" % (total_events))
|
|---|
| 204 | # end process, close CrossEPG DB saving data
|
|---|
| 205 | crossdb.close_db()
|
|---|
| 206 | self.log("Copied %d events" % total_events)
|
|---|
| 207 | self.log("--- END ---")
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | # ****************************************************************************************************************************
|
|---|
| 212 |
|
|---|
| 213 | # MAIN CODE: SCRIPT START HERE
|
|---|
| 214 |
|
|---|
| 215 | SCRIPT_DIR = 'scripts/alias/'
|
|---|
| 216 |
|
|---|
| 217 | # get CrossEPG installation dir.
|
|---|
| 218 | crossepg_instroot = crossepg.epgdb_get_installroot()
|
|---|
| 219 | if crossepg_instroot == False:
|
|---|
| 220 | sys.exit(1)
|
|---|
| 221 | scriptlocation = os.path.join(crossepg_instroot , SCRIPT_DIR)
|
|---|
| 222 |
|
|---|
| 223 | # get where CrossEPG save data (dbroot) and use it as script cache repository
|
|---|
| 224 | crossepg_dbroot = crossepg.epgdb_get_dbroot()
|
|---|
| 225 | if crossepg_dbroot == False:
|
|---|
| 226 | sys.exit(1)
|
|---|
| 227 |
|
|---|
| 228 | # initialize script class
|
|---|
| 229 | script_class = main(scriptlocation , crossepg_dbroot)
|
|---|
| 230 |
|
|---|
| 231 | # copy epg
|
|---|
| 232 | script_class.do_epg_alias()
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|