Skip to content

Commit 8d64a6c

Browse files
committed
Add check_accounts_in method
1 parent 9d42ff7 commit 8d64a6c

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ twitter_app_auth = {
2323
}
2424
bon = botornot.BotOrNot(**twitter_app_auth)
2525

26-
bon.check_account('@clayadavis')
26+
# Check a single account
27+
result = bon.check_account('@clayadavis')
28+
29+
# Check a sequence of accounts
30+
accounts = ['@clayadavis', '@onurvarol', '@jabawack']
31+
results = list(bon.check_accounts(accounts))
2732
```
2833

2934
Result:

botornot/__init__.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import tweepy
44
import requests
55
from functools import wraps
6-
from tweepy import RateLimitError
6+
from tweepy import RateLimitError, TweepError
7+
from requests import ConnectionError, Timeout
78

89

910
class NoTimelineError(ValueError):
@@ -20,7 +21,7 @@ def __init__(self, consumer_key, consumer_secret,
2021
access_token, access_token_secret, **kwargs):
2122
self.consumer_key = consumer_key
2223
self.consumer_secret = consumer_secret
23-
self.access_token_key = access_token
24+
self.access_token_key = self.access_token = access_token
2425
self.access_token_secret = access_token_secret
2526
self.wait_on_ratelimit = kwargs.get('wait_on_ratelimit', False)
2627

@@ -59,6 +60,13 @@ def wrapper(*args, **kwargs):
5960
self._bon_post = _rate_limited(requests.post)
6061

6162

63+
@classmethod
64+
def create_from(cls, instance, **kwargs):
65+
my_kwargs = vars(instance)
66+
my_kwargs.update(kwargs)
67+
return cls(**my_kwargs)
68+
69+
6270
@property
6371
def bon_api_path(self, method=''):
6472
return '%s/%s' % (self.botornot_api.rstrip('/'), self.api_version)
@@ -103,6 +111,9 @@ def _check_account(self, user_data, tweets):
103111
return bon_resp.json()
104112

105113

114+
####################
115+
## Public methods ##
116+
####################
106117

107118

108119
def check_account(self, user):
@@ -112,3 +123,34 @@ def check_account(self, user):
112123
classification = self._check_account(user_data, tweets)
113124

114125
return classification
126+
127+
128+
def check_accounts_in(self, accounts, **kwargs):
129+
#sub_instance_kwargs = vars(self)
130+
#sub_instance_kwargs['wait_on_ratelimit'] = True
131+
sub_instance = self.create_from(self, wait_on_ratelimit=True)
132+
# BotOrNot(**sub_instance_kwargs)
133+
134+
max_retries = kwargs.get('retries', 3)
135+
num_retries = 0
136+
137+
for account in accounts:
138+
for num_retries in range(max_retries + 1):
139+
result = None
140+
try:
141+
result = sub_instance.check_account(account)
142+
except (TweepError, NoTimelineError) as e:
143+
err_msg = '{}: {}'.format(
144+
type(e).__name__,
145+
getattr(e, 'msg', '') or getattr(e, 'reason', ''),
146+
)
147+
result = {'error': err_msg}
148+
except (Timeout, ConnectionError) as e:
149+
if num_retries >= max_retries:
150+
raise
151+
else:
152+
time.sleep(2 ** num_retries)
153+
154+
if result is not None:
155+
yield account, result
156+
break

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from setuptools import setup
22

33
setup(name='botornot',
4-
version='0.2',
4+
version='0.3-alpha',
55
description='Check Twitter accounts for bot behavior',
66
url='https://github.com/truthy/botornot-python',
7-
download_url='https://github.com/truthy/botornot-python/archive/0.2.zip',
7+
download_url='https://github.com/truthy/botornot-python/archive/0.3.zip',
88
author='Clayton A Davis',
99
author_email='[email protected]',
1010
license='MIT',

0 commit comments

Comments
 (0)