3 Torrent-TV API communication class
4 Forms requests to API, checks result for errors and returns in desired form (lists or raw data)
6 __author__ = 'miltador'
10 import xml.dom.minidom as dom
13 class TorrentTvApiException(Exception):
15 Exception from Torrent-TV API
20 class TorrentTvApi(object):
37 def auth(email, password, raw=False):
40 Returns user session that can be used for API requests
42 :param email: user email string
43 :param password: user password string
44 :param raw: if True returns unprocessed data
45 :return: unique session string
48 xmlresult = TorrentTvApi._result(
49 'v2_auth.php?username=' + email + '&password=' + password + '&application=tsproxy')
52 res = TorrentTvApi._check(xmlresult)
53 session = res.getElementsByTagName('session')[0].firstChild.data
57 def translations(session, translation_type, raw=False):
59 Gets list of translations
60 Translations are basically TV channels
62 :param session: valid user session required
63 :param translation_type: playlist type, valid values: all|channel|moderation|translation|favourite
64 :param raw: if True returns unprocessed data
65 :return: translations list
67 xmlresult = TorrentTvApi._result(
68 'v2_alltranslation.php?session=' + session + '&type=' + translation_type)
71 res = TorrentTvApi._check(xmlresult)
72 translationslist = res.getElementsByTagName('channel')
73 return translationslist
76 def records(session, channel_id, date, raw=False):
78 Gets list of available record for given channel and date
80 :param session: valid user session required
81 :param channel_id: id of channel in channel list
82 :param date: format %d-%m-%Y
83 :param raw: if True returns unprocessed data
86 xmlresult = TorrentTvApi._result(
87 'v2_arc_getrecords.php?session=' + session + '&channel_id=' + channel_id + '&date=' + date)
90 res = TorrentTvApi._check(xmlresult)
91 recordslist = res.getElementsByTagName('channel')
95 def archive_channels(session, raw=False):
97 Gets the channels list for archive
99 :param session: valid user session required
100 :param raw: if True returns unprocessed data
101 :return: archive channels list
103 xmlresult = TorrentTvApi._result(
104 'v2_arc_getchannels.php?session=' + session)
107 res = TorrentTvApi._check(xmlresult)
108 archive_channelslist = res.getElementsByTagName('channel')
109 return archive_channelslist
112 def stream_source(session, channel_id):
114 Gets the source for Ace Stream by channel id
116 :param session: valid user session required
117 :param channel_id: id of channel in translations list (see translations() method)
118 :return: type of stream and source
120 xmlresult = TorrentTvApi._result(
121 'v2_get_stream.php?session=' + session + '&channel_id=' + channel_id)
122 res = TorrentTvApi._check(xmlresult)
123 stream_type = res.getElementsByTagName('type')[0].firstChild.data
124 source = res.getElementsByTagName('source')[0].firstChild.data
125 return stream_type.encode('utf-8'), source.encode('utf-8')
128 def archive_stream_source(session, record_id):
130 Gets stream source for archive record
132 :param session: valid user session required
133 :param record_id: id of record in records list (see records() method)
134 :return: type of stream and source
136 xmlresult = TorrentTvApi._result(
137 'v2_arc_getstream.php?session=' + session + '&record_id=' + record_id)
138 res = TorrentTvApi._check(xmlresult)
139 stream_type = res.getElementsByTagName('type')[0].firstChild.data
140 source = res.getElementsByTagName('source')[0].firstChild.data
141 return stream_type.encode('utf-8'), source.encode('utf-8')
144 def _check(xmlresult):
146 Validates received API answer
147 Raises an exception if error detected
149 :param xmlresult: API answer to check
150 :return: minidom-parsed xmlresult
151 :raise: TorrentTvApiException
153 res = dom.parseString(xmlresult).documentElement
154 success = res.getElementsByTagName('success')[0].firstChild.data
155 if success == '0' or not success:
156 error = res.getElementsByTagName('error')[0].firstChild.data
157 raise TorrentTvApiException('API returned error: ' + error)
161 def _result(request):
163 Sends request to API and returns the result in form of string
165 :param request: API command string
166 :return: result of request to API
167 :raise: TorrentTvApiException
170 result = urllib2.urlopen('http://api.torrent-tv.ru/' + request + '&typeresult=xml', timeout=10).read()
172 except (urllib2.URLError, socket.timeout) as e:
173 raise TorrentTvApiException('Error happened while trying to access API: ' + repr(e))