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