Created
November 19, 2014 19:02
-
-
Save nealcaren/7a8525efbb7847f4b5cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:41b24dcf51398b96840d7593bb684cc27da789ef5043a6c598751a9a55d5a88b" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from twitter import * \n", | |
"\n", | |
"from random import choice\n", | |
"from time import sleep\n", | |
"import json\n", | |
"from lxml import html" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# attempt to load up list of items that have already been tweeted.\n", | |
"# If this fails, create a new empty list\n", | |
"\n", | |
"try:\n", | |
" retweeted = json.load(open('retweeted.json', 'r'))\n", | |
"except Exception, e:\n", | |
" retweeted = []\n", | |
"\n", | |
"print len(retweeted)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"936\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Twitter tokens\n", | |
"\n", | |
"consumer_key = ''\n", | |
"consumer_secret = ''\n", | |
"access_token = ''\n", | |
"access_token_secret = ''" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Authenticate\n", | |
"\n", | |
"twitter_client = Twitter(auth=OAuth(access_token, \n", | |
" access_token_secret,\n", | |
" consumer_key, \n", | |
" consumer_secret))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# List of emojis that are possible for the first character. \n", | |
"\n", | |
"emojis = [u'\\U0001f604', u'\\U0001f61c', u'\\U0001f44d', u'\\U0001F61C',\n", | |
" u'\\U0001F60F', u'\\U0001F601', u'\\U0001F603', u'\\U0001F609',\n", | |
" u'\\U0001F60A', u'\\U0001F60C', u'\\U0001F632' ,u'\\U0001F633', u'\\U0001F62E',\n", | |
" u'\\U0001F638', u'\\U0001F63A', u'\\U0001F63C' ,u'\\U0001F64C', u'\\U0001F60E']\n", | |
"print ''.join(emojis)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\ud83d\ude04\ud83d\ude1c\ud83d\udc4d\ud83d\ude1c\ud83d\ude0f\ud83d\ude01\ud83d\ude03\ud83d\ude09\ud83d\ude0a\ud83d\ude0c\ud83d\ude32\ud83d\ude33\ud83d\ude2e\ud83d\ude38\ud83d\ude3a\ud83d\ude3c\ud83d\ude4c\ud83d\ude0e\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def retweetable(status):\n", | |
" '''\n", | |
" Figure out if status is something we want to retweet.\n", | |
" '''\n", | |
" if 'RT' in status['text']:\n", | |
" return False\n", | |
" if status['text'][0] == '@':\n", | |
" return False\n", | |
" if status['id'] in retweeted:\n", | |
" return False\n", | |
" if 'yoursocprof' in status['text']:\n", | |
" return False\n", | |
" if 'yoursocprof' in status['user']:\n", | |
" return False\n", | |
" if 'assistant' in status['text'].lower():\n", | |
" return False\n", | |
" return True\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def search_and_post():\n", | |
" results = twitter_client.search.tweets(q=\"sociology professor\", count = 10)\n", | |
" tweet_count = 0\n", | |
" \n", | |
" \n", | |
" \n", | |
" for status in results['statuses']:\n", | |
" text = status['text'].replace('&','&')\n", | |
" user = status['user']['screen_name']\n", | |
" status_id = status['id']\n", | |
" \n", | |
" # Try to favorite the tweet \n", | |
" try:\n", | |
" twitter_client.favorites.create(_id=status_id)\n", | |
" print 'Favorited'\n", | |
" except Exception, e:\n", | |
" print 'Not favorited'\n", | |
" \n", | |
" # Think about retweeting it\n", | |
" if retweetable(status) == True and tweet_count < 2 :\n", | |
" status = '%s RT @%s: %s' % (choice(emojis),user,text)\n", | |
" status = html.fromstring(status).text\n", | |
" print status\n", | |
"\n", | |
" try:\n", | |
" twitter_client.statuses.update( status=status)\n", | |
" tweet_count = tweet_count + 1\n", | |
" sleep(60*30)\n", | |
" except:\n", | |
" print 'whoops too long or some other error'\n", | |
"\n", | |
" retweeted.append(status_id)\n", | |
" \n", | |
" \n", | |
" # save the retweet file\n", | |
" json.dump(retweeted,open('retweeted.json', 'w'))\n", | |
" \n", | |
" \n", | |
" sleep(60*60)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"while True:\n", | |
" try:\n", | |
" search_and_post()\n", | |
" except:\n", | |
" print 'Doh'\n", | |
" sleep(60*60)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"json.dump(retweeted,open('retweeted.json', 'w'))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"len(retweeted)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment