Skip to content

Commit

Permalink
Refactor code - create new methods for reddit client, new consts, rep…
Browse files Browse the repository at this point in the history
…ly templates
  • Loading branch information
matej2 committed Jan 16, 2025
1 parent ce86a30 commit 0929c6a
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 325 deletions.
74 changes: 74 additions & 0 deletions RedditUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import re

import praw
from praw.exceptions import RedditAPIException
from praw.models import Submission, Comment
from psaw import PushshiftAPI

from WikiClient import WikiClient
from const import Const
from replies import get_response_message


class RedditUtils:
client_id = os.environ.get('CLIENT_ID')
client_secret = os.environ.get('CLIENT_SECRET')
username = os.environ.get('USERNAME')
password = os.environ.get('PASS')

def __init__(self):
self.reddit = praw.Reddit(client_id=self.client_id,
client_secret=self.client_secret,
user_agent='windows:github.com/matej2/location-info:v0.6 (by /u/mtj510)',
username=self.username,
password=self.password)
if not self.reddit.read_only:
print("Connected and running.")

def get_meta_post(self):
"""
Get meta post
:return:
praw.models.Submission: A meta post
"""
api = PushshiftAPI()
sub = self.reddit.subreddit('u_LocationInfoBot')

gen = api.search_submissions(filter=['title', 'selftext', 'url'], limit=20, q='meta', author=Const.user)
result = list(gen)

if result == [] or result is None or result[0].selftext == '':
post = sub.submit(title='meta', selftext='{ "status": "created" }')
else:
post = Submission(self.reddit, url=result[0].url)
return post

@staticmethod
def reply_to_comment(city: str, target_comment: Comment):

if city is None:
new_comment = get_response_message(None, Const.NO_BODY, None)
else:
wiki_meta = WikiClient.get_location_meta(city)

if wiki_meta is None:
new_comment = get_response_message(None, Const.LOC_NOT_FOUND.format(city), None)
else:
nearby_locations = WikiClient.get_nearby_locations(wiki_meta.lon, wiki_meta.lat)
new_comment = get_response_message(wiki_meta.title, wiki_meta.desc, nearby_locations)

print(Const.successfully_processed(city))

try:
result_comment = target_comment.reply(new_comment)
except RedditAPIException as e:
print(e)
return result_comment.id

def get_location_from_comment(c):
result = re.search('Information for location:\s*(.*):$', c.body, flags=re.IGNORECASE | re.MULTILINE)
if result is not None:
return result.group(1)
else:
return None
80 changes: 80 additions & 0 deletions WikiClient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import json
import re

import mwparserfromhell
import requests
import wikipedia
from mwparserfromhell.nodes.extras import Parameter

from models import LocationMeta


class WikiClient:
@staticmethod
def get_location_meta(city):
search = wikipedia.search(city)
st = 0

if search is None:
return False

for result in search:
try:
page = wikipedia.page(title=result, auto_suggest=False)
except wikipedia.DisambiguationError:
return None
except wikipedia.PageError:
return None

if WikiClient.is_location(page):
summary = wikipedia.summary(page.title, sentences=3, auto_suggest=False)
return LocationMeta(page.title, summary, page.coordinates[0], page.coordinates[1], page.url)

if st > 3:
return None
st = st + 1

return None

@staticmethod
def is_location(page):
for attr in page.categories:
if attr == 'Coordinates on Wikidata':
return True
return False

@staticmethod
def get_nearby_locations(lon, lat):
loc_list = wikipedia.geosearch(lon, lat, results=10)
return ', '.join(loc_list)

# See https://stackoverflow.com/a/33336820/10538678
@staticmethod
def get_taxonomy(title):
infobox = None
parsed_params = []
a = ''

r = requests.get(
'https://en.wikipedia.org/w/api.php?action=query&titles=' + title + '&prop=revisions&rvprop=content&rvsection'
'=0&format=json')
t = json.loads(r.text)

for i in t['query']['pages']:
a = t['query']['pages'][i]['revisions'][0]['*']

template_list = mwparserfromhell.parse(a).filter_templates()
for template in template_list:
if 'Infobox' in template.name:
infobox = template

if infobox is None:
return None
else:
for param in infobox.params:
add_par = Parameter(
name=param.name.strip(),
value=re.sub('\\n', '', param.value.strip())
)
parsed_params.append(add_par)
return parsed_params
5 changes: 5 additions & 0 deletions const.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Const:

SPACE_REGEX = '\s+'
NONE = 'None'

# Reddit meta
user = 'LocationInfoBot'
mention = f'u/{user}'

@staticmethod
def successfully_processed(city: str):
"""
Expand Down
Loading

0 comments on commit 0929c6a

Please sign in to comment.