| 1 | #!/usr/bin/python
|
|---|
| 2 | # Author: Skaman
|
|---|
| 3 | # Date: October 2010
|
|---|
| 4 |
|
|---|
| 5 | from crossepg import *
|
|---|
| 6 |
|
|---|
| 7 | def dump_title(title):
|
|---|
| 8 | log_add("----------EVENT----------")
|
|---|
| 9 | log_add("ID: %d" % title.event_id)
|
|---|
| 10 | log_add("MJD: %d" % title.mjd)
|
|---|
| 11 | log_add("START TIME: %d" % title.start_time)
|
|---|
| 12 | log_add("DURATION: %d" % title.length)
|
|---|
| 13 | log_add("GENRE ID: %d" % title.genre_id)
|
|---|
| 14 | log_add("REVISION: %d" % title.revision)
|
|---|
| 15 | log_add("ISO639: %c%c%c" % (title.iso_639_1, title.iso_639_2, title.iso_639_3))
|
|---|
| 16 | log_add("IS UTF8: %d" % IS_UTF8(title.flags))
|
|---|
| 17 | log_add("DESCRIPTION_CRC: 0x%x" % title.description_crc)
|
|---|
| 18 | log_add("DESCRIPTION_SEEK: %d" % title.description_seek)
|
|---|
| 19 | log_add("DESCRIPTION LENTH: %d" % title.description_length)
|
|---|
| 20 | log_add("DESCRIPTION: %s" % epgdb_read_description(title))
|
|---|
| 21 | log_add("LONG DESCRIPTION CRC: 0x%x" % title.long_description_crc)
|
|---|
| 22 | log_add("LONG DESCRIPTION SEEK: %d" % title.long_description_seek)
|
|---|
| 23 | log_add("LONG DESCRIPTION LENTH: %d" % title.long_description_length)
|
|---|
| 24 | log_add("LONG DESCRIPTION: %s" % epgdb_read_long_description(title))
|
|---|
| 25 |
|
|---|
| 26 | def main():
|
|---|
| 27 | # get dbroot path
|
|---|
| 28 | dbroot = epgdb_get_dbroot()
|
|---|
| 29 |
|
|---|
| 30 | # open epgdb
|
|---|
| 31 | if epgdb_open(dbroot):
|
|---|
| 32 | log_add("EPGDB opened (root=%s)" % dbroot);
|
|---|
| 33 | else:
|
|---|
| 34 | log_add("Error opening EPGDB");
|
|---|
| 35 | epgdb_close();
|
|---|
| 36 | return
|
|---|
| 37 |
|
|---|
| 38 | # load indexes and hashes in memory
|
|---|
| 39 | epgdb_load();
|
|---|
| 40 |
|
|---|
| 41 | ####################
|
|---|
| 42 | # WRITE OPERATIONS #
|
|---|
| 43 | ####################
|
|---|
| 44 |
|
|---|
| 45 | # add an event into db
|
|---|
| 46 | log_add("TEST: add an event into db")
|
|---|
| 47 | nid = 0x01 # original network id
|
|---|
| 48 | tsid = 0x01 # transport service id
|
|---|
| 49 | sid = 0x01 # service id
|
|---|
| 50 | # the 3 values above identify the channel
|
|---|
| 51 |
|
|---|
| 52 | channel = epgdb_channels_add(nid, tsid, sid) # add channel into db and get a reference to the structure
|
|---|
| 53 | # doesn't matter if the channel already exist... epgdb do all the work
|
|---|
| 54 |
|
|---|
| 55 | title = epgdb_title_alloc() # alloc title structure in memory
|
|---|
| 56 | title.event_id = 1 # event id.. it's unique inside a channel
|
|---|
| 57 | title.start_time = 1293840000 # 1/1/2011 timestamp.. it's always referred to gmt+0 without daylight saving
|
|---|
| 58 | title.mjd = epgdb_calculate_mjd(title.start_time) # Modified Julian Date.. if you don't know it you can calulate it with epgdb_calculate_mjd()
|
|---|
| 59 | title.length = 600 # ten minutes.. event duration in seconds
|
|---|
| 60 | title.iso_639_1 = ord('e') # ISO 639 language code.. http://en.wikipedia.org/wiki/ISO_639
|
|---|
| 61 | title.iso_639_2 = ord('n')
|
|---|
| 62 | title.iso_639_3 = ord('g')
|
|---|
| 63 |
|
|---|
| 64 | title.flags = SET_UTF8(title.flags) # necessary only if description and/or long description have utf-8 charset
|
|---|
| 65 |
|
|---|
| 66 | title = epgdb_titles_add(channel, title) # add event in epgdb and return back a reference to the structure
|
|---|
| 67 | # remember to use always the new structure reference
|
|---|
| 68 | # if the event already exist epgdb update it and automatically destroy the new structure
|
|---|
| 69 |
|
|---|
| 70 | # now the event structure is already in epgdb
|
|---|
| 71 | # but we still need to add descriptions
|
|---|
| 72 | epgdb_titles_set_description (title, "our custom event short description");
|
|---|
| 73 | epgdb_titles_set_long_description (title, "our custom event long description");
|
|---|
| 74 |
|
|---|
| 75 | ###################
|
|---|
| 76 | # READ OPERATIONS #
|
|---|
| 77 | ###################
|
|---|
| 78 |
|
|---|
| 79 | # walk inside channels and events
|
|---|
| 80 | log_add("TEST: walk inside channels and events")
|
|---|
| 81 | channel_count = 0
|
|---|
| 82 | channel = epgdb_channels_get_first();
|
|---|
| 83 | while channel != None:
|
|---|
| 84 | log_add("---------CHANNEL---------")
|
|---|
| 85 | log_add("NID: 0x%x" % channel.nid)
|
|---|
| 86 | log_add("TSID: 0x%x" % channel.tsid)
|
|---|
| 87 | log_add("SID: 0x%x" % channel.sid)
|
|---|
| 88 | title_count = 0
|
|---|
| 89 | title = channel.title_first;
|
|---|
| 90 | while title != None:
|
|---|
| 91 | dump_title(title)
|
|---|
| 92 | title = title.next
|
|---|
| 93 | title_count += 1
|
|---|
| 94 | if title_count == 2: # it's only a test so we halt the loop after 2 titles
|
|---|
| 95 | break
|
|---|
| 96 |
|
|---|
| 97 | channel = channel.next
|
|---|
| 98 | channel_count += 1
|
|---|
| 99 | if channel_count == 3: # it's only a test so we halt the loop after 3 channels
|
|---|
| 100 | break
|
|---|
| 101 |
|
|---|
| 102 | # get a channel by nid, tsid, sid
|
|---|
| 103 | log_add("TEST: get a channel by nid, tsid, sid")
|
|---|
| 104 | channel = epgdb_channels_get_by_freq(nid, tsid, sid);
|
|---|
| 105 | if channel:
|
|---|
| 106 | # get an event by channel and timestamp
|
|---|
| 107 | log_add("TEST: get an event by channel and timestamp")
|
|---|
| 108 | title = epgdb_titles_get_by_time (channel, 1293840000); # exist also the api epgdb_titles_get_by_id_and_mjd(channel, event_id, mjd_time)
|
|---|
| 109 | if title:
|
|---|
| 110 | dump_title(title)
|
|---|
| 111 | else:
|
|---|
| 112 | log_add("Title not found")
|
|---|
| 113 | else:
|
|---|
| 114 | log_add("Channel not found")
|
|---|
| 115 |
|
|---|
| 116 | # saving data
|
|---|
| 117 | # it's necessary only if you add new events
|
|---|
| 118 | if epgdb_save(None):
|
|---|
| 119 | log_add("Data saved");
|
|---|
| 120 | else:
|
|---|
| 121 | log_add("Error saving data");
|
|---|
| 122 |
|
|---|
| 123 | # close epgdb and clean memory
|
|---|
| 124 | epgdb_close()
|
|---|
| 125 | epgdb_clean()
|
|---|
| 126 | log_add("EPGDB closed");
|
|---|
| 127 |
|
|---|
| 128 | main()
|
|---|