Skip to content

Commit

Permalink
v0.0.9 fixed credentials & minor changes
Browse files Browse the repository at this point in the history
- fixed credentials properties as described on #3 so to minimize arguments on twipper functions
- add missing docstrings
- renamed properties
  • Loading branch information
Álvaro Bartolomé del Canto committed Aug 12, 2019
1 parent 2f8403e commit 90dafd9
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 64 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def readme():

setup(
name='twipper',
version='0.0.8',
version='0.0.9',
packages=find_packages(),
url='https://github.com/alvarob96/twipper',
download_url='https://github.com/alvarob96/twipper/archive/0.0.8.tar.gz',
download_url='https://github.com/alvarob96/twipper/archive/0.0.9.tar.gz',
license='GNU General Public License v3 (GPLv3)',
author='Alvaro Bartolome',
author_email='alvarob96@usal.es',
Expand Down
2 changes: 1 addition & 1 deletion twipper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# See LICENSE for details.

__author__ = 'Alvaro Bartolome @ alvarob96 on GitHub'
__version__ = '0.0.8'
__version__ = '0.0.9'
29 changes: 17 additions & 12 deletions twipper/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import json
import oauth2
from twipper.credentials import Twipper

from twipper.utils import available_languages


def search_tweets(api, query, page_count=1, filter_retweets=False,
def search_tweets(access, query, page_count=1, filter_retweets=False,
language=None, result_type='mixed', count=100):
"""
This function retrieves historical tweets on batch processing. These tweets contain the specified words on the
Expand All @@ -21,7 +22,7 @@ def search_tweets(api, query, page_count=1, filter_retweets=False,
API Reference: https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
Args:
api (:obj:`oauth2.Client`): valid Twitter API generated via `twipper.credentials.Twipper`.
access (:obj:`twipper.credentials.Twipper`): object containing all the credentials needed to access api.twitter
query (:obj:`str`): contains the query with the words to search along Twitter historic data.
page_count (:obj:`int`, optional):
specifies the amount of pages (100 tweets per page) to retrieve data from, default value is 5.
Expand All @@ -41,11 +42,13 @@ def search_tweets(api, query, page_count=1, filter_retweets=False,
ValueError: raised if the introduced arguments do not match or errored.
"""

if not isinstance(api, oauth2.Client):
raise ValueError('specified api is not valid!')
if not access or not isinstance(access, Twipper):
raise ValueError('access object to api.twitter is not valid!')

api = access.api

if api is None:
raise ValueError('api is mandatory')
if not isinstance(api, oauth2.Client):
raise ValueError('api is not valid!')

if not isinstance(query, str):
raise ValueError('query must be a string!')
Expand Down Expand Up @@ -160,7 +163,7 @@ def search_tweets(api, query, page_count=1, filter_retweets=False,
raise IndexError('no tweets could be retrieved.')


def search_user_tweets(api, screen_name, page_count=1, filter_retweets=False,
def search_user_tweets(access, screen_name, page_count=1, filter_retweets=False,
language=None, result_type='mixed', count=100):
"""
This function retrieves historical tweets from a Twitter user by their screen_name (@), whenever they grant the
Expand All @@ -169,7 +172,7 @@ def search_user_tweets(api, screen_name, page_count=1, filter_retweets=False,
API Reference: https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
Args:
api (:obj:`oauth2.Client`): valid Twitter API generated via `twipper.credentials.Twipper`.
access (:obj:`twipper.credentials.Twipper`): object containing all the credentials needed to access api.twitter
screen_name (:obj:`str`): contains the username of the user from which tweets are going to be retrieved.
page_count (:obj:`int`, optional):
specifies the amount of pages (100 tweets per page) to retrieve data from, default value is 5.
Expand All @@ -189,11 +192,13 @@ def search_user_tweets(api, screen_name, page_count=1, filter_retweets=False,
ValueError: raised if the introduced arguments do not match or errored.
"""

if not isinstance(api, oauth2.Client):
raise ValueError('specified api is not valid!')
if not access or not isinstance(access, Twipper):
raise ValueError('access object to api.twitter is not valid!')

api = access.api

if api is None:
raise ValueError('api is mandatory')
if not isinstance(api, oauth2.Client):
raise ValueError('api is not valid!')

if not isinstance(screen_name, str):
raise ValueError('screen_name must be a string!')
Expand Down
41 changes: 34 additions & 7 deletions twipper/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,36 @@ def __init__(self, consumer_key, consumer_secret, access_token, access_token_sec
self.access_token = access_token
self.access_token_secret = access_token_secret

self.api = self.__get_api()
self.oauth = self.__get_oauth()
self.oauth_token = self.__get_oauth_token()

def get_api(self):
self.plan = ''
self.label = ''

@property
def plan(self):
return self._plan

@plan.setter
def plan(self, premium_plan):
if isinstance(premium_plan, str):
self._plan = premium_plan
else:
raise Exception('invalid value for premium plan.')

@property
def label(self):
return self._label

@label.setter
def label(self, premium_label):
if isinstance(premium_label, str):
self._label = premium_label
else:
raise Exception('invalid value for dev environment label.')

def __get_api(self):
"""
This function validates the credentials of the Twitter API, by validating the token with oauth2 as it is the
default authentication method implemented by the Twitter API (even though oauth1 authentication can be used). So
Expand Down Expand Up @@ -74,20 +101,20 @@ def get_api(self):
print(e) # Invalid client
return None

def get_oauth(self):
def __get_oauth(self):
"""
This function gets the `requests_oauthlib.OAuth1` authentication object with the OAuth1 method in order to
generate the `auth` object that will be sent inside a request header.
Returns:
:obj:`requests_oauthlib.OAuth1` - auth:
:obj:`requests_oauthlib.OAuth1` - oauth:
Returns the oauth1 object containing the credentials to access Twitter API endpoints for Streaming.
"""

auth = OAuth1(self.consumer_key, self.consumer_secret,
self.access_token, self.access_token_secret)
oauth = OAuth1(self.consumer_key, self.consumer_secret,
self.access_token, self.access_token_secret)

return auth
return oauth

def __get_oauth_token(self):
"""
Expand Down Expand Up @@ -125,7 +152,7 @@ def __get_oauth_token(self):
else:
return None

def invalidate_oauth_token(self):
def close(self):
"""
This function invalidates the specified access_token as Twitter API generates one access_token per application,
so whenever the current access_token is not needed anymore it should be invalidated in order to be able to
Expand Down
53 changes: 34 additions & 19 deletions twipper/premium.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import oauth2
import requests
from twipper.credentials import Twipper

from twipper.utils import available_languages


def search_tweets(api, oauth_token, plan, label, query, page_count,
from_date, to_date, language=None):
def search_tweets(access, query, page_count, from_date, to_date, language=None):
"""
This function retrieves historical tweets on batch processing from Twitter's Full Archive or 30Day. These tweets
contain the specified words on the query, which can use premium operators as specified on
Expand All @@ -23,10 +23,7 @@ def search_tweets(api, oauth_token, plan, label, query, page_count,
it is filled with the retrieved tweets.
Args:
api (:obj:`oauth2.Client`): valid Twitter API generated via `twipper.credentials.Twipper`.
oauth_token (:obj:`str`): valid access_token for the authentication of every Twitter Premium API request.
plan (:obj:`str`): paid plan of twitter premium api, it can either be `30day` or `fullarchive`.
label (:obj:`str`): label that has been set up for the development environment of the Twitter Premium API.
access (:obj:`twipper.credentials.Twipper`): object containing all the credentials needed to access api.twitter
query (:obj:`str`): contains the query with the words to search along Twitter historic data.
page_count (:obj:`int`): specifies the amount of pages (100 tweets per page) to retrieve data from.
from_date (:obj:`str`): starting date of the time interval to retrieve tweets from (`yyyymmddhhmm` format)
Expand All @@ -42,15 +39,26 @@ def search_tweets(api, oauth_token, plan, label, query, page_count,
ValueError: raised if the introduced arguments do not match or errored.
"""

if not api or not isinstance(api, oauth2.Client):
raise ValueError('specified api is not valid!')
if not access or not isinstance(access, Twipper):
raise ValueError('access object to api.twitter is not valid!')

if not oauth_token or not isinstance(oauth_token, str):
api = access.api

if not isinstance(api, oauth2.Client):
raise ValueError('api is not valid!')

oauth_token = access.oauth_token

if not isinstance(oauth_token, str):
raise ValueError('oauth_token is not valid!')

plan = access.plan

if not plan or not isinstance(plan, str):
raise ValueError('plan must be a `str`!')

label = access.label

if not label or not isinstance(label, str):
raise ValueError('label must be a `str`!')

Expand Down Expand Up @@ -169,8 +177,7 @@ def search_tweets(api, oauth_token, plan, label, query, page_count,
raise IndexError('no tweets could be retrieved.')


def wrapper_premium_user_search(api, oauth_token, plan, label, screen_name,
page_count, from_date, to_date, language=None):
def wrapper_premium_user_search(access, screen_name, page_count, from_date, to_date, language=None):
"""
This function retrieves historical tweets on batch processing from Twitter's Full Archive or 30Day from a specific
user via its screen name (Twitter name). These tweets contain the specified words on the query, which can use
Expand All @@ -181,10 +188,7 @@ def wrapper_premium_user_search(api, oauth_token, plan, label, screen_name,
it is filled with the retrieved tweets.
Args:
api (:obj:`oauth2.Client`): valid Twitter API generated via `twipper.credentials.Twipper`.
oauth_token (:obj:`str`): valid access_token for the authentication of every Twitter Premium API request.
plan (:obj:`str`): paid plan of twitter premium api, it can either be `30day` or `fullarchive`.
label (:obj:`str`): label that has been set up for the development environment of the Twitter Premium API.
access (:obj:`twipper.credentials.Twipper`): object containing all the credentials needed to access api.twitter
screen_name (:obj:`str`):
is the Twitter's public name of the account that tweets are going to be retrieved, note that the account
must be public.
Expand All @@ -202,15 +206,26 @@ def wrapper_premium_user_search(api, oauth_token, plan, label, screen_name,
ValueError: raised if the introduced arguments do not match or errored.
"""

if not api or not isinstance(api, oauth2.Client):
raise ValueError('specified api is not valid!')
if not access or not isinstance(access, Twipper):
raise ValueError('access object to api.twitter is not valid!')

api = access.api

if not oauth_token or not isinstance(oauth_token, str):
if not isinstance(api, oauth2.Client):
raise ValueError('api is not valid!')

oauth_token = access.oauth_token

if not isinstance(oauth_token, str):
raise ValueError('oauth_token is not valid!')

plan = access.plan

if not plan or not isinstance(plan, str):
raise ValueError('plan must be a `str`!')

label = access.label

if not label or not isinstance(label, str):
raise ValueError('label must be a `str`!')

Expand Down Expand Up @@ -328,4 +343,4 @@ def wrapper_premium_user_search(api, oauth_token, plan, label, screen_name,
if len(tweets) > 0:
return tweets
else:
raise IndexError('no tweets could be retrieved.')
raise IndexError('no tweets could be retrieved.')
49 changes: 27 additions & 22 deletions twipper/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import requests_oauthlib

from twipper.utils import available_languages, country_to_bounding_box
from twipper.credentials import Twipper


def stream_tweets(api, auth, query, language=None, filter_retweets=False,
def stream_tweets(access, query, language=None, filter_retweets=False,
tweet_limit=None, date_limit=None, retry=5):
"""
This function retrieves streaming tweets matching the given query, so on, this function will open a stream to
Expand All @@ -23,9 +24,7 @@ def stream_tweets(api, auth, query, language=None, filter_retweets=False,
API Reference: https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/basic-stream-parameters.html
Args:
api (:obj:`oauth2.Client`): valid Twitter API generated via `twipper.credentials.Twipper`.
auth (:obj:`requests_oauthlib.OAuth1`):
valid Twitter Auth object generated via `twipper.credentials.Twipper`.
access (:obj:`twipper.credentials.Twipper`): object containing all the credentials needed to access api.twitter
query (:obj:`str`): contains the query with the words to search along the Twitter Streaming.
language (:obj:`str`, optional): is the language on which the tweet has been written, default is `None`.
filter_retweets (:obj:`boolean`, optional):
Expand All @@ -48,14 +47,18 @@ def stream_tweets(api, auth, query, language=None, filter_retweets=False,
ValueError: raised if the introduced arguments do not match or errored.
"""

if not api or not isinstance(api, oauth2.Client):
raise ValueError('specified api is not valid!')
if not access or not isinstance(access, Twipper):
raise ValueError('access object to api.twitter is not valid!')

if not isinstance(auth, requests_oauthlib.OAuth1):
raise ValueError('specified auth is not valid!')
api = access.api

if auth is None:
raise ValueError('auth is mandatory')
if not isinstance(api, oauth2.Client):
raise ValueError('api is not valid!')

oauth = access.oauth

if not isinstance(oauth, requests_oauthlib.OAuth1):
raise ValueError('auth is not valid!')

if not isinstance(query, str):
raise ValueError('query must be a string!')
Expand Down Expand Up @@ -117,7 +120,7 @@ def stream_tweets(api, auth, query, language=None, filter_retweets=False,
'Content-Type': 'application/json',
}

response = requests.post(url, auth=auth, headers=headers, params=params, stream=True)
response = requests.post(url, auth=oauth, headers=headers, params=params, stream=True)

if response.status_code != 200:
raise ConnectionError('connection errored with code ' + str(response.status_code) + '.')
Expand Down Expand Up @@ -199,7 +202,7 @@ def stream_tweets(api, auth, query, language=None, filter_retweets=False,
tweet_counter += 1


def stream_country_tweets(api, auth, country, language=None, filter_retweets=False,
def stream_country_tweets(access, country, language=None, filter_retweets=False,
tweet_limit=None, date_limit=None, retry=5):
"""
This function retrieves streaming tweets matching the given query, so on, this function will open a stream to
Expand All @@ -208,9 +211,7 @@ def stream_country_tweets(api, auth, country, language=None, filter_retweets=Fal
API Reference: https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/basic-stream-parameters.html
Args:
api (:obj:`oauth2.Client`): valid Twitter API generated via `twipper.credentials.Twipper`.
auth (:obj:`requests_oauthlib.OAuth1`):
valid Twitter Auth object generated via `twipper.credentials.Twipper`.
access (:obj:`twipper.credentials.Twipper`): object containing all the credentials needed to access api.twitter
country (:obj:`str`): contains the country name from where generic tweets will be retrieved.
language (:obj:`str`, optional): is the language on which the tweet has been written, default is `None`.
filter_retweets (:obj:`boolean`, optional):
Expand All @@ -233,14 +234,18 @@ def stream_country_tweets(api, auth, country, language=None, filter_retweets=Fal
ValueError: raised if the introduced arguments do not match or errored.
"""

if not api or not isinstance(api, oauth2.Client):
raise ValueError('specified api is not valid!')
if not access or not isinstance(access, Twipper):
raise ValueError('access object to api.twitter is not valid!')

api = access.api

if not isinstance(api, oauth2.Client):
raise ValueError('api is not valid!')

if not isinstance(auth, requests_oauthlib.OAuth1):
raise ValueError('specified auth is not valid!')
oauth = access.oauth

if auth is None:
raise ValueError('auth is mandatory')
if not isinstance(oauth, requests_oauthlib.OAuth1):
raise ValueError('auth is not valid!')

if not isinstance(country, str):
raise ValueError('query must be a string!')
Expand Down Expand Up @@ -301,7 +306,7 @@ def stream_country_tweets(api, auth, country, language=None, filter_retweets=Fal
'Content-Type': 'application/json',
}

response = requests.post(url, auth=auth, headers=headers, params=params, stream=True)
response = requests.post(url, auth=oauth, headers=headers, params=params, stream=True)

if response.status_code != 200:
raise ConnectionError('connection errored with code ' + str(response.status_code))
Expand Down

0 comments on commit 90dafd9

Please sign in to comment.