Created
August 22, 2013 17:32
-
-
Save miclovich/6310309 to your computer and use it in GitHub Desktop.
mr_demo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
DemoTime | |
======== | |
About | |
----- | |
DemoTime is a simple app that searches the Twitter API for what your | |
namesake or any other name(someone with your name, other than you) might have said. | |
It uses the twitter Search API to search for stuff; once that is found, it sends you a | |
message of the tweet to your phone; it will only send a tweet that has more than 5 | |
retweets; suggestive of something popular that your namesake might have mentioned. | |
Dependencies | |
----------- | |
This program has the following dependencies: | |
- Install requests and requests-oauthlib (https://github.com/requests/requests-oauthlib) | |
- python-twitter (https://github.com/bear/python-twitter) | |
Follow instructions in on the python-twitter README | |
- Twillio Python Helper Library | |
In your terminal, you can install it as follows: | |
`pip install twilio` | |
The program also uses the Twilio API. To use it please get some cred from: | |
""" | |
import twilio | |
from twilio.rest import TwilioRestClient | |
import random | |
import requests | |
from requests_oauthlib import OAuth1 | |
import urllib | |
from urlparse import parse_qs | |
"""TWITTER SETTINGS""" | |
CONSUMER_KEY = 'XQLhnep4L02f4nDdrbF9Q' | |
CONSUMER_SECRET = 'Y1R9B3wP7i6YDZlGxhZRFfFmq072zG5xrbyDLHHcY' | |
ACCESS_TOKEN = '28098361-vHm5hC92wlnTaTCSKkOdMR9WDtnwsX3czCTz5tvqQ' | |
ACCESS_TOKEN_SECRET = 'IH3YNi7cJAUSYiG9B7lw9B4XRj45kZatmAc7pISlpkk' | |
# URLS | |
REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token" | |
AUTHORIZE_URL = 'https://api.twitter.com/oauth/authorize?oauth_token=' | |
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token' | |
# RESOURCE SPECIFIC URLS | |
SITE_STREAM_URL = "https://sitestream.twitter.com/1.1/site.json" | |
SEARCH_STREAM_URL = "https://api.twitter.com/1.1/search/tweets.json?q=" | |
"""Twilio Settings""" | |
# TODO -> to use this program, you have to apply for an account on twilio | |
ACCOUNT_SID = "" | |
AUTH_TOKEN = "" | |
# Twilio Number | |
SENDER_ID = "" | |
# Number you are sending to | |
RECIPIENT_ID = "" | |
def send_message(message, SENDER_ID, RECIPIENT_ID): | |
""" This code sends a message to RECIEPIENT using Twilio; Be sure to | |
setup your own Twilio confs above | |
""" | |
if message: | |
try: | |
client = TwilioRestClient(account=ACCOUNT_SID, token=AUTH_TOKEN) | |
client.sms.messages.create(body="%s" % message, to=RECIPIENT_ID, | |
from_=SENDER_ID) | |
except twilio.TwilioRestException as e: | |
print e | |
def oauth_setup(): | |
""" This method is used to get our OAuth1 chap working """ | |
oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET) | |
r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth) | |
_credentials = parse_qs(r.content) | |
resource_owner_key = _credentials.get('oauth_token')[0] | |
resource_owner_secret = _credentials.get('oauth_token_secret')[0] | |
# authorized URL | |
authorized_url = AUTHORIZE_URL + resource_owner_key | |
print "Authorize application by visiting this link: " + authorized_url | |
verifier = raw_input("Please input the verifier: ") | |
oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET, | |
resource_owner_key=resource_owner_key, | |
resource_owner_secret=resource_owner_secret, | |
verifier=verifier) | |
# obtain ACCESS token | |
r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth) | |
credentials = parse_qs(r.content) | |
token = credentials.get('oauth_token')[0] | |
secret = credentials.get('oauth_token_secret')[0] | |
return (token, secret) | |
def get_oauth(): | |
OAUTH_TOKEN, OAUTH_TOKEN_SECRET = oauth_setup() | |
oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET, | |
resource_owner_key=OAUTH_TOKEN, | |
resource_owner_secret=OAUTH_TOKEN_SECRET) | |
return oauth | |
def search_for_tweet(user_handle, name): | |
oauth = get_oauth() | |
# do a search on the name | |
r = requests.get(url=SEARCH_STREAM_URL+urllib.quote(name), auth=oauth) | |
# go through the JSON looking for folks with `name` | |
_json = r.json() | |
# store statuses of this nature temporarily | |
temp_status = [(status['text'], status['user']['screen_name']) | |
for status in _json.get('statuses') | |
if status['user']['name'].startswith(name)] | |
# To make this relatively cheap; only 1 of these will be sent out | |
if len(temp_status) > 0: | |
send_message(random.choice(temp_status)[0], SENDER_ID, RECIPIENT_ID) | |
else: | |
print "I am sorry, your name isn't that popular perhaps!" | |
if __name__ == "__main__": | |
print """ | |
DemoTime is a simple app that searches the Twitter API for what your | |
namesake or any other name(someone with your name, other than you) might have said.\n | |
It uses the twitter Search API to search for stuff; once that is found, it sends you a | |
message of the tweet to your phone; it will only send a tweet that has more than 5 | |
retweets; suggestive of something popular that your namesake might have mentioned. | |
""" | |
print "You will be asked a series of questions and given some instructions to follow" | |
twitter_handle = raw_input("What is your twitter handle? ") | |
name_search = raw_input(""" | |
*** Only messages (tweets) from people who's names you are | |
interested will be sent to you***\n | |
What name would you like to search; | |
""") | |
search_for_tweet(twitter_handle, name_search) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment