Skip to content
This repository has been archived by the owner on Nov 24, 2019. It is now read-only.

Commit

Permalink
Refactor MHW, RL to use parser helpers
Browse files Browse the repository at this point in the history
Adjust MHW, RL, and OW logging statements to use the parsername
  • Loading branch information
sco1 committed Sep 30, 2018
1 parent bb93e83 commit cd8604c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 113 deletions.
72 changes: 21 additions & 51 deletions bot/cogs/mhw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import json
import logging
import typing
Expand All @@ -10,16 +9,17 @@
from discord.ext import commands
from yarl import URL

from bot.models.ManualCheck import ManualCheck
from bot.models.NewsParser import NewsParser
from bot.models.Steam import SteamNewsPost
from bot.utils import Helpers


class MHWNewsParser:
class MHWNewsParser(NewsParser):
def __init__(self, bot):
self.bot = bot
super().__init__(bot)
self.parsername = "MHW News"
self.postchannelID = 478568995767713793
self.logJSONpath = Path('./log/postedMHWnews.JSON')
self.postedMHWnews = []
self.appID = 582010
self.officialaccount = "MHW_CAPCOM"

Expand All @@ -30,10 +30,10 @@ async def getofficialnews(self, appID: int=None) -> typing.List:
appID = appID if appID is not None else self.appID

news = await SteamNewsPost.asyncgetnewsforapp(appID=appID, count=15, maxlength=600)
logging.info(f"{len(news)} MHW news posts returned by Steam's API")
logging.info(f"{len(news)} {self.parsername} post(s) returned by Steam's API")
officialnews = [item for item in news if self.MHWnewsfilter(item, self.officialaccount)]

logging.info(f"Found {len(officialnews)} official MHW news posts")
logging.info(f"Found {len(officialnews)} official {self.parsername} post(s)")
return officialnews

async def postpatchnotes(self, postobj: SteamNewsPost=None, channelID: int=None):
Expand All @@ -55,45 +55,22 @@ async def postpatchnotes(self, postobj: SteamNewsPost=None, channelID: int=None)
postembed.set_footer(text="Brought to you by Palico power!",
icon_url=URL("https://cdn.discordapp.com/attachments/417527786614554638/487788870193381387/s-l300.png")
)
await postchannel.send('A new MHW news post has been released!', embed=postembed)

def loadposted(self, logJSONpath: Path=None):
logJSONpath = logJSONpath if logJSONpath is not None else self.logJSONpath

if logJSONpath.exists():
with logJSONpath.open(mode='r') as fID:
savednewsposts = [URL(urlstr) for urlstr in json.load(fID)]

if savednewsposts:
self.postedMHWnews = savednewsposts
logging.info(f"Loaded {len(self.postedMHWnews)} MHW news post(s) from '{logJSONpath}'")
else:
logging.info(f"No posted MHW news posts found in JSON log")
else:
logging.info(f"MHW news post JSON log does not yet exist")

def saveposted(self, logJSONpath: Path=None):
logJSONpath = logJSONpath if logJSONpath is not None else self.logJSONpath

if self.postedMHWnews:
with logJSONpath.open(mode='w') as fID:
json.dump([str(url) for url in self.postedMHWnews], fID)
logging.info(f"Saved {len(self.postedMHWnews)} MHW news post(s)")
else:
logging.info("No MHW news posts to save")
await postchannel.send(f"A new {self.parsername} post has been released!", embed=postembed)

async def patchcheck(self):
logging.info("MHW News check coroutine invoked")
self.loadposted()
logging.info(f"{self.parsername} check coroutine invoked")
self.loadposted(converter=URL)

posts = await self.getofficialnews()
newposts = [post for post in posts if post.url not in self.postedMHWnews]
logging.info(f"Found {len(newposts)} unposted MHW news posts")
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchnotes(post)
self.postedMHWnews.append(post.url)

self.saveposted()
newposts = [post for post in posts if post.url not in self.postednews]
logging.info(f"Found {len(newposts)} unposted {self.parsername} posts")

if newposts:
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchnotes(post)
self.postednews.append(post.url)

self.saveposted(converter=str)

@staticmethod
def MHWnewsfilter(item: SteamNewsPost=None, officialaccount: str=None) -> bool:
Expand All @@ -114,15 +91,8 @@ def __init__(self, bot):

@commands.command()
async def checkMHWpatch(self, ctx: commands.Context):
if Helpers.isDM(ctx.message.channel) and Helpers.isOwner(ctx.message.author):
logging.info(f'Manual MHW news check initiated by {ctx.message.author}')
await ctx.send("Manual MHW news parsing starting now...")
await MHWNewsParser(self.bot).patchcheck()
elif Helpers.isOwner(ctx.message.author) and not Helpers.isDM(ctx.message.channel):
await ctx.send(f'{ctx.message.author.mention}, this command only works in a DM')
else:
logging.info(f'Manual MHW news check attempted by {ctx.message.author}')
await ctx.send(f'{ctx.message.author.mention}, you are not authorized to perform this operation')
await ManualCheck.check(ctx=ctx, toinvoke=MHWNewsParser(self.bot).patchcheck, commandstr='MHW news')


def setup(bot):
bot.add_cog(MHWCommands(bot))
23 changes: 12 additions & 11 deletions bot/cogs/overwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from bot.models.NewsParser import NewsParser
from bot.models.Overwatch import OWPatch
from bot.models.Reddit import RedditJSON, RedditPost, RedditPRAW
from bot.utils import Helpers


class PatchGifParser(NewsParser):
Expand Down Expand Up @@ -43,7 +42,7 @@ async def getpatchgifs(self, jsonURL: URL=None):
if postobj.subreddit == 'Overwatch' and 'patch' in postobj.title.lower():
patchposts.append(postobj)

logging.info(f"Found {len(patchposts)} OW Patch GIF post(s)")
logging.info(f"Found {len(patchposts)} {self.parsername}")
return patchposts

async def postpatchgif(self, postobj: RedditPost=None, channelID: int=None):
Expand All @@ -66,17 +65,18 @@ async def postpatchgif(self, postobj: RedditPost=None, channelID: int=None):
await postchannel.send('A new patch gif has been posted!', embed=postembed)

async def patchcheck(self):
logging.info("OW patch GIF check coroutine invoked")
logging.info(f"{self.parsername} check coroutine invoked")
self.loadposted(converter=URL)

posts = await self.getpatchgifs()
newposts = [post for post in posts if post.contentURL not in self.postednews]
logging.info(f"Found {len(newposts)} new GIF(s) to post")
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchgif(post)
self.postednews.append(post.contentURL.human_repr())
logging.info(f"Found {len(newposts)} new {self.parsername} to post")

if newposts:
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchgif(post)
self.postednews.append(post.contentURL.human_repr())

self.saveposted(converter=str)

@staticmethod
Expand Down Expand Up @@ -126,12 +126,13 @@ async def patchcheck(self):

patches = await OWPatch.asyncfromURL(self.patchesURL)
newpatches = [patch for patch in patches if patch.ver not in self.postednews]
logging.info(f"Found {len(newpatches)} new OW patch(es) to post")
for patch in reversed(newpatches): # Attempt to get close to posting in chronological order
await self.postpatchnotes(patch)
self.postednews.append(patch.ver)
logging.info(f"Found {len(newpatches)} new {self.parsername} to post")

if newpatches:
for patch in reversed(newpatches): # Attempt to get close to posting in chronological order
await self.postpatchnotes(patch)
self.postednews.append(patch.ver)

self.saveposted(converter=str)


Expand Down
72 changes: 21 additions & 51 deletions bot/cogs/rocketleague.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import json
import logging
import typing
Expand All @@ -8,16 +7,17 @@
from discord.ext import commands
from yarl import URL

from bot.models.ManualCheck import ManualCheck
from bot.models.NewsParser import NewsParser
from bot.models.Steam import SteamNewsPost
from bot.utils import Helpers


class RLNewsParser:
class RLNewsParser(NewsParser):
def __init__(self, bot):
self.bot = bot
super().__init__(bot)
self.parsername = "RL News"
self.postchannelID = 494682432688226316
self.logJSONpath = Path('./log/postedRLnews.JSON')
self.postedRLnews = []
self.appID = 252950
self.psyonixstaff = ('dirkened', 'psyonix devin')

Expand All @@ -28,10 +28,10 @@ async def getofficialnews(self, appID: int=None) -> typing.List:
appID = appID if appID is not None else self.appID

news = await SteamNewsPost.asyncgetnewsforapp(appID=appID, count=15, maxlength=600)
logging.info(f"{len(news)} RL news post(s) returned by Steam's API")
logging.info(f"{len(news)} {self.parsername} post(s) returned by Steam's API")
officialnews = [item for item in news if self.RLnewsfilter(item, self.psyonixstaff)]

logging.info(f"Found {len(officialnews)} official RL news posts")
logging.info(f"Found {len(officialnews)} official {self.parsername} post(s)")
return officialnews

async def postpatchnotes(self, postobj: SteamNewsPost=None, channelID: int=None):
Expand All @@ -53,45 +53,22 @@ async def postpatchnotes(self, postobj: SteamNewsPost=None, channelID: int=None)
postembed.set_footer(text="What a save! What a save! What a save!",
icon_url=URL("https://cdn.discordapp.com/attachments/417527786614554638/494690857379692564/unknown.png")
)
await postchannel.send('A new RL news post has been released!', embed=postembed)

def loadposted(self, logJSONpath: Path=None):
logJSONpath = logJSONpath if logJSONpath is not None else self.logJSONpath

if logJSONpath.exists():
with logJSONpath.open(mode='r') as fID:
savednewsposts = [URL(urlstr) for urlstr in json.load(fID)]

if savednewsposts:
self.postedRLnews = savednewsposts
logging.info(f"Loaded {len(self.postedRLnews)} RL news post(s) from '{logJSONpath}'")
else:
logging.info(f"No posted RL news posts found in JSON log")
else:
logging.info(f"RL news post JSON log does not yet exist")

def saveposted(self, logJSONpath: Path=None):
logJSONpath = logJSONpath if logJSONpath is not None else self.logJSONpath

if self.postedRLnews:
with logJSONpath.open(mode='w') as fID:
json.dump([str(url) for url in self.postedRLnews], fID)
logging.info(f"Saved {len(self.postedRLnews)} RL news post(s)")
else:
logging.info("No RL news posts to save")
await postchannel.send(f"A new {self.parsername} post has been released!", embed=postembed)

async def patchcheck(self):
logging.info("RL News check coroutine invoked")
self.loadposted()
logging.info(f"{self.parsername} check coroutine invoked")
self.loadposted(converter=URL)

posts = await self.getofficialnews()
newposts = [post for post in posts if post.url not in self.postedRLnews]
logging.info(f"Found {len(newposts)} unposted RL news posts")
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchnotes(post)
self.postedRLnews.append(post.url)

self.saveposted()
newposts = [post for post in posts if post.url not in self.postednews]
logging.info(f"Found {len(newposts)} unposted {self.parsername} post(s)")

if newposts:
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchnotes(post)
self.postednews.append(post.url)

self.saveposted(converter=str)

@staticmethod
def RLnewsfilter(item: SteamNewsPost=None, psyonixstaff: typing.Tuple=None) -> bool:
Expand All @@ -112,15 +89,8 @@ def __init__(self, bot):

@commands.command()
async def checkRLpatch(self, ctx: commands.Context):
if Helpers.isDM(ctx.message.channel) and Helpers.isOwner(ctx.message.author):
logging.info(f'Manual RL news check initiated by {ctx.message.author}')
await ctx.send("Manual RL news parsing starting now...")
await RLNewsParser(self.bot).patchcheck()
elif Helpers.isOwner(ctx.message.author) and not Helpers.isDM(ctx.message.channel):
await ctx.send(f'{ctx.message.author.mention}, this command only works in a DM')
else:
logging.info(f'Manual RL news check attempted by {ctx.message.author}')
await ctx.send(f'{ctx.message.author.mention}, you are not authorized to perform this operation')
await ManualCheck.check(ctx=ctx, toinvoke=RLNewsParser(self.bot).patchcheck, commandstr='RL news')


def setup(bot):
bot.add_cog(RocketLeagueCommands(bot))

0 comments on commit cd8604c

Please sign in to comment.