Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
matej2 committed Jul 7, 2022
1 parent 9874537 commit 242ba8a
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 146 deletions.
179 changes: 107 additions & 72 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 4 additions & 74 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
import re
import urllib
from time import sleep

import mwparserfromhell
Expand All @@ -14,6 +13,7 @@
from psaw import PushshiftAPI

from models import LocationMeta
from replies import FOOTER, LOC_NOT_FOUND, get_response_message, NO_BODY

user = 'LocationInfoBot'
mention = f'u/{user}'
Expand All @@ -25,30 +25,12 @@
# Cities
CITY_REGEX = '.+$'

WIKI_URL = 'https://en.wikipedia.org/wiki/{}'
VISIT_URL = 'https://www.visitacity.com/en/{}/activities/all-activities'
MAPS_URL = 'https://www.google.com/maps/search/{}'
BOOKING_URL = 'https://www.booking.com/searchresults.sl.html?ss={}'
WANDER_URL = 'http://www.wandermap.net/sl/search/?q={}'
TB_URL = 'https://www.tumblr.com/search/{}'
PT_URL = 'https://www.pinterest.com/search/pins/?q={}'

# TODO Add links - info about country

SPACE_REGEX = '\s+'
NOT_CHAR = '\W+'
BODY_REGEX = f'{mention}\s*({CITY_REGEX})'
COMMA_REGEX = ',+'
SPECIAL_CHARS = '[^A-Za-z0-9\s,]'

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.'

# TODO Add links - info about country
TRIGGER_PHARSE = 'location:'
TRIGGER_SUBREDDITS = 'naturephotography,AdventurePhotography,snow,UrbanExploring,Outdoors'
KEYWORD = 'Location:\s*([^.\n]+)'
Expand All @@ -57,7 +39,7 @@
def get_reddit_instance():
reddit = praw.Reddit(client_id=client_id,
client_secret=client_secret,
user_agent='windows:github.com/matej2/location-info:v0.5 (by /u/mtj510)',
user_agent='windows:github.com/matej2/location-info:v0.6 (by /u/mtj510)',
username=username,
password=password)
if not reddit.read_only:
Expand All @@ -67,36 +49,6 @@ def get_reddit_instance():
return False


def get_visit_link(txt):
parsed = re.sub(SPACE_REGEX, '-', txt)
return VISIT_URL.format(parsed)


def get_map_link(txt):
parsed = re.sub(SPACE_REGEX, '+', txt)
return MAPS_URL.format(parsed)


def get_booking_url(txt):
parsed = re.sub(SPACE_REGEX, '+', txt)
return BOOKING_URL.format(parsed)


def get_wander_url(txt):
parsed = re.sub(SPACE_REGEX, '+', txt)
return WANDER_URL.format(parsed)


def get_th_url(txt):
parsed = re.sub(SPACE_REGEX, '+', txt)
return TB_URL.format(parsed)


def get_pt_url(txt):
parsed = urllib.parse.quote_plus(txt)
return PT_URL.format(parsed)


def send_link(city, where):

if city is None:
Expand Down Expand Up @@ -126,7 +78,6 @@ def get_location_meta(city):
if search is None:
return False

# Get first location
for result in search:
try:
page = wikipedia.page(title=result, auto_suggest=False)
Expand All @@ -153,27 +104,6 @@ def is_location(page):
return False


def get_response_message(city, msg, link, nearby):
if city is None:
message = f'''
{msg}
{FOOTER}
'''
else:
message = f'''
Information for location: {city}:\n\n {msg} \n\n
- locations/events nearby: {nearby}\n\n
- links: [wiki]({link})
~[map]({get_map_link(city)})
~ [hotels]({get_booking_url(city)})
~ [hiking]({get_wander_url(city)})
~ [thumblr]({get_th_url(city)})
~ [pinterest]({get_pt_url(city)})
{FOOTER}'''

return message


def get_nearby_locations(lon, lat):
loc_list = wikipedia.geosearch(lon, lat, results=10)
return ', '.join(loc_list)
Expand Down Expand Up @@ -249,7 +179,7 @@ def process_keywords():
else:
return True

if last_processed is '':
if last_processed == '':
last_processed = post.id
sleep(3)

Expand Down
Loading

0 comments on commit 242ba8a

Please sign in to comment.