Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
continue
candidate = db.get_candidate(slug)
if 'twitter_handle' not in candidate:
timestamp = datetime.now().timestamp()
db.saveToImportLog(IMPORT_KEY, {'candidates_wo_twitter': {candidate['slug']: timestamp}})
print(candidate["slug"] + " is missing a twitter handle")
continue
twitter_handle = candidate['twitter_handle']
if args.both:
save_tweets_with_retweets(twitter_handle)
save_followers_with_botness(twitter_handle)
elif args.tweets:
save_tweets_with_retweets(twitter_handle)
elif args.followers:
save_followers_with_botness(twitter_handle)
except tweepy.error.TweepError as ex:
timestamp = datetime.now().timestamp()
db.saveToImportLog(IMPORT_KEY, {'candidates_w_exception': {slug: {'at': timestamp, 'msg': str(ex)}}})
print(slug + " raised exception: " + str(ex))
try:
user_status = api.get_user(id=tweep.uid)
if user_status['screen_name'] != tweep.name:
print ' -- updating screen name to %s' % \
user_status['screen_name']
former_names = tweep.former_names
if not tweep.former_names:
former_names = '{}'
oldnames = json.loads(former_names)
oldnames[datetime.datetime.utcnow().strftime(
'%Y-%m-%dT%H:%M:%SZ')] = tweep.name
tweep.former_names = json.dumps(oldnames)
tweep.name = user_status['screen_name']
#TODO: Is this save unnecessary, since it gets saved below?
tweep.save()
except tweepy.error.TweepError as e:
print 'Error: %s' % e
#go to the next tweep in the for loop
continue
finally:
time.sleep(set_wait_time(api.last_response))
def _get_request_token(self, access_type=None):
try:
url = self._get_oauth_url('request_token')
if access_type:
url += '?x_auth_access_type=%s' % access_type
return self.oauth.fetch_request_token(url)
except Exception as e:
raise TweepError(e)
def filter(self, follow=None, track=None, async=False):
params = {}
self.headers['Content-type'] = "application/x-www-form-urlencoded"
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/%i/statuses/filter.json?delimited=length' % STREAM_VERSION
if follow:
params['follow'] = ','.join(map(str, follow))
if track:
params['track'] = ','.join(map(str, track))
self.body = urllib.urlencode(params)
self._start(async)
def build_parameters(self, args, kargs):
self.parameters = {}
for idx, arg in enumerate(args):
if arg is None:
continue
try:
self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
except IndexError:
raise TweepError('Too many parameters supplied!')
for k, arg in kargs.items():
if arg is None:
continue
if k in self.parameters:
raise TweepError('Multiple values for parameter %s supplied!' % k)
self.parameters[k] = convert_to_utf8_str(arg)
def build_parameters(self, args, kargs):
self.parameters = {}
for idx, arg in enumerate(args):
if arg is None:
continue
try:
self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
except IndexError:
raise TweepError('Too many parameters supplied!')
for k, arg in kargs.items():
if arg is None:
continue
if k in self.parameters:
raise TweepError('Multiple values for parameter %s supplied!' % k)
self.parameters[k] = convert_to_utf8_str(arg)
raise TweepError('Unable to access file: %s' % e.strerror)
# build the mulitpart-formdata body
fp = open(filename, 'rb')
else:
f.seek(0, 2) # Seek to end of file
if f.tell() > (max_size * 1024):
raise TweepError('File is too big, must be less than %skb.'
% max_size)
f.seek(0) # Reset to beginning of file
fp = f
# image must be gif, jpeg, or png
file_type = mimetypes.guess_type(filename)
if file_type is None:
raise TweepError('Could not determine file type')
file_type = file_type[0]
if file_type not in ['image/gif', 'image/jpeg', 'image/png']:
raise TweepError('Invalid file type for image: %s' % file_type)
if isinstance(filename, six.text_type):
filename = filename.encode('utf-8')
BOUNDARY = b'Tw3ePy'
body = []
body.append(b'--' + BOUNDARY)
body.append('Content-Disposition: form-data; name="{0}";'
' filename="{1}"'.format(form_field, filename)
.encode('utf-8'))
body.append('Content-Type: {0}'.format(file_type).encode('utf-8'))
body.append(b'')
body.append(fp.read())
url = self._get_oauth_url('access_token')
# build request
request = oauth.OAuthRequest.from_consumer_and_token(
self._consumer,
token=self.request_token, http_url=url,
verifier=str(verifier)
)
request.sign_request(self._sigmethod, self._consumer, self.request_token)
# send request
resp = urlopen(Request(url, headers=request.to_header()))
self.access_token = oauth.OAuthToken.from_string(resp.read())
return self.access_token
except Exception, e:
raise TweepError(e)
if self.api.auth:
self.api.auth.apply_auth(
self.scheme + self.host + url,
self.method, self.headers, self.parameters
)
# Request compression if configured
if self.api.compression:
self.headers['Accept-encoding'] = 'gzip'
# Execute request
try:
conn.request(self.method, url, headers=self.headers, body=self.post_data)
resp = conn.getresponse()
except Exception, e:
raise TweepError('Failed to send request: %s' % e)
# Exit request loop if non-retry error code
if self.retry_errors:
if resp.status not in self.retry_errors: break
else:
if resp.status == 200: break
# Sleep before retrying request again
time.sleep(self.retry_delay)
retries_performed += 1
# If an error was returned, throw an exception
self.api.last_response = resp
if resp.status and not 200 <= resp.status < 300:
try:
error_msg = self.api.parser.parse_error(resp.read())
def build_parameters(self, args, kargs):
self.parameters = {}
for idx, arg in enumerate(args):
if isinstance(arg, unicode):
arg = arg.encode('utf-8')
elif not isinstance(arg, str):
arg = str(arg)
try:
self.parameters[self.allowed_param[idx]] = arg
except IndexError:
raise TweepError('Too many parameters supplied!')
for k, arg in kargs.items():
if arg is None:
continue
if k in self.parameters:
raise TweepError('Multiple values for parameter %s supplied!' % k)
if isinstance(arg, unicode):
arg = arg.encode('utf-8')
elif not isinstance(arg, str):
arg = str(arg)
self.parameters[k] = arg