Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCC04 avnig #841

Open
wants to merge 1 commit into
base: community
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions 04/avnig/usertweets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from collections import namedtuple
import csv
import os

import tweepy

from config import CONSUMER_KEY, CONSUMER_SECRET
from config import ACCESS_TOKEN, ACCESS_SECRET

DEST_DIR = 'data'
EXT = 'csv'
NUM_TWEETS = 100

Tweet = namedtuple('Tweet', 'id_str created_at text')

class UserTweets(object):

def __init__(self, handle, max_id=None):
"""Get handle and optional max_id.
Use tweepy.OAuthHandler, set_access_token and tweepy.API
to create api interface.
Use _get_tweets() helper to get a list of tweets.
Save the tweets as data/<handle>.csv"""
# ...
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
self.api = tweepy.API(auth)
self.handle = handle
self.max_id = max_id
self.output_file = os.path.join(f"{DEST_DIR}/handle.{EXT}")
self._tweets = list(self._get_tweets())
self._save_tweets()

def _get_tweets(self):
"""Hint: use the user_timeline() method on the api you defined in init.
See tweepy API reference: http://docs.tweepy.org/en/v3.5.0/api.html
Use a list comprehension / generator to filter out fields
id_str created_at text (optionally use namedtuple)"""
# file_tweets = tweets.TWEETS
data_tweets = self.api.user_timeline(screen_name=self.handle, max_id=self.max_id, count=NUM_TWEETS)
for tweet in data_tweets:
yield Tweet(tweet.id_str, tweet.created_at, tweet.text)

def _save_tweets(self):
"""Use the csv module (csv.writer) to write out the tweets.
If you use a namedtuple get the column names with Tweet._fields.
Otherwise define them as: id_str created_at text
You can use writerow for the header, writerows for the rows"""
if not os.path.exists(DEST_DIR):
os.mkdir(DEST_DIR)
with open(self.output_file, 'w') as f:
w = csv.writer(f)
w.writerow(Tweet._fields)
w.writerows(tweet for tweet in self._tweets)

def __len__(self):
"""See http://pybit.es/python-data-model.html"""
return len(self._tweets)

def __getitem__(self, pos):
"""See http://pybit.es/python-data-model.html"""
return self._tweets[pos]


if __name__ == "__main__":
for handle in ('pybites', 'juliansequeira', 'bbelderbos'):
print('--- {} ---'.format(handle))
user = UserTweets(handle)
for tw in user[:5]:
print(tw)
print()