def __init__(self, id, type='basic'): self.token = None self.cookie_redirect = None self.cookie = None self.type = type self.oauth = None self.trust = None # Proposed response code after checkAuthentication() or authorise() have been called self.response = { 'code': 403, # Result code (http) 'text': "Forbidden", # Proposed response text 'headers': [], # Headers to add to response after authentication has been done } # Whether authentication is complete or not (depends on flow) self.authn_done = False # acl stores the actual verified credentials and access rights after # authentication and authorisation have been done self.acl = { "authenticated": False, # Has authentication been verified and passed? "authorised": False, # Has authorisation been done and appropriate acls set? "rights": '', # "a", "r" (approve or reject) "relationship": None, # E.g. creator, friend, admin, etc "peerid": '', # Peerid if there is a relationship "approved": False, # True if the peer is approved } Config = config.config() self.config = Config self.actor = actor.actor(id) if not self.actor.id: self.actor = None self.oauth = oauth.oauth(token=None) self.token = None self.expiry = None self.refresh_expiry = None self.refresh_token = None return # We need to initialise oauth for use towards the external oauth service self.property = 'oauth_token' # Property name used to set self.token self.token = self.actor.getProperty(self.property).value self.oauth = oauth.oauth(token=self.token) self.expiry = self.actor.getProperty('oauth_token_expiry').value self.refresh_expiry = self.actor.getProperty('oauth_refresh_token_expiry').value self.refresh_token = self.actor.getProperty('oauth_refresh_token').value if self.type == 'basic': self.realm = Config.auth_realm elif self.type == 'oauth': if self.oauth.enabled(): self.cookie = 'oauth_token' if self.actor.getProperty('cookie_redirect').value: self.cookie_redirect = Config.root + \ self.actor.getProperty('cookie_redirect').value else: self.cookie_redirect = None self.redirect = Config.root + self.actor.id + '/oauth' else: self.type = 'none'
def get(self): query = self.get_argument('q') client = tornado.httpclient.AsyncHTTPClient() url = "https://api.twitter.com/1.1/search/tweets.json?" + \ urllib.urlencode({"q": query, "result_type": "recent", "count": 100}) # here an object of tornado.gen.Task is created to which the function # to be called and the parameters are sent. # here yield gives the control back to Tornado so that it can perform # other tasks or receive new request while the HTTP request is still # in progress # When the HTTP request is finished, the RequestHandler method # resumes where it left off # There is no performance difference using tornado.gen, it is done for # readability of the code response = yield tornado.gen.Task(client.fetch, url, headers={'Authorization': oauth(url)}) body = json.loads(response.body) result_count = len(body['statuses']) now = datetime.datetime.utcnow() raw_oldest_tweet_at = body['statuses'][-1]['created_at'] oldest_tweet_at = datetime.datetime.strptime(raw_oldest_tweet_at, "%a %b %d %H:%M:%S +0000 %Y") seconds_diff = time.mktime(now.timetuple()) - \ time.mktime(oldest_tweet_at.timetuple()) tweets_per_second = float(result_count) / seconds_diff self.write(""" <div style="text-align: center"> <div style="font-size: 72px">%s</div> <div style="font-size: 144px">%.02f</div> <div style="font-size: 24px">tweets per second</div> </div>""" % (query, tweets_per_second)) self.finish()
def get(self): # localhost:8000/?q=stuff # so here self.get_arugement gives the value of q i.e. 'stuff' query = self.get_argument('q') # Using tornado's HTTPClient class, an object is instantiated # and fetch method is called on that client = tornado.httpclient.HTTPClient() # urllib.urlencode formats it to proper url format %xx something like # that. and response of client.fetch is stored in response #response = client.fetch("http://search.twitter.com/search.json?" + \ # urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100})) url = "https://api.twitter.com/1.1/search/tweets.json?" + urllib.urlencode({"q": query, "result_type": "recent", "count": 100}) response = client.fetch(url, headers={'Authorization': oauth(url)}) # the response has header and body. As body contains the results, it # is used. And it is converted to python object from json # using json.loads body = json.loads(response.body) # result_count = len(body['results']) result_count = len(body['statuses']) now = datetime.datetime.utcnow() #raw_oldest_tweet_at = body['results'][-1]['created_at'] raw_oldest_tweet_at = body['statuses'][-1]['created_at'] oldest_tweet_at = datetime.datetime.strptime(raw_oldest_tweet_at, # "%a, %d %b %Y %H:%M:%S +0000") "%a %b %d %H:%M:%S +0000 %Y") seconds_diff = time.mktime(now.timetuple()) - \ time.mktime(oldest_tweet_at.timetuple()) tweets_per_second = float(result_count) / seconds_diff self.write(""" <div style="text-align: center"> <div style="font-size: 72px">%s</div> <div style="font-size: 144px">%.02f</div> <div style="font-size: 24px">tweets per second</div> </div>""" % (query, tweets_per_second))
def get(self): query = self.get_argument('q') client = tornado.httpclient.HTTPClient() # 实例化一个httpclient类, url = "https://api.twitter.com/1.1/search/tweets.json?" + \ urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}) response = client.fetch(url, headers={'Authorization': oauth(url)}) # 调用结果对象的fetch方法使用要获取的url作为参数,rpp参数指定我们想获取搜索结果首页的100个推文, # 而result_type参数指定我么你想获取匹配搜索的最近推文,fetch方法会返回一个HTTPResponse对象,其中body为从远端URL获取的任何数据, # Twitter将返回一个json格式的结果,所以我们可以使用python的json模块来从结果中创建一个python数据结构. body = json.loads(response.body) result_count = len(body["statuses"]) now = datetime.datetime.utcnow() raw_oldest_tweet_at = body['statuses'][-1]['created_at'] oldest_tweet_at = datetime.datetime.strptime(raw_oldest_tweet_at, "%a %b %d %H:%M:%S +0000 %Y") second_diff = time.mktime(now.timetuple()) - \ time.mktime(oldest_tweet_at.timetuple()) tweet_per_second = float(result_count) / second_diff self.write(""" <div style="text-align":center"> <div style="font-size: 72px">%s</div> <div style="font-size: 144px">%.02f</div> <div style="font-size: 244px">tweets per second<div> </div> """ % (query, tweet_per_second,))
def __init__(self, ckey, csecret, atoken, asecret, oauth_obj=None): if oauth_obj == None: self.oauth = oauth.oauth(ckey, csecret, atoken, asecret) else: self.oauth = oauth_obj req = self.oauth.oauth_request(self.url["account"]["verify_credentials"]) getxml = urllib2.urlopen(req).read() user = twitterxml.xmlparse(getxml) self.user = user
def testInitParamDefault(self): """ __init__のデフォルトパラメータを使用した時の初期化が、成功しているかを確認。 """ oa = oauth( oauth_consumer_key="UbYsYTD9LgiHZjNud0aLKg", oauth_consumer_secret="jDSsu9dLjrNINkNwepepBHiBHerKTRDJ8xKFSBJ15DQ", ) eq_(oa.oauth_params["oauth_consumer_key"], "UbYsYTD9LgiHZjNud0aLKg") eq_(oa.oauth_params["oauth_consumer_secret"], "jDSsu9dLjrNINkNwepepBHiBHerKTRDJ8xKFSBJ15DQ") ok_(oa.oauth_params["oauth_timestamp"]) ok_(oa.oauth_params["oauth_nonce"])
def get(self): query = self.get_argument('q') # here AsynHTTPClient is used to to handle requests asynchrnously # and a call back function is needed which will be called when the # response is returned. # in this case, when twitter returns the response, the callback # is called. # compared to earlier the code is now split in two functions to # handle the requests. client = tornado.httpclient.AsyncHTTPClient() url = "https://api.twitter.com/1.1/search/tweets.json?" + \ urllib.urlencode({"q": query, "result_type": "recent", "count": 100}) client.fetch(url, headers={'Authorization': oauth(url)}, callback=self.on_response)
def setUp(self): """ twitterのOAuth関連URL """ self.sample_urls = { "request_token_url": "https://api.twitter.com/oauth/request_token", "access_token_url": "https://api.twitter.com/oauth/access_token", "authorize_token_url": "https://api.twitter.com/oauth/authorize", } """ サンプルのパラメータ 以下docテストを含む全てのテストは、この値に依存している。 """ self.sample_params = { "oauth_consumer_key": "UbYsYTD9LgiHZjNud0aLKg", "oauth_consumer_secret": "jDSsu9dLjrNINkNwepepBHiBHerKTRDJ8xKFSBJ15DQ", "oauth_timestamp": "1274589200", "oauth_nonce": "290086721772933738", "oauth_signature_method": "HMAC-SHA1", "oauth_version": "1.0", } self.sample_params_current = { "oauth_consumer_key": "UbYsYTD9LgiHZjNud0aLKg", "oauth_consumer_secret": "jDSsu9dLjrNINkNwepepBHiBHerKTRDJ8xKFSBJ15DQ", } self.sample_params_current2 = { "oauth_consumer_key": "YpNDjDwvaL65ShPCPXgDA", "oauth_consumer_secret": "dHdKfmtIqE5tKMgo0vhVe08ACfv2COPzcg89q2GP0Y", } """ サンプルのパラメータを使用し、dictのアンパックで初期化。 各テストでこのオブジェクトを使用する。 """ self.oa = oauth(**self.sample_params) self.oa_c = oauth(**self.sample_params_current)
def __init__(self, ckey, csecret, atoken, asecret, screen_name = "", oauth_obj = None): # Oauth init if oauth_obj == None: self.oauth = oauth.oauth(ckey, csecret, atoken, asecret) else: self.oauth = oauth_obj self.user = { "screen_name" : screen_name } # ratelimit var init self.ratelimit_limit = -1 self.ratelimit_remaining = -1 self.ratelimit_reset = datetime.datetime.now() self.ratelimit_iplimit = -1 self.ratelimit_ipremaining = -1 self.ratelimit_ipreset = datetime.datetime.now()
def get(self): # localhost:8000/?q=stuff # so here self.get_arugement gives the value of q i.e. 'stuff' query = self.get_argument('q') # Using tornado's HTTPClient class, an object is instantiated # and fetch method is called on that client = tornado.httpclient.HTTPClient() # urllib.urlencode formats it to proper url format %xx something like # that. and response of client.fetch is stored in response #response = client.fetch("http://search.twitter.com/search.json?" + \ # urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100})) url = "https://api.twitter.com/1.1/search/tweets.json?" + urllib.urlencode( { "q": query, "result_type": "recent", "count": 100 }) response = client.fetch(url, headers={'Authorization': oauth(url)}) # the response has header and body. As body contains the results, it # is used. And it is converted to python object from json # using json.loads body = json.loads(response.body) # result_count = len(body['results']) result_count = len(body['statuses']) now = datetime.datetime.utcnow() #raw_oldest_tweet_at = body['results'][-1]['created_at'] raw_oldest_tweet_at = body['statuses'][-1]['created_at'] oldest_tweet_at = datetime.datetime.strptime( raw_oldest_tweet_at, # "%a, %d %b %Y %H:%M:%S +0000") "%a %b %d %H:%M:%S +0000 %Y") seconds_diff = time.mktime(now.timetuple()) - \ time.mktime(oldest_tweet_at.timetuple()) tweets_per_second = float(result_count) / seconds_diff self.write(""" <div style="text-align: center"> <div style="font-size: 72px">%s</div> <div style="font-size: 144px">%.02f</div> <div style="font-size: 24px">tweets per second</div> </div>""" % (query, tweets_per_second))
def get(self): query = self.get_argument('q') client = tornado.httpclient.HTTPClient() url = "https://api.twitter.com/1.1/search/tweets.json?" + \ urllib.urlencode({"q": query, "result_type": "recent", "count": 100}) response = client.fetch(url, headers={'Authorization': oauth(url)}) body = json.loads(response.body) result_count = len(body['statuses']) now = datetime.datetime.utcnow() raw_oldest_tweet_at = body['statuses'][-1]['created_at'] oldest_tweet_at = datetime.datetime.strptime(raw_oldest_tweet_at, "%a %b %d %H:%M:%S +0000 %Y") seconds_diff = time.mktime(now.timetuple()) - \ time.mktime(oldest_tweet_at.timetuple()) tweets_per_second = float(result_count) / seconds_diff self.write(""" <div style="text-align: center"> <div style="font-size: 72px">%s</div> <div style="font-size: 144px">%.02f</div> <div style="font-size: 24px">tweets per second</div> </div>""" % (query, tweets_per_second))
def __init__(self, ckey, csecret, atoken, asecret, screen_name="", oauth_obj=None): # Oauth init if oauth_obj == None: self.oauth = oauth.oauth(ckey, csecret, atoken, asecret) else: self.oauth = oauth_obj self.user = {"screen_name": screen_name} # ratelimit var init self.ratelimit_limit = -1 self.ratelimit_remaining = -1 self.ratelimit_reset = datetime.datetime.now() self.ratelimit_iplimit = -1 self.ratelimit_ipremaining = -1 self.ratelimit_ipreset = datetime.datetime.now()
c.putheader("X-Verify-Credentials-Authorization", oauth_header) c.putheader("X-Auth-Service-Provider", self.verify_credentials_url) c.putheader("Content-Length", str(len(multipart))) c.putheader("Content-Type", ctype.split(": ", 1)[-1]) c.endheaders() c.send(multipart) response = c.getresponse().read() return json.loads(response) if __name__ == "__main__": import sys ckey = sys.argv[1] csecret = sys.argv[2] atoken = sys.argv[3] asecret = sys.argv[4] filepath = sys.argv[5] # from python-twoauth apikey = "dcb62be3b2f310d4484f22364c1edd65" oauth = oauth.oauth(ckey, csecret, atoken, asecret) twpic = Twitpic(oauth, apikey) ret = twpic.upload(open(filepath, "rb"), "らりるれろ") print ret
def get(self): query = self.get_argument('q') client = tornado.httpclient.AsyncHTTPClient() url = "https://api.twitter.com/1.1/search/tweets.json?" + \ urllib.urlencode({"q": query, "result_type": "recent", "count": 100}) client.fetch(url, headers={'Authorization': oauth(url)}, callback=self.on_response)
import datetime as dt from loggingHandler import logger from downloadFiles import downloadFiles from uploadFiles import uploadFiles from deleteFiles import deleteFiles from updateReport import updateReport from executeRules import executeRules from oauth import oauth starttime = dt.datetime.now() if __name__ == '__main__': logger.info("**************************************") logger.info("Content Analyzer API Sample tool starting...") logger.info("Logs can be found in the current directory, processing.log") uploadSuccess = oauth() bearerToken = "Bearer " + uploadSuccess logger.info("Uploading files") uploadSuccess = uploadFiles(bearerToken) if (uploadSuccess): logger.info("Ready to download output files...") complete = downloadFiles(bearerToken) if (complete): logger.info("Deleting files on the server") deleteFiles() uploadSuccess = executeRules() if (uploadSuccess): logger.info("All done")
def user(self, **params): path = "https://userstream.twitter.com/2/user.json" return Stream(self._request(path, params=params)) import sys import oauth if __name__ == "__main__": ckey = sys.argv[1] csecret = sys.argv[2] atoken = sys.argv[3] asecret = sys.argv[4] oauth = oauth.oauth(ckey, csecret, atoken, asecret) s = StreamingAPI(oauth) # User Streams start streaming = s.user() streaming.start() while True: try: events = streaming.pop() for e in events: print type(e) streaming.event.wait() except:
import praw import requests import requests.auth import oauth from bs4 import BeautifulSoup import csv import re client_id = 'aCOsBF1HMnSKDg' client_secret = 'tpvzRJM44mwXXy2UsZoVSD54ThM' reddit = oauth.oauth(client_id, client_secret) idx = 0 comment_file = open('comments.csv', 'wb') wr = csv.writer(comment_file, dialect = 'excel') for comments in reddit.subreddit('all').comments(limit = None): idx += 1 soup = BeautifulSoup(comments.body_html,"lxml") if not soup.find('a'): for node in soup.findAll('p'): wr.writerow([''.join(node.findAll(text = True)).encode('utf-8')]) print idx '''ids = submission post = reddit.submission(ids) post.comment_sort = 'top' post.comments.replace_more(limit = 1) comment_file = open('comments.csv', 'wb')
COMMANDS = [ 'help', 'client', 'clients', 'upload', 'shell', 'pwd', 'exit', 'kill', 'scan', 'ls', 'system', 'wget', 'cat', 'crypt' ] HELP = ''' -h --help --Show the help menu -c --client [ID] --Connect to client using <client ID> -clients --Show all connected clients -u --upload --Upload file, executable, payload etc. -shell --Open shell and execute commands -exit --Exit the server and keep all clients alive -end --Kill current connection -kill --Kill all active connections -scan --Scan the network -system --Show all current system information -wget --Download file from web -crypt --Generate crypto token ''' banner() #print target #print host print HELP q = raw_input("Sign in as existing user? [y/n] ") if q == "y" or q == "Y": oauth() Main() else: create() Main()