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

Commit

Permalink
Remove string URLs where possible, update type hinting
Browse files Browse the repository at this point in the history
Move getblizztrack to OWPatch
  • Loading branch information
sco1 committed Sep 19, 2018
1 parent 75f3bfe commit 29aa7a8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
42 changes: 23 additions & 19 deletions cogs/overwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
class PatchGifParser:
def __init__(self, bot):
self.bot = bot
self.postjsonURL = "https://www.reddit.com/user/itsjieyang/submitted.json"
self.postjsonURL = URL("https://www.reddit.com/user/itsjieyang/submitted.json")
self.postchannelID = 477916849879908386
self.logJSONpath = Path('./log/postedGIFs.JSON')
self.postedGIFs = []

async def getpatchgifs(self, jsonURL: str=None):
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 Down Expand Up @@ -80,12 +80,12 @@ async def patchcheck(self):
newposts = [post for post in posts if post.contentURL not in self.postedGIFs]
for post in reversed(newposts): # Attempt to get close to posting in chronological order
await self.postpatchgif(post)
self.postedGIFs.append(post.contentURL)
self.postedGIFs.append(post.contentURL.human_repr())

self.saveposted()

@staticmethod
def gfygif(inURL: str):
def gfygif(inURL: typing.Union[str, URL]) -> URL:
"""
Build a direct gif link from a gfycat URL
Expand All @@ -94,7 +94,7 @@ def gfygif(inURL: str):
Returns a string
"""
gfyID = URL(inURL).path.replace('/', '')
return URL.build(scheme="https", host="giant.gfycat.com", path=f"{gfyID}.gif").human_repr()
return URL.build(scheme="https", host="giant.gfycat.com", path=f"{gfyID}.gif")


class OWPatch():
Expand All @@ -114,7 +114,7 @@ def __repr__(self):
return f"OWPatch: v{self.ver}, Released: {datetime.strftime(self.patchdate, '%Y-%m-%d')}"

@staticmethod
def fromURL(inURL: str='https://playoverwatch.com/en-us/news/patch-notes/pc') -> typing.List:
def fromURL(inURL: typing.Union[str, URL]=URL('https://playoverwatch.com/en-us/news/patch-notes/pc')) -> typing.List:
"""
Return a list of OWPatch objects from Blizzard's Patch Notes
"""
Expand All @@ -127,7 +127,7 @@ def fromURL(inURL: str='https://playoverwatch.com/en-us/news/patch-notes/pc') ->
return OWPatch._parseOWpatchHTML(r)

@staticmethod
async def asyncfromURL(inURL: str='https://playoverwatch.com/en-us/news/patch-notes/pc') -> typing.List:
async def asyncfromURL(inURL: typing.Union[str, URL]=URL('https://playoverwatch.com/en-us/news/patch-notes/pc')) -> typing.List:
"""
This function is a coroutine
Expand Down Expand Up @@ -183,10 +183,23 @@ def _parseOWpatchHTML(inHTML: str) -> typing.List:
else:
patchbanner = None

patchobjs.append(OWPatch(patchref, ver, patchdate, PatchNotesParser.getblizztrack(patchref_num), patchbanner))
patchobjs.append(OWPatch(patchref, ver, patchdate, OWPatch.getblizztrack(patchref_num), patchbanner))

return patchobjs

@staticmethod
def getblizztrack(patchref: str=None) -> URL:
"""
Return BlizzTrack URL to patch notes, built using Blizzard's patchref
e.g. https://blizztrack.com/patch_notes/overwatch/50148
"""
if not patchref:
raise ValueError('No patch reference provided')

baseURL = URL('https://blizztrack.com/patch_notes/overwatch/')
return baseURL / patchref


class PatchNotesParser:
def __init__(self, bot):
Expand Down Expand Up @@ -240,19 +253,9 @@ async def patchcheck(self):
self.postedpatches.append(patch.ver)

self.saveposted()

@staticmethod
def getblizztrack(patchref:str) -> URL:
"""
Return BlizzTrack URL to patch notes, built using Blizzard's patchref
e.g. https://blizztrack.com/patch_notes/overwatch/50148
"""
baseURL = URL('https://blizztrack.com/patch_notes/overwatch/')
return baseURL / patchref


async def patchchecktimer(client, sleepseconds=3600):
async def patchchecktimer(client, sleepseconds: int=3600):
await client.wait_until_ready()
parsers = (PatchGifParser(client), PatchNotesParser(client))
while not client.is_closed():
Expand All @@ -261,6 +264,7 @@ async def patchchecktimer(client, sleepseconds=3600):

await asyncio.sleep(sleepseconds)


class OverwatchCommands:
def __init__(self, bot):
self.bot = bot
Expand Down
6 changes: 3 additions & 3 deletions cogs/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __repr__(self):
return f"{self.title}: {self.permalink}"

@staticmethod
def fromJSON(jsonURL: str=None) -> typing.List:
def fromJSON(jsonURL: typing.Union[str, URL]=None) -> typing.List:
"""
Return a list of RedditPost from an input Reddit JSON URL
Expand Down Expand Up @@ -67,7 +67,7 @@ def fromJSON(jsonURL: str=None) -> typing.List:
return [RedditPost(post) for post in postlist]

@staticmethod
def fromURL(inURL: str=None) -> typing.List:
def fromURL(inURL: typing.Union[str, URL]=None) -> typing.List:
"""
Return a list of RedditPost objects from an input Reddit URL
Expand All @@ -91,7 +91,7 @@ def fromURL(inURL: str=None) -> typing.List:
return RedditPost.fromJSON(inURL.join(URL('.json')))

@staticmethod
async def asyncfromJSON(jsonURL: str=None) -> typing.List:
async def asyncfromJSON(jsonURL: typing.Union[str, URL]=None) -> typing.List:
"""
This function is a coroutine
Expand Down
4 changes: 2 additions & 2 deletions docs/cogs/mhw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Parser Reference

Path to JSON storage file

.. attribute:: postedMHWnews(List[str])
.. attribute:: postedMHWnews(List[yarl.URL])

``List`` containing posted news posts

Patches are stored as Steam news permalinks, as ``str`` (e.g. ``'https://steamcommunity.com/games/582010/announcements/detail/1689302358462352379'``)
Patches are stored as Steam news permalinks, as ``yarl.URL`` (e.g. ``[URL('https://steamcommunity.com/games/582010/announcements/detail/1689302358462352379')]``)

.. attribute:: appID(int)

Expand Down
26 changes: 13 additions & 13 deletions docs/cogs/overwatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ Parser Reference
#. Check patch references against those previously posted
#. If new patch(es): Build embed, post to channel, and save the patch reference(s) to the local JSON log

.. staticmethod:: getblizztrack(patchref:str) -> yarl.URL

Build a ``yarl.URL`` object from a patch reference ID

.. code-block:: python3
>>> from cogs import overwatch
>>> patchURL = overwatch.PatchNotesParser.getblizztrack('50148')
>>> print(patchURL)
https://blizztrack.com/patch_notes/overwatch/50148
.. class:: overwatch.PatchNotesParser

Reddit user `/u/itsjieyang <https://reddit.com/u/itsjieyang>`_'s Overwatch Patch GIF Monitor
Expand All @@ -59,7 +48,7 @@ Parser Reference

Discord bot instance

.. attribute:: postjsonURL(yar.URL)
.. attribute:: postjsonURL(yarl.URL)

Reddit's JSON URL for /u/itsjieyang's submissions

Expand Down Expand Up @@ -87,7 +76,7 @@ Parser Reference
#. Check Gfycat URLs against those previously posted
#. If new patch GIF(s): Build embed, post to channel, and save the Gfycat permalink to the local JSON log

.. staticmethod:: gfygif(inURL: str) -> str
.. staticmethod:: gfygif(inURL: typing.Union[str, yarl.URL]) -> yarl.URL

Build a direct GIF link from a Gfycat URL

Expand Down Expand Up @@ -155,3 +144,14 @@ Class Reference
.. attribute:: bannerURL(yarl.URL)

Blizzard patch banner URL permalink

.. staticmethod:: getblizztrack(patchref:str) -> yarl.URL

Build a ``yarl.URL`` object from a patch reference ID

.. code-block:: python3
>>> from cogs import overwatch
>>> patchURL = overwatch.PatchNotesParser.getblizztrack('50148')
>>> print(patchURL)
https://blizztrack.com/patch_notes/overwatch/50148
6 changes: 3 additions & 3 deletions docs/cogs/reddit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Class Reference

Post creation date (UTC)

.. attribute:: contentURL(str)
.. attribute:: contentURL(yarl.URL)

Submission permalink

Expand All @@ -75,7 +75,7 @@ Class Reference
Other input URL formats are not supported

.. staticmethod:: fromURL(inURL: str=None) -> typing.List:
.. staticmethod:: fromURL(inURL: typing.Union[str, yarl.URL]=None) -> typing.List:

Return a list of ``reddit.RedditPost`` objects from an input Reddit URL

Expand All @@ -89,7 +89,7 @@ Class Reference
Other input URL formats are not supported

.. comethod:: asyncfromURL(inURL: str=None) -> typing.List:
.. comethod:: asyncfromURL(inURL: typing.Union[str, yarl.URL]=None) -> typing.List:
:staticmethod:

Return a list of ``reddit.RedditPost`` objects from an input Reddit URL
Expand Down

0 comments on commit 29aa7a8

Please sign in to comment.