Example #1
0
    def populate_database(self, published_before_datetime, page_token, search_params):

        if page_token != None: # get next page results
            search_params['pageToken'] = page_token
        elif published_before_datetime != None: # Continue populating database from where we last left off
            search_params['publishedBefore'] = published_before_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
        else: # the database is currently empty. Start a search
            # Ensure that we get the updated videos list
            search_params['publishedAfter'] = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ')

        response = requests.get(self.request_url, params=search_params)

        if str(response.status_code) == '200':
            response = response.json() # Convert json response to python dictionary
            next_page_token = response['nextPageToken']

            for item in response['items']:
                video_data = item['snippet']
                video = Video() # Video Model instance
                video.title = video_data['title']
                video.description = video_data['description']
                video.publish_datetime = video_data['publishedAt']
                video.thumbnail_url = video_data['thumbnails']['default']['url']
                video.save()
                print(video.publish_datetime)

            return next_page_token # needed for next API call
        else:
            print('failed to fetch data')
            print(response.json())