Skip to content

Instantly share code, notes, and snippets.

@bitchwhocodes
Last active December 18, 2015 18:19
Show Gist options
  • Save bitchwhocodes/5824908 to your computer and use it in GitHub Desktop.
Save bitchwhocodes/5824908 to your computer and use it in GitHub Desktop.
Pull your twitter timeline tweets ( or another user ) using python and tweepy and save to file
import sys
import tweepy
import re
import time
#for encoding to the terminal window
reload(sys)
sys.setdefaultencoding("utf-8")
def replace_url_to_link(value):
# Replace url to link
urls = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", re.MULTILINE|re.UNICODE)
value = urls.sub(r'<a href="\1" target="_blank">\1</a>', value)
# Replace email to mailto
urls = re.compile(r"([\w\-\.]+@(\w[\w\-]+\.)+[\w\-]+)", re.MULTILINE|re.UNICODE)
value = urls.sub(r'<a href="mailto:\1">\1</a>', value)
return value
CONSUMER_KEY = 'CLIENT_ID_REPLACE'
CONSUMER_SECRET = 'CONSUMER_SECRET_REPLACE'
ACCESS_KEY = 'ACCESS_KEY_REPLACE'
ACCESS_SECRET = 'ACCESS_SECRET_REPLACE'
tweetList = []
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
statuses = api.user_timeline('user_name',count=400)
for status in statuses:
dict={}
dict['text'] = replace_url_to_link(status.text)
tweetList.append(dict)
f = open("tweet.txt", "w")
for item in tweetList:
f.write("================\n")
f.write(item['text']+"\n")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment