From 69b4001fff3e2e7ac530fc6f422075023b23f7c7 Mon Sep 17 00:00:00 2001 From: AniketTripathi05 <126483339+AniketTripathi05@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:29:31 +0530 Subject: [PATCH 1/2] Update first_timers.py i have actually formatted the previous code --- first_timers/first_timers.py | 76 ++++++------------------------------ 1 file changed, 13 insertions(+), 63 deletions(-) diff --git a/first_timers/first_timers.py b/first_timers/first_timers.py index 773124e..463076e 100644 --- a/first_timers/first_timers.py +++ b/first_timers/first_timers.py @@ -9,15 +9,14 @@ # Set up logging logging.basicConfig(filename='app.log', level=logging.INFO) - DAYS_OLD = 15 MAX_TWEETS_LEN = 280 +ELLIPSE = u'…' -ellipse = u'…' -api = 'https://api.github.com/search/issues' -FIRST_ISSUE_QUERY_URL = api + '?q=label:"{}"+is:issue+is:open&sort=updated&order=desc' +API = 'https://api.github.com/search/issues' +FIRST_ISSUE_QUERY_URL = API + '?q=label:"{}"+is:issue+is:open&sort=updated&order=desc' -# Logging helper function +# Logging helper functions def log_info(message): logging.info(message) @@ -27,18 +26,14 @@ def log_warning(message): def log_error(message): logging.error(message) - def humanize_url(api_url: str) -> str: - """Make an API endpoint to a Human endpoint.""" - match = re.match( - 'https://api.github.com/repos/(.*)/(.*)/issues/([0-9]*)', api_url) + """Make an API endpoint a Human endpoint.""" + match = re.match('https://api.github.com/repos/(.*)/(.*)/issues/([0-9]*)', api_url) if not match: raise ValueError(f'Format of API URLs has changed: {api_url}') user, repo, issue_num = match.group(1, 2, 3) - return f'https://github.com/{user}/{repo}/issues/{issue_num}' - def get_first_timer_issues(issue_label: str) -> list: """Fetches the first page of issues with the label first-timers-label which are still open. @@ -53,12 +48,10 @@ def get_first_timer_issues(issue_label: str) -> list: return items - def check_days_passed(date_created: str, days: int) -> bool: created_at = datetime.strptime(date_created, "%Y-%m-%dT%H:%M:%SZ") return (datetime.now() - created_at).days < days - def add_repo_languages(issues): """Adds the repo languages to the issues list.""" for issue in issues: @@ -70,22 +63,18 @@ def add_repo_languages(issues): if res.ok: issue['languages'] = res.json() else: - log_warning('Could not handle response: ' + - str(res) + ' from the API.') + log_warning('Could not handle response: ' + str(res) + ' from the API.') return issues - def get_fresh(old_issue_list, new_issue_list): """Returns which issues are not present in the old list of issues.""" old_urls = {x['url'] for x in old_issue_list} return [x for x in new_issue_list if x['url'] not in old_urls] - def tweet_issues(issues, creds, debug=False): """Takes a list of issues and credentials and tweets through the account - associated with the credentials. - Also takes a parameter 'debug', which can prevent actual tweeting. - Returns a list of tweets. + associated with the credentials. Also takes a parameter 'debug', which can + prevent actual tweeting. Returns a list of tweets. """ if len(issues) == 0: return [] @@ -112,52 +101,13 @@ def tweet_issues(issues, creds, debug=False): title = issue['title'] if len(title) > allowed_title_len: - title = title[: allowed_title_len - 1] + ellipse + title = title[:allowed_title_len - 1] + ELLIPSE else: if 'languages' in issue: - language_hashTags = ''.join( - ' #{}'.format(lang) for lang in issue['languages'] - ) + language_hashTags = ''.join(' #{}'.format(lang) for lang in issue['languages']) hashTags = hashTags + language_hashTags - max_hashtags_len = MAX_TWEETS_LEN - \ - (url_len + 1) - (len(title) + 1) + max_hashtags_len = MAX_TWEETS_LEN - (url_len + 1) - (len(title) + 1) if len(hashTags) > max_hashtags_len: - hashTags = hashTags[:max_hashtags_len - 1] + ellipse - - url = humanize_url(issue['url']) - - try: - # Encoding here because .format will fail with Unicode characters. - tweet = '{title} {url} {tags}'.format( - title=title, - url=url, - tags=hashTags - ) - - if not debug: - api.update_status(tweet) - - tweets.append({ - 'error': None, - 'tweet': tweet - }) - - log_info('Tweeted issue: {}'.format(issue['title'])) - except Exception as e: - tweets.append({ - 'error': e, - 'tweet': tweet - }) - - log_error('Error tweeting issue: {}'.format(issue['title'])) - log_error('Error message: {}'.format(str(e))) - - return tweets - - -def limit_issues(issues, limit_len=100000): - """Limit the number of issues saved in our DB.""" - sorted_issues = sorted(issues, key=lambda x: x['updated_at'], reverse=True) - return sorted_issues[:limit_len] + hashTags = hashTags[:max_hashtags_len - 1] + E From 94d470c2b51bc52771f8ad90bfdfdccceeebcb5d Mon Sep 17 00:00:00 2001 From: AniketTripathi05 <126483339+AniketTripathi05@users.noreply.github.com> Date: Sat, 14 Oct 2023 22:28:32 +0530 Subject: [PATCH 2/2] Update first_timers.py pls review it and tell if anything is wrong --- first_timers/first_timers.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/first_timers/first_timers.py b/first_timers/first_timers.py index 463076e..b137f15 100644 --- a/first_timers/first_timers.py +++ b/first_timers/first_timers.py @@ -73,8 +73,11 @@ def get_fresh(old_issue_list, new_issue_list): def tweet_issues(issues, creds, debug=False): """Takes a list of issues and credentials and tweets through the account - associated with the credentials. Also takes a parameter 'debug', which can - prevent actual tweeting. Returns a list of tweets. + associated with the credentials. + + # Also takes a parameter 'debug', which can prevent actual tweeting. + + Returns a list of tweets. """ if len(issues) == 0: return [] @@ -104,10 +107,16 @@ def tweet_issues(issues, creds, debug=False): title = title[:allowed_title_len - 1] + ELLIPSE else: if 'languages' in issue: - language_hashTags = ''.join(' #{}'.format(lang) for lang in issue['languages']) + language_hashTags = ''.join( + ' #{}'.format(lang) for lang in issue['languages'] + ) + # Revert the previous change (multi-line comment) + # language_hashTags = ''.join(' #{}'.format(lang) for lang in issue['languages']) hashTags = hashTags + language_hashTags max_hashtags_len = MAX_TWEETS_LEN - (url_len + 1) - (len(title) + 1) if len(hashTags) > max_hashtags_len: - hashTags = hashTags[:max_hashtags_len - 1] + E + hashTags = hashTags[:max_hashtags_len - 1] + ELLIPSE + # Revert the previous change + # hashTags = hashTags[:max_hashtags_len - 1] + E