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

Commit

Permalink
Refactor news parsers for common patchparser
Browse files Browse the repository at this point in the history
  • Loading branch information
sco1 committed Oct 2, 2018
1 parent 3f59303 commit cb77e50
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 81 deletions.
27 changes: 10 additions & 17 deletions bot/cogs/mhw.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
class MHWNewsParser(NewsParser):
def __init__(self, bot):
super().__init__(bot)
self.parsername = "MHW News"
self.postchannelID = 478568995767713793
self.logJSONpath = Path('./log/postedMHWnews.JSON')
self.appID = 582010
self.officialaccount = "MHW_CAPCOM"

self._parsername = "MHW News"
self._loadconverter = URL
self._saveconverter = str
self._comparator = 'url'

async def getofficialnews(self, appID: int=None) -> typing.List:
"""
Expand All @@ -30,13 +34,13 @@ 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)} {self.parsername} 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.MHWnewsfilter(item, self.officialaccount)]

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

async def postpatchnotes(self, postobj: SteamNewsPost=None, channelID: int=None):
async def postembed(self, postobj: SteamNewsPost=None, channelID: int=None):
channelID = channelID if channelID is not None else self.postchannelID
if postobj is None:
raise ValueError("No postobj provided")
Expand All @@ -55,22 +59,11 @@ 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(f"A new {self.parsername} post has been released!", embed=postembed)
await postchannel.send(f"A new {self._parsername} post has been released!", embed=postembed)

async def patchcheck(self):
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.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)
await super().patchcheck(posts)

@staticmethod
def MHWnewsfilter(item: SteamNewsPost=None, officialaccount: str=None) -> bool:
Expand Down
49 changes: 18 additions & 31 deletions bot/cogs/overwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
class PatchGifParser(NewsParser):
def __init__(self, bot):
super().__init__(bot)
self.parsername = "OW GIF(s)"
self.postjsonURL = URL("https://www.reddit.com/user/itsjieyang/submitted.json")
self.postchannelID = 477916849879908386
# self.postchannelID = 477916849879908386
self.postchannelID = 417527786614554638
self.logJSONpath = Path('./log/postedGIFs.JSON')

self._parsername = "OW GIF(s)"
self._loadconverter = URL
self._saveconverter = str
self._comparator = 'contentURL'

async def getpatchgifs(self, jsonURL: URL=None):
"""
Return a list of RedditPost objects generated from Patch Notes submissions by /u/itsjieyang to /r/Overwatch
Expand All @@ -42,10 +47,10 @@ 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)} {self.parsername}")
logging.info(f"Found {len(patchposts)} {self._parsername}")
return patchposts

async def postpatchgif(self, postobj: RedditPost=None, channelID: int=None):
async def postembed(self, postobj: RedditPost=None, channelID: int=None):
channelID = channelID if channelID is not None else self.postchannelID

if postobj is None:
Expand All @@ -65,19 +70,8 @@ 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(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 {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)
await super().patchcheck(posts)

@staticmethod
def gfygif(inURL: typing.Union[str, URL]) -> URL:
Expand All @@ -95,12 +89,16 @@ def gfygif(inURL: typing.Union[str, URL]) -> URL:
class PatchNotesParser(NewsParser):
def __init__(self, bot):
super().__init__(bot)
self.parsername = "OW Patch(es)"
self.patchesURL = URL('https://playoverwatch.com/en-us/news/patch-notes/pc')
self.postchannelID = 477916849879908386
self.logJSONpath = Path('./log/postedOWpatches.JSON')

async def postpatchnotes(self, postobj: OWPatch=None, channelID: int=None):
self._parsername = "OW Patch(es)"
self._loadconverter = str
self._saveconverter = str
self._comparator = 'ver'

async def postembed(self, postobj: OWPatch=None, channelID: int=None):
channelID = channelID if channelID is not None else self.postchannelID
if postobj is None:
raise ValueError("No post object provided")
Expand All @@ -121,19 +119,8 @@ async def postpatchnotes(self, postobj: OWPatch=None, channelID: int=None):
await postchannel.send('A new Overwatch Patch has been released!', embed=postembed)

async def patchcheck(self):
logging.info("OW Patch check coroutine invoked")
self.loadposted(converter=str)

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 {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)
posts = await OWPatch.asyncfromURL(self.patchesURL)
await super().patchcheck(posts)


class OverwatchCommands:
Expand Down
27 changes: 10 additions & 17 deletions bot/cogs/rocketleague.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@
class RLNewsParser(NewsParser):
def __init__(self, bot):
super().__init__(bot)
self.parsername = "RL News"
self.postchannelID = 494682432688226316
self.logJSONpath = Path('./log/postedRLnews.JSON')
self.appID = 252950
self.psyonixstaff = ('dirkened', 'psyonix devin')

self._parsername = "RL News"
self._loadconverter = URL
self._saveconverter = str
self._comparator = 'url'

async def getofficialnews(self, appID: int=None) -> typing.List:
"""
Return a list of SteamNewsPost objects containing official Rocket League announcements
"""
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)} {self.parsername} 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 {self.parsername} post(s)")
logging.info(f"Found {len(officialnews)} official {self._parsername} post(s)")
return officialnews

async def postpatchnotes(self, postobj: SteamNewsPost=None, channelID: int=None):
async def postembed(self, postobj: SteamNewsPost=None, channelID: int=None):
channelID = channelID if channelID is not None else self.postchannelID
if postobj is None:
raise ValueError("No postobj provided")
Expand All @@ -53,22 +57,11 @@ 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(f"A new {self.parsername} post has been released!", embed=postembed)
await postchannel.send(f"A new {self._parsername} post has been released!", embed=postembed)

async def patchcheck(self):
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.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)
await super().patchcheck(posts)

@staticmethod
def RLnewsfilter(item: SteamNewsPost=None, psyonixstaff: typing.Tuple=None) -> bool:
Expand Down
46 changes: 30 additions & 16 deletions bot/models/NewsParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,44 @@ def __init__(self, bot):
self.bot = bot
self.postednews = []

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

if logJSONpath.exists():
with logJSONpath.open(mode='r') as fID:
savednews = [converter(post) for post in json.load(fID)]
self._parsername = None
self._loadconverter = None
self._saveconverter = None

def loadposted(self):
if self.logJSONpath.exists():
with self.logJSONpath.open(mode='r') as fID:
savednews = [self._loadconverter(post) for post in json.load(fID)]

if savednews:
self.postednews = savednews
logging.info(f"Loaded {len(self.postednews)} {self.parsername} from '{logJSONpath}'")
logging.info(f"Loaded {len(self.postednews)} {self._parsername} from '{self.logJSONpath}'")
else:
logging.info(f"No posted {self.parsername} found in JSON log")
logging.info(f"No posted {self._parsername} found in JSON log")
else:
logging.info(f"{self.parsername} log JSON does not yet exist")
logging.info(f"{self._parsername} log JSON does not yet exist")

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

def saveposted(self):
if self.postednews:
with logJSONpath.open(mode='w') as fID:
json.dump([converter(post) for post in self.postednews], fID)
logging.info(f"Saved {len(self.postednews)} {self.parsername} post(s)")
with self.logJSONpath.open(mode='w') as fID:
json.dump([self._saveconverter(post) for post in self.postednews], fID)
logging.info(f"Saved {len(self.postednews)} {self._parsername} post(s)")
else:
logging.info(f"No {self.parsername} to save")
logging.info(f"No {self._parsername} to save")

async def patchcheck(self, posts):
logging.info(f"{self._parsername} check coroutine invoked")
self.loadposted()

newposts = [post for post in posts if getattr(post, self._comparator) not in self.postednews]
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.postembed(post)
self.postednews.append(self._loadconverter(getattr(post, self._comparator)))

self.saveposted()

async def patchchecktimer(client, parsers: typing.Tuple=(), sleepseconds: int=3600):
await client.wait_until_ready()
Expand Down

0 comments on commit cb77e50

Please sign in to comment.