SeriesPlugin 2.2.2: Bulk update
[enigma2-plugins.git] / seriesplugin / src / WebChannels.py
1 # -*- coding: utf-8 -*-
2 from __init__ import _
3
4 import re
5
6 from twisted.web.client import getPage
7 from twisted.internet import defer
8
9 import socket
10 from urllib import urlencode
11 from urllib2 import urlopen, Request, URLError
12
13 from Components.config import config
14
15
16 #from SerienRecorder import getUserAgent
17 import datetime, random
18 def getUserAgent():
19         userAgents = [
20                 "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; de) Presto/2.9.168 Version/11.52",
21             "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0",
22             "Mozilla/5.0 (X11; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0",
23             "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
24             "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)",
25             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
26             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36",
27             "Mozilla/5.0 (compatible; Konqueror/4.5; FreeBSD) KHTML/4.5.4 (like Gecko)",
28             "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"
29         ]
30         today = datetime.date.today()
31         random.seed(today.toordinal())
32         return userAgents[random.randint(0, 8)]
33
34 def iso8859_Decode(txt):
35         txt = unicode(txt, 'ISO-8859-1')
36         txt = txt.encode('utf-8')
37         txt = txt.replace('...','').replace('..','').replace(':','')
38
39         # ', ", &, <, and >
40         txt = txt.replace('&amp;','&').replace('&apos;',"'").replace('&gt;','>').replace('&lt;','<').replace('&quot;','"')
41         return txt
42
43
44 class WebChannels(object):
45         def __init__(self, user_callback=None, user_errback=None):
46                 self.user_callback = user_callback
47                 self.user_errback  = user_errback
48
49         def     request(self):
50                 print "[SP] request webpage.."
51                 url = "http://www.wunschliste.de/updates/stationen"
52                 #getPage(url, agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0", headers={'Content-Type':'application/x-www-form-urlencoded'}).addCallback(self.__callback).addErrback(self.__errback)
53                 getPage(url, agent=getUserAgent(), headers={'Content-Type':'application/x-www-form-urlencoded'}).addCallback(self.__callback).addErrback(self.__errback)
54
55         def request_and_return(self):
56                 print "[SP] request_and_return webpage.."
57                 url = "http://www.wunschliste.de/updates/stationen"
58                 req = Request(url, headers={'Content-Type':'application/x-www-form-urlencoded'})
59                 try:
60                         data = urlopen(req).read()
61                 except URLError as e:
62                         self.__errback(str(e))
63                 except socket.timeout as e:
64                         self.__errback(str(e))
65                 return self.__callback(data)
66
67         def __errback(self, error):
68                 print error
69                 if (self.user_errback):
70                         self.user_errback(error)
71
72         def __callback(self, data):
73                 #from SP import iso8859_Decode
74                 stations = re.findall('<option value=".*?>(.*?)</option>', data, re.S)
75                 if stations:
76                         web_chlist = []
77                         for station in stations:
78                                 if station != 'alle':
79                                         station = iso8859_Decode(station)
80                                         station = station.replace(' (Pay-TV)','').replace(' (Schweiz)','').replace(' (GB)','').replace(' (Österreich)','').replace(' (USA)','').replace(' (RP)','').replace(' (F)','').replace('&#x1f512;','')
81                                         #station = station.strip()
82                                         
83                                         web_chlist.append(station)
84
85                 if (self.user_callback):
86                         self.user_callback(web_chlist)
87                 
88                 #web_chlist.sort()
89                 
90                 return web_chlist