Last active
May 27, 2020 15:27
-
-
Save Jacoby6000/f632b25846ed8e673e45dff6a2eae1ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# max_records is there so you can get back partial result sets, if you want. Useful for testing. | |
def get_all_course_data(max_records=None): | |
page_number = 1 | |
page_size = 100 | |
more_records_exist = True | |
all_courses = [] | |
while more_records_exist: | |
api_response = get_courses(page_number, page_size) | |
results = process_response(api_response) | |
# add the new results to our aggregate set | |
all_courses += results | |
# if we get back fewer records than we asked for, we know we reached the end. | |
# terminate the loop if we're at the end, or have hit our max. | |
if len(results) < page_size or (max_records and len(all_courses) >= max_records): | |
more_records_exist = False | |
else: | |
page_number += 1 | |
return all_courses | |
def get_courses(page_number, page_size): | |
courses_url = "https://www.udemy.com/api-2.0/courses/" | |
query_params = { | |
"page_size": page_size, | |
"page": page_number | |
} | |
credentials = ('username', 'password') | |
return requests.get(url=courses_url, auth=credentials, params=query_params).json() | |
def process_response(api_response): | |
# api_response is a dictionary object which is a representation of the json structure. | |
courses = [] | |
# The json structure places all of the courses inside the results array | |
for course in api_response['results']: | |
# we add to the courses list tuples containing id, price, and headline. | |
courses.append((course['id'], course['price'], course['headline'])) | |
return courses |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment