Skip to content

Commit

Permalink
Add support for FanFiction author links
Browse files Browse the repository at this point in the history
  • Loading branch information
adorabilis committed Oct 17, 2019
1 parent c10b5df commit 51ea626
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

| Cog | Version | Description |
| --- | --- | --- |
| ffembed | 1.0.14 | Automatically show FanFiction, AO3, and SIYE story info in an embed message when a supported link is detected. (Commissioned by marclapin#0812) |
| ffpicker | 1.1.12 | Allow saving and retrieval of FanFiction, AO3, and SIYE stories to and from a curated collection. (Commissioned by marclapin#0812) |
| ffembed | 1.1.0 | Automatically show FanFiction, AO3, and SIYE story info in an embed message when a supported link is detected. (Commissioned by marclapin#0812) |
| ffpicker | 1.1.13 | Allow saving and retrieval of FanFiction, AO3, and SIYE stories to and from a curated collection. (Commissioned by marclapin#0812) |

## Installation

Expand Down
27 changes: 24 additions & 3 deletions ffembed/ffembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bs4 import BeautifulSoup
from redbot.core import checks, commands, Config

__version__ = "1.0.15"
__version__ = "1.1.0"

BaseCog = getattr(commands, "Cog", object)

Expand Down Expand Up @@ -108,7 +108,7 @@ async def toggle_channel(self, ctx, channel: discord.TextChannel):
def parse_url(self, message):
url_regex = (
r"https?://(?:www.)?(?:(?:m.)?fanfiction.net/"
r"s/\d+/?(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),])*|"
r"(?:(?:(?:s|u)/\d+/?)|~)(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),])*|"
r"archiveofourown.org/works/\d+(?:/chapters/\d+)?|"
r"siye.co.uk/(?:siye/)?viewstory.php\?sid=\d+(?:&chapter=\d+)?)"
)
Expand All @@ -133,6 +133,24 @@ async def fetch_url(self, url):
page = BeautifulSoup(html, "html.parser")
return page

def parse_FanFiction_author(self, page, url):
div = page.find(id="content_wrapper_inner")
thumbnail = div.find(id="bio").img
author = div.span
desc = page.find("meta", attrs={"name": "description"})['content']
footer = div.find_all("td")[2].get_text().replace("id", "ID")
footer = footer[:6] + ":" + footer[6:]
return {
"link": None,
"icon": "https://i.imgur.com/0eUBQHu.png",
"thumbnail": "https:" + thumbnail['data-original'] if thumbnail else None,
"author": author.get_text(strip=True),
"author_link": url,
"title": None,
"desc": desc,
"footer": " ∙ ".join(footer.split(", ")),
}

def parse_FanFiction(self, page, url):
base = "https://fanfiction.net"
div = page.find(id="profile_top")
Expand Down Expand Up @@ -197,7 +215,10 @@ def parse_SIYE(self, page, url):

def parse(self, page, url):
if "fanfiction" in url:
return self.parse_FanFiction(page, url)
if "/s/" in url:
return self.parse_FanFiction(page, url)
else:
return self.parse_FanFiction_author(page, url)
elif "archiveofourown" in url:
return self.parse_AO3(page, url)
elif "siye" in url:
Expand Down

0 comments on commit 51ea626

Please sign in to comment.