thatmattbone / twitiwt
A twitter bot for detecting palindromes.
Clone this repository (size: 16.8 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/thatmattbone/twitiwt/
| commit 1: | 7881ef5d78ce |
| parent 0: | 07898c3c08dc |
| branch: | default |
twisted version almost working...buffering issues?
Changed (Δ3.0 KB):
raw changeset »
test_server.py (58 lines added, 0 lines removed)
twitiwt.py (43 lines added, 0 lines removed)
Up to file-list test_server.py:
1 |
#!/usr/bin/env python |
|
2 |
import BaseHTTPServer |
|
3 |
import base64 |
|
4 |
||
5 |
TEST_LINES = ['hello', |
|
6 |
'world', |
|
7 |
'how are', |
|
8 |
'you?'] |
|
9 |
||
10 |
NEWLINE = '\n' |
|
11 |
class TwitterStreamRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
|
12 |
||
13 |
def is_authorized(self): |
|
14 |
try: |
|
15 |
authentication = self.headers['Authorization'] |
|
16 |
decoded_auth = base64.b64decode(authentication.split()[1]) |
|
17 |
||
18 |
username = decoded_auth.split(':')[0] |
|
19 |
password = decoded_auth.split(':')[1] |
|
20 |
||
21 |
print("username: %s" % username) |
|
22 |
print("password: %s" % password) |
|
23 |
||
24 |
return True |
|
25 |
except KeyError: |
|
26 |
return False |
|
27 |
||
28 |
||
29 |
||
30 |
def echo_request(self): |
|
31 |
if(self.is_authorized()): |
|
32 |
request = "GET: %s" % self.path |
|
33 |
||
34 |
self.send_response(200) |
|
35 |
self.send_header('content-type', 'text/plain') |
|
36 |
self.end_headers() |
|
37 |
||
38 |
for line in TEST_LINES: |
|
39 |
self.wfile.write(line) |
|
40 |
self.wfile.write(NEWLINE) |
|
41 |
else: |
|
42 |
self.send_error(401, "Not Authorized") |
|
43 |
||
44 |
def do_GET(self): |
|
45 |
#print(self.headers) |
|
46 |
self.echo_request() |
|
47 |
||
48 |
||
49 |
||
50 |
def run(server_class=BaseHTTPServer.HTTPServer, |
|
51 |
handler_class=BaseHTTPServer.BaseHTTPRequestHandler): |
|
52 |
server_address = ('', 8000) |
|
53 |
httpd = server_class(server_address, handler_class) |
|
54 |
httpd.serve_forever() |
|
55 |
||
56 |
||
57 |
if __name__ == '__main__': |
|
58 |
run(handler_class=TwitterStreamRequestHandler) |
1 |
from twisted.internet import protocol, reactor |
|
2 |
from twisted.web.client import HTTPPageGetter, HTTPClientFactory, _makeGetterFactory |
|
3 |
import base64 |
|
4 |
||
5 |
class StreamingTwitter(HTTPPageGetter): |
|
6 |
receiving_tweets = False |
|
7 |
||
8 |
def lineReceived(self, line): |
|
9 |
if(self.receiving_tweets is True): |
|
10 |
print "got line: %s" % line |
|
11 |
else: |
|
12 |
return HTTPPageGetter.lineReceived(self, line) |
|
13 |
||
14 |
def handleEndHeaders(self): |
|
15 |
self.receiving_tweets = True |
|
16 |
return HTTPPageGetter.handleEndHeaders(self) |
|
17 |
||
18 |
||
19 |
class StreamingTwitterClientFactory(HTTPClientFactory): |
|
20 |
protocol = StreamingTwitter |
|
21 |
||
22 |
||
23 |
def getTwitterStream(url, username, password, contextFactory=None, *args, **kwargs): |
|
24 |
encoded_auth = base64.encodestring("%s:%s" % (username, password)) |
|
25 |
authorization_header = "Basic %s" % encoded_auth |
|
26 |
kwargs.update({'headers' : {'authorization': authorization_header}}) |
|
27 |
||
28 |
return _makeGetterFactory( |
|
29 |
url, |
|
30 |
StreamingTwitterClientFactory, |
|
31 |
contextFactory=contextFactory, |
|
32 |
*args, **kwargs).deferred |
|
33 |
||
34 |
||
35 |
if __name__ == '__main__': |
|
36 |
#d = getTwitterStream('http://localhost:8000', 'mbone', 'helloworld') |
|
37 |
from passwords import TWITTER_USERNAME, TWITTER_PASSWORD |
|
38 |
d = getTwitterStream('http://stream.twitter.com/1/statuses/sample.json', TWITTER_USERNAME, TWITTER_PASSWORD) |
|
39 |
def echo_page(page): |
|
40 |
print(page) |
|
41 |
reactor.stop() |
|
42 |
d.addCallback(echo_page) |
|
43 |
reactor.run() |
