Skip to content

Commit

Permalink
Moved strings to const class
Browse files Browse the repository at this point in the history
  • Loading branch information
matej2 committed Jan 15, 2025
1 parent e485b01 commit ce86a30
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
27 changes: 27 additions & 0 deletions const.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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})'
41 changes: 14 additions & 27 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand All @@ -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

Expand Down
25 changes: 9 additions & 16 deletions replies.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)


Expand All @@ -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}
Expand Down

0 comments on commit ce86a30

Please sign in to comment.