Last active
December 18, 2015 18:19
-
-
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
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
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