def Authenticate(): if not os.path.exists('TwitterCredentials.pkl'): Twitter = {} Twitter['Consumer Key'] = '' Twitter['Consumer Secret'] = '' Twitter['Access Token'] = '' Twitter['Access Token Secret'] = '' with open('TwitterCredentials.pkl', 'wb') as f: pickle.dump(Twitter, f) else: Twitter = pickle.load(open('TwitterCredentials.pkl', 'rb')) try: auth1 = OAuthHandler(Twitter['Consumer Key'], Twitter['Consumer Secret']) auth1.set_access_token(Twitter['Access Token'], Twitter['Access Token Secret']) auth1.apply_auth() api = tweepy.API(auth1, wait_on_rate_limit=True) except tweepy.TweepError: print 'Error in Logging in :)' return api
class Activity: _protocol: str = "https:/" _host: str = "api.twitter.com" _version: str = "1.1" _product: str = "account_activity" def __init__(self, credential: dict): ''' :param credential: dict that contains consumer_key, consumer_secret, \ access_token, access_token_secret, and env_name ''' self.env_name = credential['env_name'] self._auth = OAuthHandler(credential['consumer_key'], credential['consumer_secret']) self._auth.set_access_token(credential['access_token'], credential['access_token_secret']) def api(self, method: str, endpoint: str, data: dict = None) -> json: """ :param method: GET or POST :param endpoint: API Endpoint to be specified by user :param data: POST Request payload parameter :return: json """ try: with requests.Session() as r: response = r.request( url="/".join([ self._protocol, self._host, self._version, self._product, endpoint, ]), method=method, auth=self._auth.apply_auth(), data=data, ) return response except TweepError: raise def register_webhook(self, callback_url: str) -> json: try: return self.api( method="POST", endpoint=f"all/{self.env_name}/webhooks.json", data={ "url": callback_url }, ).json() except Exception as e: raise e def refresh(self, webhook_id: str) -> NoReturn: """Refreshes CRC for the provided webhook_id. """ try: return self.api( method="PUT", endpoint=f"all/{self.env_name}/webhooks/{webhook_id}.json", ) except Exception as e: raise e def delete(self, webhook_id: str) -> NoReturn: """Removes the webhook from the provided webhook_id. """ try: return self.api( method="DELETE", endpoint=f"all/{self.env_name}/webhooks/{webhook_id}.json", ) except Exception as e: raise e def subscribe(self) -> NoReturn: try: return self.api( method="POST", endpoint=f"all/{self.env_name}/subscriptions.json", ) except Exception: raise def webhooks(self) -> json: """Returns all environments, webhook URLs and their statuses for the authenticating app. Only one webhook URL can be registered to each environment. """ try: return self.api(method="GET", endpoint=f"all/webhooks.json").json() except Exception as e: raise e
#!/usr/bin/env python2.7 #! -*- coding:utf-8 -*- from tweepy.streaming import StreamListener from tweepy import API, OAuthHandler, Stream import commands import platform consumer_key = "" consumer_secret = "" access_token = "" access_token_secret = "" auth = OAuthHandler(consumer_key, consumer_secret) auth.access_token = access_token auth.access_token_secret = access_token_secret auth.apply_auth() api = API(auth) class Commands(StreamListener): def post_on_twitter(self, data): for i in range(0, int(len(data) / 140) + 1): if i == 0: first = 140 * i last = 140 - (140 * i) else: first = (140 * (i + 1)) - 140 last = 140 * (i + 1) status = data[first:last] if status: api.update_status(status=status)