forked from gitlaura/get_tweets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_tweets.py
104 lines (85 loc) · 3.01 KB
/
get_tweets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import csv
import time
import re
from papirus import Papirus
from papirus import PapirusText
screen = Papirus()
text = PapirusText()
#http://www.tweepy.org/
import tweepy
#Get your Twitter API credentials and enter them here
import keys
consumer_key = keys.consumer_key
consumer_secret = keys.consumer_secret
access_key = keys.access_key
access_secret = keys.access_secret
#method to get a user's last 100 tweets
def get_tweets(username):
#http://tweepy.readthedocs.org/en/v3.1.0/getting_started.html#api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#set count to however many tweets you want; twitter only allows 200 at once
number_of_tweets = 10
#get tweets
tweets = api.user_timeline(screen_name = username,count = number_of_tweets)
tweets_for_csv = [[tweet.text.encode("utf-8")] for tweet in tweets]
#we always want the latest tweet
tweets_for_pi = tweets_for_csv.pop(0)
#we want to find if tweets are in a series
for num, tweet in enumerate(tweets_for_csv):
firstTweet = str(tweets_for_pi)
thisTweet = str(tweet)
#if the next tweet starts with this '..' we want the next one too
if firstTweet.startswith(".."):
tweets_for_pi.insert(0,thisTweet[2:-2])
#this is the next one in the series
if firstTweet.startswith("..") and thisTweet.startswith("['.."):
#debug
print "[DEBUG] Yes for %d" % num
tweets_for_pi.insert(0,thisTweet[2:-2])
#another format type, this is usually the last one
if thisTweet.endswith("..']"):
#debug
print "[DEBUG] Also yes for %d" % num
tweets_for_pi.insert(0,thisTweet[2:-2])
#but we need to see if we failed to catch one before now
while (num > 0):
num = num - 1
thisTweet = str(tweets_for_pi[num])
if thisTweet.endswith("..']"):
tweets_for_pi.insert(0,thisTweet[2:-2])
print "[DEBUG] Adding prev1 as well"
else:
tweets_for_pi.insert(0,thisTweet[2:-2])
print "[DEBUG] Adding prev2 as well"
break
print "[DEBUG] it broke right"
#if nothing else, the first one is standalone
else: break
for tweet in tweets_for_pi:
text.write(tweet,13)
time.sleep(10)
screen.clear()
#the following is old and won't be used
#create array of tweet information: username, tweet id, date/time, text
#tweets_for_csv = [[username,tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in tweets]
#write to a new csv file from the array of tweets
#print "writing to {0}_tweets.csv".format(username)
#with open("{0}_tweets.csv".format(username) , 'w+') as file:
# writer = csv.writer(file, delimiter='|')
# writer.writerows(tweets_for_csv)
#if we're running this as a script
if __name__ == '__main__':
#get tweets for username passed at command line
if len(sys.argv) == 2:
get_tweets(sys.argv[1])
else:
print "Error: enter one username"
#alternative method: loop through multiple users
# users = ['user1','user2']
# for user in users:
# get_tweets(user)