Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib2, json
- STREAM_URL ="http://chirpstream.twitter.com/2b/user.json"
- REST_URL = "http://api.twitter.com/1/"
- class Client:
- def __init__(self):
- self.friends = []
- self.buffer = ""
- self.userid = None
- password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- handler = urllib2.HTTPBasicAuthHandler(password_mgr)
- opener = urllib2.build_opener(handler, self.on_receive)
- urllib2.install_opener(opener)
- def authenticate(self, username, password):
- password_mgr.add_password(None, REST_URL + "account/verify_credentials.json", username, password)
- password_mgr.add_password(None, STREAM_URL, username, password)
- password_mgr.add_password(None, REST_URL, username, password)
- data = json.load(urlopen(REST_URL + "account/verify_credentials.json"))
- if "error" in data: return False
- self.userid = data["id"]
- return True
- def connect(self):
- urlopen(STREAM_URL, None, 45)
- def on_receive(self, data):
- self.buffer += data
- if data.endswith("\r\n") and self.buffer.strip():
- content = json.loads(self.buffer)
- self.buffer = ""
- if "friends" in content:
- self.friends = content["friends"]
- elif "text" in content:
- to = content["in_reply_to_user"]
- if to and to != self.userid and to not in self.friends: return
- if to == self.userid: print("(REPLY)"),
- print (content["user_name"],": ", content["text"])
- client = Client()
- if client.authenticate("BLKress", "lycos.gsv"):
- client.connect()
- else:
- print ("Login credentials are not valid!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement