diff --git a/const.py b/const.py index 137cd4c..46639fd 100644 --- a/const.py +++ b/const.py @@ -1,5 +1,28 @@ class Const: + # Cities + CITY_REGEX = '.+$' + NOT_CHAR = '\W+' + COMMA_REGEX = ',+' + SPECIAL_CHARS = '[^A-Za-z0-9\s,]' + + # TODO Add links - info about country + TRIGGER_PHARSE = 'location:' + TRIGGER_SUBREDDITS = 'naturephotography,AdventurePhotography,snow,UrbanExploring,Outdoors' + KEYWORD = 'Location:\s*([^.\n]+)' + + # Response messages + FOOTER = '\n\n---\n\n^(I am a bot and this was an automated message. I am not responsible for the content neither am ' \ + 'I an author of this content. If you think this message is problematic, please contact developers mentioned ' \ + 'below.)\n\n^(Author: [u/mtj510](https://www.reddit.com/user/mtj510) | [how to use this bot](' \ + 'https://github.com/matej2/location-info/blob/master/README.md#example) | [github](' \ + 'https://github.com/matej2/location-info) ) ' + + NO_BODY = 'Location name not found in comment body.' + LOC_NOT_FOUND = 'No summary found for {}. Either unknown location or mistype.' + NOT_DETECTED = f'Did not detect any message. Please try again\n\n{FOOTER}' + + SPACE_REGEX = '\s+' NONE = 'None' @staticmethod def successfully_processed(city: str): @@ -8,3 +31,7 @@ def successfully_processed(city: str): :return: str """ return f'{city} successfully processed' + + @staticmethod + def body_regex(mention: str): + return f'{mention}\s*({Const.CITY_REGEX})' diff --git a/main.py b/main.py index d40e945..6d48691 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ from const import Const from models import LocationMeta -from replies import FOOTER, LOC_NOT_FOUND, get_response_message, NO_BODY +from replies import get_response_message user = 'LocationInfoBot' mention = f'u/{user}' @@ -23,19 +23,6 @@ username = os.environ.get('USERNAME') password = os.environ.get('PASS') -# Cities -CITY_REGEX = '.+$' - -NOT_CHAR = '\W+' -BODY_REGEX = f'{mention}\s*({CITY_REGEX})' -COMMA_REGEX = ',+' -SPECIAL_CHARS = '[^A-Za-z0-9\s,]' - -# TODO Add links - info about country -TRIGGER_PHARSE = 'location:' -TRIGGER_SUBREDDITS = 'naturephotography,AdventurePhotography,snow,UrbanExploring,Outdoors' -KEYWORD = 'Location:\s*([^.\n]+)' - def get_reddit_instance(): reddit = praw.Reddit(client_id=client_id, @@ -53,20 +40,20 @@ def get_reddit_instance(): def reply_to_comment(city: str, target_comment: Comment): if city is None: - comment = get_response_message(None, NO_BODY, Const.NONE) + new_comment = get_response_message(None, Const.NO_BODY, None) else: wiki_meta = get_location_meta(city) if wiki_meta is None: - comment = get_response_message(None, LOC_NOT_FOUND.format(city), Const.NONE) + new_comment = get_response_message(None, Const.LOC_NOT_FOUND.format(city), None) else: nearby_locations = get_nearby_locations(wiki_meta.lon, wiki_meta.lat) - comment = get_response_message(wiki_meta.title, wiki_meta.desc, nearby_locations) + new_comment = get_response_message(wiki_meta.title, wiki_meta.desc, nearby_locations) print(Const.successfully_processed(city)) try: - result_comment = target_comment.reply(comment) + result_comment = target_comment.reply(new_comment) except RedditAPIException as e: print(e) return result_comment.id @@ -75,7 +62,7 @@ def reply_to_comment(city: str, target_comment: Comment): def send_photo(city, photo): response = {} if city is None: - comment = get_response_message(None, NO_BODY, None) + comment = get_response_message(None, Const.NO_BODY, None) else: wiki_obj = get_location_meta(city) @@ -199,15 +186,15 @@ def process_inbox_by_keywords(): comment_results = list(gen) for comment in comment_results: - if TRIGGER_PHARSE in comment.title.lower(): + if Const.TRIGGER_PHARSE in comment.title.lower(): if comment.id == config.get(last_processed_key): return True # extract the word from the comment - body = re.search(KEYWORD, comment.title, flags=re.IGNORECASE) + body = re.search(Const.KEYWORD, comment.title, flags=re.IGNORECASE) if body is not None: - word = re.sub(SPECIAL_CHARS, '', body.group(1)).strip() + word = re.sub(Const.SPECIAL_CHARS, '', body.group(1)).strip() post = Submission(r, id=comment.id) if is_replied(post) is False: @@ -268,14 +255,14 @@ def main(): for item in inbox: if mention.lower() in item.body.lower(): text = item.body - result = re.search(BODY_REGEX, text, flags=re.IGNORECASE) + result = re.search(Const.body_regex(mention), text, flags=re.IGNORECASE) if result is not None: body = result.group(1) if reply_to_comment(body, item): item.mark_read() else: - item.reply(f'Did not detect any message. Please try again\n\n{FOOTER}') + item.reply(Const.NOT_DETECTED) sleep(10) @@ -285,14 +272,14 @@ def main_stream(): for item in reddit.inbox.stream(): if mention.lower() in item.body.lower(): text = item.body - result = re.search(BODY_REGEX, text, flags=re.IGNORECASE) + result = re.search(Const.body_regex(mention), text, flags=re.IGNORECASE) if result is not None: body = result.group(1) if reply_to_comment(body, item): item.mark_read() else: - item.reply(f'Did not detect any message. Please try again\n\n{FOOTER}') + item.reply(Const.NOT_DETECTED) sleep(10) @@ -302,7 +289,7 @@ def get_comments(): filtered_comments = [] for c in comments: - if LOC_NOT_FOUND not in c.body and 'I am a bot and this was an automated message' in c.body: + if Const.LOC_NOT_FOUND not in c.body and 'I am a bot and this was an automated message' in c.body: filtered_comments.append(c) return filtered_comments diff --git a/replies.py b/replies.py index d1666a8..2e167d8 100644 --- a/replies.py +++ b/replies.py @@ -1,5 +1,8 @@ import re import urllib +from typing import Optional + +from const import Const WIKI_URL = 'https://en.wikipedia.org/wiki/{}' VISIT_URL = 'https://www.visitacity.com/en/{}/activities/all-activities' @@ -9,38 +12,28 @@ TB_URL = 'https://www.tumblr.com/search/{}' PT_URL = 'https://www.pinterest.com/search/pins/?q={}' -FOOTER = '\n\n---\n\n^(I am a bot and this was an automated message. I am not responsible for the content neither am ' \ - 'I an author of this content. If you think this message is problematic, please contact developers mentioned ' \ - 'below.)\n\n^(Author: [u/mtj510](https://www.reddit.com/user/mtj510) | [how to use this bot](' \ - 'https://github.com/matej2/location-info/blob/master/README.md#example) | [github](' \ - 'https://github.com/matej2/location-info) ) ' -NO_BODY = 'Location name not found in comment body.' -LOC_NOT_FOUND = 'No summary found for {}. Either unknown location or mistype.' -SPACE_REGEX = '\s+' - - def get_visit_link(txt): - parsed = re.sub(SPACE_REGEX, '-', txt) + parsed = re.sub(Const.SPACE_REGEX, '-', txt) return VISIT_URL.format(parsed) def get_map_link(txt): - parsed = re.sub(SPACE_REGEX, '+', txt) + parsed = re.sub(Const.SPACE_REGEX, '+', txt) return MAPS_URL.format(parsed) def get_booking_url(txt): - parsed = re.sub(SPACE_REGEX, '+', txt) + parsed = re.sub(Const.SPACE_REGEX, '+', txt) return BOOKING_URL.format(parsed) def get_wander_url(txt): - parsed = re.sub(SPACE_REGEX, '+', txt) + parsed = re.sub(Const.SPACE_REGEX, '+', txt) return WANDER_URL.format(parsed) def get_th_url(txt): - parsed = re.sub(SPACE_REGEX, '+', txt) + parsed = re.sub(Const.SPACE_REGEX, '+', txt) return TB_URL.format(parsed) @@ -49,7 +42,7 @@ def get_pt_url(txt): return PT_URL.format(parsed) -def get_response_message(city, msg, nearby: str): +def get_response_message(city, msg, nearby: Optional[str]): if city is None: message = f''' {msg}