def get_repos_from_user(username): repos_array = [] page_number = 1 while True: for attempt in range(0, GITHUB_API_RETRIES): try: endpoint = GITHUB_USER_REPOS_ENDPOINT.format(username=username) params = dict( client_id=env.get_github_client_id(), client_secret=env.get_github_client_secret(), per_page=GITHUB_PER_PAGE, page=page_number ) response = requests.get(endpoint, params=params, timeout=GITHUB_API_TIMEOUT) if response.ok: response = response.json() if response: repos_array.extend(response) break else: return repos_array except Exception as e: if attempt < GITHUB_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(GITHUB_API_RTD) else: log.error(f'Error in {get_repos_from_user.__name__} function. [{e}]') return None page_number += 1
def get_commit_messages(username): commits_array = [] page_number = 1 while True: for attempt in range(0, GITHUB_API_RETRIES): try: endpoint = GITHUB_USER_EVENTS_ENDPOINT.format(username=username) params = dict( client_id=env.get_github_client_id(), client_secret=env.get_github_client_secret(), page=page_number ) response = requests.get(endpoint, params=params, timeout=GITHUB_API_TIMEOUT) if response.ok: response = response.json() if response: for res in response: if get('type', res, default='') == GITHUB_PUSH_EVENT_TYPE: res_payload = get('payload', res) if res_payload: res_commits = get('commits', res_payload, default=[]) commits_array.extend([get('message', c) for c in res_commits]) break else: return commits_array except Exception as e: if attempt < GITHUB_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(GITHUB_API_RTD) else: log.error(f'Error in {get_commit_messages.__name__} function. [{e}]') return None page_number += 1
def get(name=None): """ API endpoint for returning a given device by its name, all if name is not specified. :return: All devices. """ try: for attempt in range(1, RETRIEVE_RETRY + 1): log.info(f'Attempt [{attempt}]...') chromecast_list = pychromecast.get_chromecasts() chromecast_list = [ Device(chromecast_item) for chromecast_item in chromecast_list ] if name is not None: log.info(f'Applying filter name by name: [{name}]') chromecast_list = [ device for device in chromecast_list if device.has_device_name(name) ] chromecast_list = [ device.serialize() for device in chromecast_list ] if chromecast_list: return response.make( error=False, response=dict(chromecast_list=chromecast_list)) else: log.warn(f'No devices found at attempt {attempt}.') time.sleep(RETRIEVE_RTD) log.warn(f'No devices found.') return response.make(error=False, response=dict(chromecast_list=[])) except Exception as e: log.error(f'Unexpected error: [{e}]') log.exception(e) return response.make(error=True, message=MESSAGE_UNEXPECTED_ERROR)
def get_start_hour(): environment_value = _get(ENV_NAME_START_HOUR) if environment_value is not None: if environment_value.isdigit() and 0 <= int(environment_value) <= 23: return environment_value else: log.warn( f'[{ENV_NAME_START_HOUR}] environment value has to be a natural between 0 and 23. ' f'Using default value: [{ENV_DEFAULT_START_HOUR}]') return ENV_DEFAULT_START_HOUR
def get_current_user_id(access_token): for attempt in range(0, SPOTIFY_API_RETRIES): try: headers = {'Authorization': f'Bearer {access_token}'} profile_response = requests.get(SPOTIFY_API_CURRENT_USER, headers=headers, timeout=SPOTIFY_API_TIMEOUT) if profile_response.ok: profile_response = profile_response.json() return response.get('id', profile_response) except Exception as e: if attempt < SPOTIFY_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(SPOTIFY_API_RTD) else: log.error(f'Error in {get_current_user_id.__name__} function. [{e}]') return None
def add_tracks_to_playlist(access_token, playlist_id, track_uri_list): for attempt in range(0, SPOTIFY_API_RETRIES): try: headers = {'Authorization': f'Bearer {access_token}'} data = {'uris': track_uri_list} endpoint = SPOTIFY_API_ADD_TRACKS.format(playlist_id=playlist_id) track_response = requests.post(endpoint, json=data, headers=headers, timeout=SPOTIFY_API_TIMEOUT) if track_response.ok: return True except Exception as e: if attempt < SPOTIFY_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(SPOTIFY_API_RTD) else: log.error(f'Error in {add_tracks_to_playlist.__name__} function. [{e}]') return None
def get_basic_user_information(username): for attempt in range(0, GITHUB_API_RETRIES): try: endpoint = GITHUB_SINGLE_USER_ENDPOINT.format(username=username) params = dict(client_id=env.get_github_client_id(), client_secret=env.get_github_client_secret()) response = requests.get(endpoint, params=params, timeout=GITHUB_API_TIMEOUT) if response.ok: response = response.json() return response except Exception as e: if attempt < GITHUB_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(GITHUB_API_RTD) else: log.error(f'Error in {get_basic_user_information.__name__} function. [{e}]') return None
def get_last_update(): """ Retrieve data from Worldometers. :return: 3 Dictionaries (confirmed, deaths & recovered) with the last update grouped by location. """ confirmed_dict = dict() death_dict = dict() recovered_dict = dict() html_content = _get_html(WORLDOMETERS_URL) if html_content: soup = BeautifulSoup(html_content, 'html.parser') table_countries = soup.findChildren('table', id='main_table_countries_today') # Table if table_countries: body_list = table_countries[0].findChildren('tbody') # Body if body_list: row_list = body_list[0].findChildren('tr') # Rows if row_list: for row in row_list: # Every row try: cell_list = row.findChildren('td') if cell_list and len(cell_list) >= 6: # Extract country country_cell = cell_list[0] if country_cell.find('a'): country = cell_list[0].find('a').text.strip() else: country = cell_list[0].text.strip() # Extract string confirmed_n = '' if not cell_list[1] else cell_list[1].text.strip().replace(',', '') deaths_n = '' if not cell_list[3] else cell_list[3].text.strip().replace(',', '') recovered_n = '' if not cell_list[5] else cell_list[5].text.strip().replace(',', '') # Parse to integer confirmed_n = 0 if not confirmed_n else int(confirmed_n) deaths_n = 0 if not deaths_n else int(deaths_n) recovered_n = 0 if not recovered_n else int(recovered_n) # Add to dictionary confirmed_dict[country] = confirmed_n death_dict[country] = deaths_n recovered_dict[country] = recovered_n except Exception as e: log.warn(f'There was an error processing one row - [{e}]. Ignoring...') return confirmed_dict, death_dict, recovered_dict
def get_contributors(args): username, repository = args for attempt in range(0, GITHUB_API_RETRIES): try: endpoint = GITHUB_CONTRIBUTORS_ENDPOINT.format(username=username, repository=repository) params = dict(client_id=env.get_github_client_id(), client_secret=env.get_github_client_secret()) response = requests.get(endpoint, params=params, timeout=GITHUB_API_TIMEOUT) if response.ok: response = response.json() return response except Exception as e: if attempt < GITHUB_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(GITHUB_API_RTD) else: log.error(f'Error in {get_contributors.__name__} function. [{e}]') return None
def get_topics(args): username, repository = args for attempt in range(0, GITHUB_API_RETRIES): try: endpoint = GITHUB_TOPICS_ENDPOINT.format(username=username, repository=repository) params = dict(client_id=env.get_github_client_id(), client_secret=env.get_github_client_secret()) headers = dict(Accept='application/vnd.github.mercy-preview+json') response = requests.get(endpoint, params=params, headers=headers, timeout=GITHUB_API_TIMEOUT) if response.ok: response = response.json()['names'] return response except Exception as e: if attempt < GITHUB_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(GITHUB_API_RTD) else: log.error(f'Error in {get_topics.__name__} function. [{e}]') return None
def post_playlist(access_token, spotify_user_id, github_user): for attempt in range(0, SPOTIFY_API_RETRIES): try: headers = {'Authorization': f'Bearer {access_token}'} data = {'name': f'Github {github_user}\'s playlist 🐙', 'description': 'Generated by git-inspect'} endpoint = SPOTIFY_API_CREATE_PLAYLIST.format(user_id=spotify_user_id) playlist_response = requests.post(endpoint, json=data, headers=headers, timeout=SPOTIFY_API_TIMEOUT) if playlist_response.ok: playlist_response = playlist_response.json() playlist_id = response.get('id', playlist_response) playlist_url = None if not response.get('external_urls', playlist_response) \ else response.get('spotify', playlist_response['external_urls']) return playlist_id, playlist_url except Exception as e: if attempt < SPOTIFY_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(SPOTIFY_API_RTD) else: log.error(f'Error in {post_playlist.__name__} function. [{e}]') return None, None
def get_access_token(code): for attempt in range(0, SPOTIFY_API_RETRIES): try: code_payload = { 'grant_type': 'authorization_code', 'code': str(code), 'redirect_uri': SPOTIFY_REDIRECT_URI, 'client_id': env.get_spotify_client_id(), 'client_secret': env.get_spotify_client_secret(), } post_request = requests.post(SPOTIFY_API_TOKEN_URL, data=code_payload, timeout=SPOTIFY_API_TIMEOUT) if post_request.ok: post_request = post_request.json() return response.get('access_token', post_request) except Exception as e: if attempt < SPOTIFY_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(SPOTIFY_API_RTD) else: log.error(f'Error in {get_access_token.__name__} function. [{e}]') return None
def _get_html(url): """ Retrieves the HTML content given a Internet accessible URL. :param url: URL to retrieve. :return: HTML content formatted as String, None if there was an error. """ for i in range(0, SCRAPE_RETRIES_AMOUNT): try: proxies = {'http': HTTP_PROXY} if TOR_ENABLE else {} headers = {'User-Agent': UserAgent().random} response = requests.get(url, proxies=proxies, headers=headers) assert response.ok html_content = response.content return html_content except Exception as e: if i == SCRAPE_RETRIES_AMOUNT - 1: log.error(f'Unable to retrieve HTML from {url}: {e}') else: log.warn(f'Unable to retrieve HTML from {url} - Retry {i}: {e}') time.sleep(random.uniform(SCRAPE_RTD_ERROR_MINIMUM, SCRAPE_RTD_ERROR_MAXIMUM)) return None
def search_for_tracks(args): access_token, query = args for attempt in range(0, SPOTIFY_API_RETRIES): try: headers = {'Authorization': f'Bearer {access_token}'} params = {'q': query, 'type': 'track', 'limit': 1} endpoint = SPOTIFY_API_SEARCH track_response = requests.get(endpoint, params=params, headers=headers, timeout=GITHUB_API_TIMEOUT) if track_response.ok: track_response = track_response.json() track_item = response.get('tracks', track_response) if track_item: for track_dict in response.get('items', track_item, default=[]): track_uri = response.get('uri', track_dict) if track_uri: return track_uri except Exception as e: if attempt < SPOTIFY_API_RETRIES - 1: log.warn(f'Attempt number {attempt}: Failed - [{e}]. Retrying...') time.sleep(SPOTIFY_API_RTD) else: log.error(f'Error in {search_for_tracks.__name__} function. [{e}]') return None
def get(name, source_url): """ API endpoint for playing a source to a device given its name. :param name: Device name. :param source_url: Source URL. :return: Source playing at given device. """ try: # Random generation, if needed random_video = None if source_url == PLAY_RANDOM: log.info('Getting random video...') random_video = random_extractor.get() source_url = random_video.get('sources')[0] # Extracting information log.info('Extracting source names...') source_ending = '.{}'.format(source_url.split('.')[-1]) source_name = source_url.split( '/')[-1] if not random_video else random_video.get('title') log.info(f'Source ending: [{source_ending}]') log.info(f'Source name: [{source_name}]') if source_ending not in MIME_TYPES: log.warn(MESSAGE_PLAY_DEVICE_NOT_FOUND) return response.make(error=True, message=MESSAGE_PLAY_DEVICE_NOT_FOUND) # Getting devices for attempt in range(1, RETRIEVE_RETRY + 1): log.info(f'Attempt [{attempt}]...') chromecast_list = pychromecast.get_chromecasts() chromecast_list = [ Device(chromecast_item) for chromecast_item in chromecast_list ] chromecast_list = [ device for device in chromecast_list if device.has_device_name(name) ] if chromecast_list: # Process media chromecast_device = chromecast_list[0] log.info( f'Chromecast device: [{chromecast_device.uuid} - {chromecast_device.name}]' ) log.info('Waiting for being loaded...') chromecast_device.chromecast_object.wait() log.info('Loaded.') mime_type = MIME_TYPES[source_ending] log.info(f'MIME type: [{mime_type}]') log.info(f'Cast type: [{chromecast_device.cast_type}]') if (chromecast_device.cast_type == 'audio' and 'video' in mime_type) or \ (chromecast_device.cast_type == 'group' and 'video' in mime_type): log.warn(MESSAGE_PLAY_MIME_DEVICE_NOT_SUPPORTED) return response.make( error=True, message=MESSAGE_PLAY_MIME_DEVICE_NOT_SUPPORTED) # Play media log.info('Playing media...') chromecast_device.chromecast_object.media_controller.play_media( url=source_url, content_type=mime_type, title=source_name) chromecast_device.chromecast_object.media_controller.block_until_active( ) log.info('Done!') return response.make( error=False, response=dict(message=MESSAGE_PLAY_SUCCESS)) else: log.warn(f'No devices found at attempt {attempt}.') time.sleep(RETRIEVE_RTD) log.warn(f'No devices found.') return response.make(error=True, message=MESSAGE_PLAY_DEVICE_NOT_FOUND) except Exception as e: log.error(f'Unexpected error: [{e}]') log.exception(e) return response.make(error=True, message=MESSAGE_UNEXPECTED_ERROR)