示例#1
0
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
示例#2
0
def get_last_update(data_url):
    """
    Retrieve data from Johns Hopinks CSV files given a accessible URL.
    :param data_url: Data URL.
    :return: Dictionary with the last update grouped by location.
    """
    result_dict = dict()
    for attempt in range(JOHNS_HOPKINS_DATA_ATTEMPTS):
        try:
            response = requests.get(
                data_url,
                headers={'User-Agent': JOHNS_HOPKINS_USER_AGENT},
                timeout=JOHNS_HOPKINS_DATA_TIMEOUT)
            rows = [
                row
                for row in csv.reader(StringIO(response.text), delimiter=',')
            ][1:]
            for row in rows:
                dict_key = row[1] if not row[0] else f'{row[0]}, {row[1]}'
                result_dict[dict_key] = int(row[-1])
            return result_dict
        except Exception as e:
            log.error(
                f'Cannot retrieve information from {data_url} - Attempt [{attempt + 1}] - [{e}]'
            )
            time.sleep(JOHNS_HOPKINS_DATA_RTD)
            if attempt == JOHNS_HOPKINS_DATA_ATTEMPTS - 1:
                log.exception(e)
                return None
示例#3
0
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)
示例#4
0
def run():
    """
    Main function in order to process a new iteration of the Twitter bot.
    :return: Iteration done.
    """
    try:
        for i, item in enumerate(RESOURCES):
            item_name = item['name']
            log.info(f'{i + 1}/{len(RESOURCES)} Processing {item_name}...')
            results = retriever.get_last_update(item['data_url'])
            if results is not None:
                item_data_path = item['data_path']
                item_data_path_exists = os.path.isfile(item_data_path)
                # Get old results if they exist
                old_results = dict()
                if item_data_path_exists:
                    with open(item_data_path, 'r') as item_data_file:
                        old_results = json.load(item_data_file)
                # Save latest results
                with open(item_data_path, 'w') as item_data_file:
                    json.dump(results, item_data_file)
                # Check for differences if it did not exist before
                if item_data_path_exists:
                    total_worldwide = sum(old_results.values())
                    diff_results = list(dictdiffer.diff(results, old_results))
                    for j, diff_tuple in enumerate(diff_results):
                        log.info(f'{j + 1}/{len(diff_results)} New changes found: [{diff_tuple}]')
                        total_worldwide = _notify_changes(diff_tuple, item_name, item['icon'], results, total_worldwide)
            time.sleep(TIME_BETWEEN_RESOURCES)
    except Exception as e:
        log.error(f'Unexpected error: [{e}]')
        log.exception(e)
示例#5
0
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
示例#6
0
def login():
    try:
        with Timer('Get Auth URL'):
            auth_url = spotify_api.get_auth_url()
        return response.make(error=False, response=dict(redirect=auth_url))
    except Exception as e:
        log.error(
            f'Exception while processing {login.__name__} function: [{e}]')
        log.exception(e)
        return response.make(error=True, message=MESSAGE_ERROR)
示例#7
0
def download_models():
    try:
        if 'credentials' not in flask.session:
            return flask.redirect(flask.url_for(oauth2_authorize.__name__))
        thing.download_models(
            flask.session.get('credentials', {}).get('access_token'))
        return flask.redirect(THINGIVERSE_API_DONE)
    except Exception as e:
        message = f'Exception in {download_models.__name__} function: [{e}]'
        log.error(message)
        log.exception(e)
        return flask.jsonify(dict(error=True, response=message))
示例#8
0
def playlist(code, github_user):
    try:
        with Timer('Request token retrieving'):
            access_token = spotify_api.get_access_token(code)
            if not access_token:
                return response.make(error=True,
                                     message=MESSAGE_TOKEN_NOT_FOUND)

        with Timer('Get profile data'):
            user_id = spotify_api.get_current_user_id(access_token)
            if not user_id:
                return response.make(error=True,
                                     message=MESSAGE_SPOTIFY_NOT_FOUND)

        with Timer('Playlist generation'):
            playlist_id, playlist_url = spotify_api.post_playlist(
                access_token, user_id, github_user)
            if not playlist_id and not playlist_url:
                return response.make(error=True,
                                     message=MESSAGE_SPOTIFY_PLAYLIST_ERROR)

        with Timer('Retrieve commits from user'):
            commit_messages = github_api.get_commit_messages(github_user)
            if not commit_messages:
                return response.make(error=True,
                                     message=MESSAGE_COMMIT_NOT_FOUND)

        with Timer('Retrieve most common words'):
            most_common_words = nltk.extract_most_common(commit_messages)

        with Timer('Search for tracks'):
            with ThreadPool(CONCURRENT_POOL) as pool:
                thread_args = [(access_token, word)
                               for word in most_common_words]
                track_uri_list = list(
                    pool.imap(spotify_api.search_for_tracks, thread_args))
                track_uri_list = [t for t in track_uri_list if t]

        with Timer('Add tracks to the playlist'):
            success = spotify_api.add_tracks_to_playlist(
                access_token, playlist_id, track_uri_list)
            if not success:
                return response.make(error=True,
                                     message=MESSAGE_SPOTIFY_TRACK_ERROR)

        return response.make(error=False, response=dict(url=playlist_url))

    except Exception as e:
        log.error(
            f'Exception while processing {playlist.__name__} function: [{e}]')
        log.exception(e)
        return response.make(error=True, message=MESSAGE_ERROR)
示例#9
0
def run():
    """
    Main function in order to process a new iteration of the Twitter bot.
    :return: Iteration done.
    """
    try:
        resources_list = [DATA_CONFIRMED, DATA_DEATHS, DATA_RECOVERED]
        confirmed_results, deaths_results, recovered_results = retriever.get_last_update(
        )
        if all([confirmed_results, deaths_results, recovered_results]):
            for i, item_name in enumerate(resources_list):
                log.info(
                    f'{i + 1}/{len(resources_list)} Processing {item_name}...')
                # Get results
                results = None
                if item_name == DATA_CONFIRMED:
                    results = confirmed_results
                elif item_name == DATA_DEATHS:
                    results = deaths_results
                elif item_name == DATA_RECOVERED:
                    results = recovered_results
                # Process
                if results is not None:
                    item_data_path = DATA_PATH_DICT[item_name]
                    item_data_path_exists = os.path.isfile(item_data_path)
                    # Get old results if they exist
                    old_results = dict()
                    if item_data_path_exists:
                        with open(item_data_path, 'r') as item_data_file:
                            old_results = json.load(item_data_file)
                    # Save latest results
                    with open(item_data_path, 'w') as item_data_file:
                        json.dump(results, item_data_file)
                    # Check for differences if it did not exist before
                    if item_data_path_exists:
                        total_worldwide = sum(old_results.values())
                        diff_results = list(
                            dictdiffer.diff(results, old_results))
                        for j, diff_tuple in enumerate(diff_results):
                            log.info(
                                f'{j + 1}/{len(diff_results)} New changes found: [{diff_tuple}]'
                            )
                            total_worldwide = _notify_changes(
                                diff_tuple, item_name, ICON_DICT[item_name],
                                results, total_worldwide)
                time.sleep(TIME_BETWEEN_RESOURCES)
    except Exception as e:
        log.error(f'Unexpected error: [{e}]')
        log.exception(e)
示例#10
0
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
示例#11
0
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
示例#12
0
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
示例#13
0
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
示例#14
0
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
示例#15
0
def oauth2_callback():
    try:
        auth_code = flask.request.args.get('code')
        auth_query_parameters = dict(
            client_id=os.environ.get('THINGIVERSE_API_CLIENT_ID'),
            client_secret=os.environ.get('THINGIVERSE_API_CLIENT_SECRET'),
            code=auth_code)
        response = requests.post(THINGIVERSE_API_TOKEN,
                                 params=auth_query_parameters)
        query_string = dict(parse_qsl(response.text))
        flask.session['credentials'] = dict(
            access_token=query_string.get('access_token'))
        return flask.redirect(flask.url_for(download_models.__name__))
    except Exception as e:
        message = f'Exception in {oauth2_callback.__name__} function: [{e}]'
        log.error(message)
        log.exception(e)
        return flask.jsonify(dict(error=True, response=message))
示例#16
0
def oauth2_authorize():
    try:
        auth_query_parameters = dict(
            client_id=os.environ.get('THINGIVERSE_API_CLIENT_ID'),
            redirect_uri=flask.url_for(oauth2_callback.__name__,
                                       _external=True),
            response_type='code')
        url_args = '&'.join([
            '{}={}'.format(key, quote(val))
            for key, val in auth_query_parameters.items()
        ])
        auth_url = '{}/?{}'.format(THINGIVERSE_API_AUTH, url_args)
        return flask.redirect(auth_url)
    except Exception as e:
        message = f'Exception in {oauth2_authorize.__name__} function: [{e}]'
        log.error(message)
        log.exception(e)
        return flask.jsonify(dict(error=True, response=message))
示例#17
0
def main():
    if not os.path.isfile('.env'):
        log.error('Environment file (.env) could not be found.')
        return

    log.info(f'Spinning up local API for extracting Thingiverse Thing IDs at port {THINGIVERSE_FLASK_PORT}.')
    process = multiprocessing.Process(target=launcher.run_app, args=())
    process.start()
    log.info(f'Sleeping {THINGIVERSE_FLASK_WAIT_PRE} seconds for letting the API to initialize and open a browser.')
    time.sleep(THINGIVERSE_FLASK_WAIT_PRE)
    webbrowser.open(f'http://localhost:{THINGIVERSE_FLASK_PORT}{THINGIVERSE_FLASK_ENDPOINT}', new=2)
    if THINGIVERSE_FLASK_WAIT_ENABLE:
        log.info(f'Sleeping {THINGIVERSE_FLASK_WAIT_POST} seconds for letting the process to finish.')
        log.info('You can stop the script if the process is done.')
        time.sleep(THINGIVERSE_FLASK_WAIT_POST)
        log.info('Killing processes.')
        process.terminate()
    else:
        log.debug('Processing forever.')
示例#18
0
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
示例#19
0
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
示例#20
0
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
示例#21
0
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
示例#22
0
def execute(method, url, params=None):
    if method not in ('GET', 'POST', 'PUT', 'PATCH', 'DELETE'):
        log.error(
            '[ERROR] Indicated method must be GET, POST, PUT, PATCH or DELETE')
        return None
    for it in range(1, 4):
        try:
            response = requests.request(method=method,
                                        url=url,
                                        params=params,
                                        timeout=REQUEST_TIMEOUT)
            if response is not None and response.ok:
                return response.json()
            else:
                raise Exception(
                    f'Problem requesting URL {url}: '
                    f'[{response.status_code if response is not None else None}]. Number of tries: {it}'
                )
        except Exception as e:
            time.sleep(it)
            if it >= 3:
                log.debug(f'Error: {e}')
    return None
示例#23
0
def get(github_user):
    try:
        with Timer('Initialize response dictionary'):
            resp = {}

        with Timer('Basic information'):
            basic_information = github_api.get_basic_user_information(
                github_user)
            if not basic_information:
                return response.make(error=True,
                                     message=MESSAGE_USER_NOT_FOUND)
            resp['username'] = github_user
            resp['photo'] = response.get('avatar_url', basic_information)
            resp['public_repos'] = response.get('public_repos',
                                                basic_information)
            resp['public_gists'] = response.get('public_gists',
                                                basic_information)
            resp['followers'] = response.get('followers', basic_information)
            resp['following'] = response.get('following', basic_information)

        with Timer('Repositories'):
            repos_list = github_api.get_repos_from_user(github_user)
            if not repos_list:
                return response.make(error=True,
                                     message=MESSAGE_USER_NOT_FOUND)
            resp['repo_amount'] = len(repos_list)
            resp['repo_fork_amount'] = sum(
                [response.get('fork', d, default=False) for d in repos_list])
            resp['repo_total_size'] = sum(
                [response.get('size', d, default=0)
                 for d in repos_list]) / 1000
            resp['repo_total_stars'] = sum([
                response.get('stargazers_count', d, default=0)
                for d in repos_list
            ])
            resp['repo_total_forks'] = sum([
                response.get('forks_count', d, default=0) for d in repos_list
            ])
            resp['repo_total_open_issues'] = sum([
                response.get('open_issues', d, default=0) for d in repos_list
            ])
            resp['repo_avg_size'] = formatter.to_float(
                resp['repo_total_size'] / resp['repo_amount'])
            resp['repo_avg_stars'] = formatter.to_float(
                resp['repo_total_stars'] / resp['repo_amount'])
            resp['repo_avg_forks'] = formatter.to_float(
                resp['repo_total_forks'] / resp['repo_amount'])
            resp['repo_avg_open_issues'] = formatter.to_float(
                resp['repo_total_open_issues'] / resp['repo_amount'])

        with Timer('Languages & topics - threads'):
            with ThreadPool(CONCURRENT_POOL) as pool:
                thread_args = [(github_user, response.get('name', r))
                               for r in repos_list if response.get('name', r)]
                language_response_list = list(
                    pool.imap(github_api.get_languages, thread_args))
                topic_response_list = list(
                    pool.imap(github_api.get_topics, thread_args))

        with Timer('Languages & topics - amount'):
            languages_dict = {}
            for language_response in language_response_list:
                if language_response:
                    for key, value in language_response.items():
                        if key not in languages_dict:
                            languages_dict[key] = 0
                        languages_dict[key] += value
            topics_dict = {}
            for topic_response in topic_response_list:
                if topic_response:
                    for topic in topic_response:
                        if topic not in topics_dict:
                            topics_dict[topic] = 0
                        topics_dict[topic] += 1

        with Timer('Languages - percentage'):
            resp['languages'] = []
            total_languages = sum(languages_dict.values())
            sorted_languages = sorted(languages_dict.items(),
                                      key=operator.itemgetter(1),
                                      reverse=True)
            for idx in range(0, len(sorted_languages)):
                if idx < GITHUB_LANGUAGES_MAX:
                    language_name, language_amount = sorted_languages[idx]
                    percentage = formatter.to_float(
                        (language_amount / total_languages) * 100)
                    resp['languages'].append(
                        dict(label=language_name,
                             amount=language_amount,
                             percentage=percentage))
                else:
                    language_amount = sum([
                        v[1] for v in sorted_languages[GITHUB_LANGUAGES_MAX:]
                    ])
                    percentage = formatter.to_float(
                        (language_amount / total_languages) * 100)
                    resp['languages'].append(
                        dict(label='Others',
                             amount=language_amount,
                             percentage=percentage))
                    break

        with Timer('Topics - percentage'):
            resp['topics'] = []
            total_topics = sum(topics_dict.values())
            sorted_topics = sorted(topics_dict.items(),
                                   key=operator.itemgetter(1),
                                   reverse=True)
            for idx in range(0, len(sorted_topics)):
                if idx < GITHUB_TOPICS_MAX:
                    topic_name, topic_amount = sorted_topics[idx]
                    percentage = formatter.to_float(
                        (topic_amount / total_topics) * 100)
                    resp['topics'].append(
                        dict(label=topic_name,
                             amount=topic_amount,
                             percentage=percentage))
                else:
                    topic_amount = sum(
                        [v[1] for v in sorted_topics[GITHUB_TOPICS_MAX:]])
                    percentage = formatter.to_float(
                        (topic_amount / total_topics) * 100)
                    resp['topics'].append(
                        dict(label='others',
                             amount=topic_amount,
                             percentage=percentage))
                    break

        with Timer('Contributors - thread'):
            with ThreadPool(CONCURRENT_POOL) as pool:
                thread_args = [(github_user, response.get('name', r))
                               for r in repos_list if response.get('name', r)]
                contributor_response_list = list(
                    pool.imap(github_api.get_contributors, thread_args))

        with Timer('Contributors - save'):
            resp['commits'] = 0
            resp['commits_user'] = 0
            resp['commits_contributor'] = 0
            contributors_dict = {}
            for contributor_response in contributor_response_list:
                if contributor_response:
                    for contributor in contributor_response:
                        contributor_name = response.get('login', contributor)
                        if contributor_name:
                            contributions = response.get('contributions',
                                                         contributor,
                                                         default=0)
                            resp['commits'] += contributions
                            if contributor_name.lower() == github_user.lower():
                                resp['commits_user'] += contributions
                            else:
                                if contributor_name not in contributors_dict:
                                    contributors_dict[contributor_name] = dict(
                                        commits=0,
                                        photo=response.get('avatar_url',
                                                           contributor,
                                                           default=''),
                                        url=response.get('html_url',
                                                         contributor,
                                                         default=''))
                                resp['commits_contributor'] += contributions
                                contributors_dict[contributor_name][
                                    'commits'] += contributions
            resp['commits_user_percentage'] = formatter.to_float(
                (resp['commits_user'] / resp['commits']) * 100)
            resp['commits_contributor_percentage'] = formatter.to_float(
                (resp['commits_contributor'] / resp['commits']) * 100)
            resp['contributors'] = []
            for contributor_name, contributor_dict in contributors_dict.items(
            ):
                contributor_dict['label'] = contributor_name
                resp['contributors'].append(contributor_dict)
            resp['contributors'] = sorted(resp['contributors'],
                                          key=lambda k: k['commits'],
                                          reverse=True)

        return response.make(error=False, response=resp)

    except Exception as e:
        log.error(f'Exception while processing {get.__name__} function: [{e}]')
        log.exception(e)
        return response.make(error=True, message=MESSAGE_ERROR)
示例#24
0
import time

from src import scheduler
from src.config import DEBUG
from src.helper import log

if __name__ == '__main__':
    try:
        log.info('Covid19-bot was born!')
        if not DEBUG:
            scheduler.scheduler.start()
        else:
            log.info('Running jobs manually since DEBUG is enabled.')
            time.sleep(5)
            scheduler.run()
            while True:
                time.sleep(10000)

    except Exception as e:
        log.error(f'Unexpected error {e} in Covid-19')
        log.exception(e)

    finally:
        log.info('Rest in peace, Covid-19.')
示例#25
0
import time

from src import scheduler
from src.helper import log, env

if __name__ == '__main__':
    try:
        log.info('auto-factorial was born!')
        if not env.is_development():
            scheduler.scheduler.start()
        else:
            log.info(
                'Running jobs manually since DEVELOPMENT MODE is enabled, and then sleep eternally.'
            )
            scheduler.auto_factorial_cron_job()
            while True:
                time.sleep(1000)

    except Exception as e:
        log.error(f'Unexpected error {e} in auto-factorial')
        log.exception(e)

    finally:
        log.info('Rest in peace, auto-factorial.')
示例#26
0
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)