WebM transcoding added
[vpproxy.git] / plugins / torrenttv_api.py
1 # coding=utf-8
2 """
3 Torrent-TV API communication class
4 Forms requests to API, checks result for errors and returns in desired form (lists or raw data)
5 """
6 __author__ = 'miltador'
7
8 import urllib2
9 import socket
10 import xml.dom.minidom as dom
11
12
13 class TorrentTvApiException(Exception):
14     """
15     Exception from Torrent-TV API
16     """
17     pass
18
19
20 class TorrentTvApi(object):
21     CATEGORIES = {
22         1: 'Детские',
23         2: 'Музыка',
24         3: 'Фильмы',
25         4: 'Спорт',
26         5: 'Общие',
27         6: 'Познавательные',
28         7: 'Новостные',
29         8: 'Развлекательные',
30         9: 'Для взрослых',
31         10: 'Мужские',
32         11: 'Региональные',
33         12: 'Религиозные'
34     }
35
36     @staticmethod
37     def auth(email, password, raw=False):
38         """
39         User authentication
40         Returns user session that can be used for API requests
41
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
46         """
47
48         xmlresult = TorrentTvApi._result(
49             'v2_auth.php?username=' + email + '&password=' + password + '&application=tsproxy')
50         if raw:
51             return xmlresult
52         res = TorrentTvApi._check(xmlresult)
53         session = res.getElementsByTagName('session')[0].firstChild.data
54         return session
55
56     @staticmethod
57     def translations(session, translation_type, raw=False):
58         """
59         Gets list of translations
60         Translations are basically TV channels
61
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
66         """
67         xmlresult = TorrentTvApi._result(
68             'v2_alltranslation.php?session=' + session + '&type=' + translation_type)
69         if raw:
70             return xmlresult
71         res = TorrentTvApi._check(xmlresult)
72         translationslist = res.getElementsByTagName('channel')
73         return translationslist
74
75     @staticmethod
76     def records(session, channel_id, date, raw=False):
77         """
78         Gets list of available record for given channel and date
79
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
84         :return: records list
85         """
86         xmlresult = TorrentTvApi._result(
87             'v2_arc_getrecords.php?session=' + session + '&channel_id=' + channel_id + '&date=' + date)
88         if raw:
89             return xmlresult
90         res = TorrentTvApi._check(xmlresult)
91         recordslist = res.getElementsByTagName('channel')
92         return recordslist
93
94     @staticmethod
95     def archive_channels(session, raw=False):
96         """
97         Gets the channels list for archive
98
99         :param session: valid user session required
100         :param raw: if True returns unprocessed data
101         :return: archive channels list
102         """
103         xmlresult = TorrentTvApi._result(
104             'v2_arc_getchannels.php?session=' + session)
105         if raw:
106             return xmlresult
107         res = TorrentTvApi._check(xmlresult)
108         archive_channelslist = res.getElementsByTagName('channel')
109         return archive_channelslist
110
111     @staticmethod
112     def stream_source(session, channel_id):
113         """
114         Gets the source for Ace Stream by channel id
115
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
119         """
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')
126
127     @staticmethod
128     def archive_stream_source(session, record_id):
129         """
130         Gets stream source for archive record
131
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
135         """
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')
142
143     @staticmethod
144     def _check(xmlresult):
145         """
146         Validates received API answer
147         Raises an exception if error detected
148
149         :param xmlresult: API answer to check
150         :return: minidom-parsed xmlresult
151         :raise: TorrentTvApiException
152         """
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)
158         return res
159
160     @staticmethod
161     def _result(request):
162         """
163         Sends request to API and returns the result in form of string
164
165         :param request: API command string
166         :return: result of request to API
167         :raise: TorrentTvApiException
168         """
169         try:
170             result = urllib2.urlopen('http://api.torrent-tv.ru/' + request + '&typeresult=xml', timeout=10).read()
171             return result
172         except (urllib2.URLError, socket.timeout) as e:
173             raise TorrentTvApiException('Error happened while trying to access API: ' + repr(e))