2
\$\begingroup\$

I have a module collection.py that functions as a tool to collect tweets using tweepy. It has two classes : Authentication and Collection.

Authentication class will create an object that acts as a starting point to collect data from twitter. An instance of this will the an argument for Collection.

Collection class will create a 'collection' object, it has different methods on how to collect the data, by Collect_object.collect(method = ...), through home_timeline, through Cursor(api.home_timeline, ....), and also through user_timeline.

I would like to know how to design better, and using better techniques. (for functionality, readability, compactness, and becoming a library)

Below are the code and usage example.


collection.py:

import tweepy
from tweepy import OAuthHandler
import numpy
import datetime



class Authentication(object):

    def __init__(self, consumer_key, consumer_secret, access_token, access_secret):

        self.consumer_key = consumer_key
        self.consunmer_secret = consumer_secret
        self.access_token = access_token
        self.access_secret = access_secret

        self.auth = OAuthHandler(consumer_key, consumer_secret)
        self.auth.set_access_token(access_token, access_secret)
        self.api = tweepy.API(self.auth)


class Collection(object):

    def __init__(self, auth_object):

        self.auth_object = auth_object
        self.api = self.auth_object.api
        self.collection = []

    def collect_home(self, method = 'Default', n = 20, **kwargs):

        if method == 'Default':

            public_stats = self.api.home_timeline()

        if method == 'Cursor':

            public_stats = tweepy.Cursor(self.api.home_timeline, **kwargs).items(n)

        self.stats = [stat for stat in public_stats]
        self.collection.extend(self.stats)

        date = datetime.datetime.now()
        time = date.timetuple()
        year = time.tm_year
        month = time.tm_mon
        day = time.tm_mday
        hour = time.tm_hour
        mnt = time.tm_min
        time_string = 'hour_min_{}_{}_date_{}_{}_{}'.format(\
            str(hour), str(mnt), str(day), str(month), str(year))
        self.time_collected = time_string;

        return self.stats, self.time_collected

    def collect_user(self, username, n = 20, **kwargs):

        user_stats = tweepy.Cursor(self.api.user_timeline, \
                                   screen_name = username, **kwargs).items(n)

        self.stats = [stat for stat in user_stats]
        self.collection.extend(self.stats)

        date = datetime.datetime.now()
        time = date.timetuple()
        year = time.tm_year
        month = time.tm_mon
        day = time.tm_mday
        hour = time.tm_hour
        mnt = time.tm_min
        time_string = 'hour_min_{}_{}_date_{}_{}_{}'.format(\
            str(hour), str(mnt), str(day), str(month), str(year))
        self.time_collected = time_string;

        return self.stats, self.time_collected

    def save(self, *args, **kwargs):
        numpy.save(*args, **kwargs)

Usage example :

consumer_key = '...............................'
consumer_secret = '...............................'
access_token = '...............................'
access_secret = '..............................'

Init = Authentication(consumer_key, consumer_secret, access_token, access_secret)
Collect = Collection(Init)

Collect.collect_user('Reuters', n = 50)
Collect.collect_user('anbarief', n = 50)

data = Collect.collection
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.