collecting.py module for working with Tweepy

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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
python object-oriented twitter
add a comment |Â
up vote
2
down vote
favorite
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
python object-oriented twitter
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
python object-oriented twitter
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
python object-oriented twitter
edited Mar 24 at 15:00
200_success
123k14142399
123k14142399
asked Mar 24 at 14:48
Arief
420112
420112
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190372%2fcollecting-py-module-for-working-with-tweepy%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password