-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitmote.py
executable file
·71 lines (65 loc) · 2.25 KB
/
twitmote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env python
# Twitmote
# A remote control server for Twitter Direct Messages
# File created: 2009-05-09
# Author: Kyle Krafka
import getpass
import base64
import string # TODO: from string import strip
import httplib
from xml.dom import minidom
from os import system
import time
print "Greetings! Please log your Twitter Bot in."
while True:
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
auth = 'Basic ' + \
string.strip(base64.encodestring(username + ":" + password))
conn = httplib.HTTPConnection('twitter.com')
conn.putrequest('GET', '/direct_messages.xml?count=1')
conn.putheader('Authorization', auth)
conn.endheaders()
resp = conn.getresponse()
if resp.status == 200:
print "You're good.\n"
break
else:
print 'Your credentials could not be verified ("'+\
`resp.status`+' '+\
resp.reason+'"). Please try again.\n'
# Read the data into a string
data = resp.read()
dom = minidom.parseString(data)
since_id = dom.getElementsByTagName('direct_message')[0]\
.getElementsByTagName('id')[0].firstChild.nodeValue
loop_count = 0
# The initial since_id has been set, begin the regular message checking.
while True:
conn.putrequest('GET', '/direct_messages.xml?since_id='+since_id)
conn.putheader('Authorization', auth)
conn.endheaders()
resp = conn.getresponse()
if resp.status == 200:
data = resp.read()
dom = minidom.parseString(data)
# TODO: do this list in reverse
textNodesReversed = dom.getElementsByTagName('text')
textNodesReversed.reverse()
for command in textNodesReversed:
command = command.firstChild.nodeValue
print "Command received:", command
# Put logic for processing the command here, or directly interpret it
system(command)
# if last time, set new since_id
if len(textNodesReversed) > 0: # if there were any new commands
since_id = dom.getElementsByTagName('direct_message')[0]\
.getElementsByTagName('id')[0].firstChild.nodeValue
loop_count = loop_count + 1
print "Just checked: ", loop_count
# Don't exceed API rate limit
time.sleep(36)
else:
print 'There was a problem connecting ("'+\
`resp.status`+' '+\
resp.reason+'"). Will continue to try.\n'