following changes in httpclient
[enigma2-plugins.git] / lastfm / src / httpclient.py
1 from twisted.internet import reactor\r
2 from twisted.internet.protocol import ClientFactory,connectionDone\r
3 from twisted.web2.client.http import HTTPClientProtocol\r
4 \r
5 from twisted.internet import error \r
6 \r
7 import socket\r
8 \r
9 class myProtocol(HTTPClientProtocol):\r
10     path = "/"\r
11     method = "GET"\r
12     data=""\r
13     header=""\r
14     headerread=False\r
15     def __init__(self,hostname,path,method="GET"):\r
16         self.path=path\r
17         self.method=method\r
18         self.hostname=hostname\r
19         HTTPClientProtocol.__init__(self)\r
20         self.setRawMode()\r
21         \r
22     def rawDataReceived(self,line):\r
23         for l in line.split(self.delimiter): \r
24             if self.headerread:\r
25                 self.data +=l\r
26             else:\r
27                 if l == "":\r
28                     self.headerread=True\r
29                 else:\r
30                     self.header +=l\r
31                 \r
32     def connectionMade(self):\r
33         self.sendLine("%s %s HTTP/1.0"%(self.method,self.path))\r
34         self.sendLine("Host: %s"%self.hostname)\r
35         self.sendLine("User-Agent: enigma2 lastfm")\r
36         self.sendLine("")\r
37 \r
38         \r
39 class myClientFactory(ClientFactory):\r
40 \r
41     initialDelay = 20\r
42     maxDelay = 500\r
43     \r
44     def __init__(self,hostname,path,method="GET",callback=None,errorback=None):\r
45         self.hangup_ok = False\r
46         self.path=path\r
47         self.method=method\r
48         self.callback=callback\r
49         self.errorback=errorback\r
50         self.protocol = myProtocol(hostname,self.path,method=self.method)\r
51         \r
52     def startedConnecting(self, connector):\r
53         pass\r
54     \r
55     def buildProtocol(self, addr):\r
56         return self.protocol\r
57 \r
58     def clientConnectionLost(self, connector, reason):\r
59         if not self.hangup_ok:\r
60             self.callback(self.protocol.data)\r
61     def clientConnectionFailed(self, connector, reason):\r
62         if self.errorback is not None:\r
63             self.errorback(reason.getErrorMessage())\r
64         ClientFactory.clientConnectionFailed(self, connector, reason)\r
65 \r
66 \r
67 def getPage(hostname,port,path,method="GET",callback=None,errorback=None):\r
68         f = myClientFactory(hostname,path,method,callback,errorback)\r
69         try:\r
70             hostname = socket.gethostbyname(hostname)\r
71         except socket.error,e:\r
72             msg = "address %r not found! %s" % (hostname,e)\r
73             if errorback is not None:\r
74                 errorback(msg)\r
75         reactor.connectTCP(hostname, port, f)\r
76 \r
77 def getFile(filename,hostname,port,path,method="GET",callback=None,errorback=None):\r
78         def save(data):\r
79             try:\r
80                 fp = open(filename,"w")\r
81                 fp.write(data)\r
82                 fp.close()\r
83                 callback()\r
84             except Exception,e:\r
85                 print e\r
86                 if errorback is not None:\r
87                     errorback(e)\r
88                 \r
89         f = myClientFactory(hostname,path,method,save,errorback)\r
90         try:\r
91             hostname = socket.gethostbyname(hostname)\r
92         except socket.error,e:\r
93             msg = "address %r not found! %s" % (hostname,e)\r
94             if errorback is not None:\r
95                 errorback(msg)\r
96         reactor.connectTCP(hostname, port, f)\r
97 \r