示例#1
0
def profileScrape():

    global POINTS

    resource_url = "/api/v1/user"

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    start = time.time()
    response = client.access_resource(resource_url, ACCESS_TOKEN)
    mongoclient = MongoClient()
    db = mongoclient.flexkhan


    start = time.time()
    parsed_json = json.loads(response)
    length = len(parsed_json)
    profile = db.profile
    find = profile.find({})
    
    dbCount = profile.find({}).count()-1

    end = time.time()
    if dbCount == 0:
        POINTS = find[0]["points"]

    if parsed_json["points"] == POINTS:
        print "Match"
    else:
        print "No Match"
        result = db.profile.insert_one(parsed_json)
        POINTS = parsed_json["points"]
示例#2
0
def api_call(target_version,
             target_api_url,
             session,
             debug=False,
             authenticate=True):
    """
    Generic API call function, that will try to use an authenticated request if available,
    otherwise will fall back to non-authenticated request.
    """
    # TODO : Use requests for both kinds of authentication.
    # usage : api_call("v1", "/badges")
    resource_url = "/api/" + target_version + target_api_url
    try:
        if authenticate and session.REQUEST_TOKEN and session.ACCESS_TOKEN:
            client = TestOAuthClient(session.SERVER_URL, CONSUMER_KEY,
                                     CONSUMER_SECRET)
            response = client.access_resource(resource_url,
                                              session.ACCESS_TOKEN)
        else:
            response = requests.get(session.SERVER_URL + resource_url).content
        json_object = json.loads(response)
    except Exception as e:
        print e, "for target: %(target)s " % {"target": target_api_url}
        return {}
    if (debug):
        print json_object
    return json_object
示例#3
0
def get_api_resource():

    global result, ticker

    resource_url = "/api/v1/user/exercises/progress_changes"

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    start = time.time()
    response = client.access_resource(resource_url, ACCESS_TOKEN)
    end = time.time()

    print type(response)
    print "\n"
    parsed_json = json.loads(response)
    print type(parsed_json)
    length = len(parsed_json)
    mclient = MongoClient()
    db = mclient.flexkhan
    progress = db.progress
    # result = db.progress.insert_one(parsed_json[0])
    result = progress.find()
    size = result.count()
    ticker = 0
    while ticker < size:
       date = datetime.strptime(result[ticker]['date'], "%Y-%m-%dT%H:%M:%SZ")
       aware = pytz.timezone('US/Mountain').localize(date)
       utc_dt = aware.astimezone(pytz.utc)
       print utc_dt.strftime("%Y:%m:%d %I:%M:%S")
       ticker += 1

    print "\nTime: %ss\n" % (end - start)
示例#4
0
def progressUpdate():

    resource_url = "/api/v1/user/exercises/progress_changes"

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    response = client.access_resource(resource_url, ACCESS_TOKEN)
    mongoclient = MongoClient()
    db = mongoclient.flexkhan

    counter = 0
    while counter == 0:
        start = time.time()
        parsed_json = json.loads(response)
        freshSize = len(parsed_json)
        progress = db.progress
        find = progress.find({})
        currentDBCount = progress.find({}).count()
        
        if currentDBCount != freshSize:
            diff = freshSize - currentDBCount
            diffcounter = diff 
            i = 0
            while i < diffcounter:
                marker = freshSize-diff+i
                print parsed_json[marker]
                print "\n"
                print marker
                result = db.progress.insert_one(parsed_json[marker])
                i += 1
        else:
            print("No diff")
        
        # print "\nTime: %ss\n" % (end - start) 
        counter = 1 
示例#5
0
文件: test.py 项目: xmarcosx/khan-api
def get_api_resource():
	resource_url = raw_input("Resource relative url (/api/v1/playlists): ") or "/api/v1/playlists"
	client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
	start = time.time()
	response = client.access_resource(resource_url, ACCESS_TOKEN)
	end = time.time()
	print "\n"
	print response
	print "\nTime: %ss\n" % (end - start)
示例#6
0
def get_api_resource():

    resource_url = raw_input(
        "Resource relative url (/api/v1/playlists): ") or "/api/v1/playlists"

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    response = client.access_resource(resource_url, ACCESS_TOKEN)

    print "\n"
    print response
    print "\n"
示例#7
0
def get_api_resource():

    resource_url = (raw_input("Resource relative url (/api/v1/playlists): ") or
                    "/api/v1/playlists")

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    response = client.access_resource(resource_url, ACCESS_TOKEN)

    print "\n"
    print response
    print "\n"
def get_api_resource(resource_url = '/api/v1/user/exercises/scientific_notation'):

    #resource_url = '/api/v1/user/exercises/scientific_notation' #JS# raw_input("Resource relative url (/api/v1/playlists): ") \
        #or "/api/v1/playlists"
    

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    start = time.time()
    response = client.access_resource(resource_url, ACCESS_TOKEN)
    end = time.time()
    return response, start, end
示例#9
0
def get_api_resource(
        resource_url='/api/v1/user/exercises/scientific_notation'):

    #resource_url = '/api/v1/user/exercises/scientific_notation' #JS# raw_input("Resource relative url (/api/v1/playlists): ") \
    #or "/api/v1/playlists"

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    start = time.time()
    response = client.access_resource(resource_url, ACCESS_TOKEN)
    end = time.time()
    return response, start, end
示例#10
0
    def get_api_resource(self, resourceUrl):

        # Example URLs
        # /api/v1/user/exercises/[email protected]
        # /api/v1/[email protected]
        # /api/v1/exercises

        client = TestOAuthClient(self.SERVER_URL, self.CONSUMER_KEY, self.CONSUMER_SECRET)
        start = time.time()
        response = client.access_resource(resourceUrl, self.ACCESS_TOKEN)
        end = time.time()

        # print "\nTime: %ss\n" % (end - start)
        return response
示例#11
0
def api_call(target_version, target_api_url, debug=False, authenticate=True):
    """
    Generic API call function, that will try to use an authenticated request if available,
    otherwise will fall back to non-authenticated request.
    """
    # TODO : Use requests for both kinds of authentication.
    # usage : api_call("v1", "/badges")
    resource_url = "/api/" + target_version + target_api_url
    try:
        if authenticate and REQUEST_TOKEN and ACCESS_TOKEN:
            client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
            response = client.access_resource(resource_url, ACCESS_TOKEN)
        else:
            response = requests.get(SERVER_URL + resource_url).content
        json_object = json.loads(response)
    except Exception as e:
        print e
        return {}
    if(debug):
        print json_object
    return json_object
示例#12
0
def progressScrape():

    resource_url = "/api/v1/user/exercises/progress_changes"

    client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
    response = client.access_resource(resource_url, ACCESS_TOKEN)
    mongoclient = MongoClient()
    db = mongoclient.flexkhan

    counter = 0
    while counter == 0:
        
        parsed_json = json.loads(response)
        size = len(parsed_json)
        progress = db.progress
        
        i = 0
        while i < size:
            result = db.progress.insert_one(parsed_json[i])
            print("Check!")
            i += 1
        end = time.time()
        # print "\nTime: %ss\n" % (end - start) 
        counter = 1