From 75aedf8601e39fded2404974909ce34d72080d5a Mon Sep 17 00:00:00 2001 From: "Wayne.S.Lui" Date: Tue, 30 Aug 2022 22:46:40 +0800 Subject: [PATCH] Update fanza.py Update to get trailer, extrafanarts and cover, using the method from older version --- scrapinglib/fanza.py | 54 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/scrapinglib/fanza.py b/scrapinglib/fanza.py index 2706b91..0ccf285 100644 --- a/scrapinglib/fanza.py +++ b/scrapinglib/fanza.py @@ -11,8 +11,8 @@ class Fanza(Parser): expr_title = '//*[starts-with(@id, "title")]/text()' expr_actor = "//td[contains(text(),'出演者')]/following-sibling::td/span/a/text()" - expr_cover = './/head/meta[@property="og:image"]/@content' - expr_extrafanart = '//a[@name="sample-image"]/img/@src' + # expr_cover = './/head/meta[@property="og:image"]/@content' + # expr_extrafanart = '//a[@name="sample-image"]/img/@src' expr_outline = "//div[@class='mg-b20 lh4']/text()" expr_outline2 = "//div[@class='mg-b20 lh4']//p/text()" expr_outline_og = '//head/meta[@property="og:description"]/@content' @@ -119,6 +119,56 @@ class Fanza(Parser): if ret == "----": return '' return ret + + def getCover(self, htmltree): + cover_number = self.number + try: + result = htmltree.xpath('//*[@id="' + cover_number + '"]/@href')[0] + except: + # sometimes fanza modify _ to \u0005f for image id + if "_" in cover_number: + cover_number = cover_number.replace("_", r"\u005f") + try: + result = htmltree.xpath('//*[@id="' + cover_number + '"]/@href')[0] + except: + # (TODO) handle more edge case + # print(html) + # raise exception here, same behavior as before + # people's major requirement is fetching the picture + raise ValueError("can not find image") + return result + + def getExtrafanart(self, htmltree): + html_parent = re.compile(r'
\s*?') + html = html_parent.search( + self.htmlcode) + if html: + html = html.group() + extrafanart_parent = re.compile(r'[\s\S].*}\s*?') + html = html_parent.search( + self.htmlcode) + if html: + html = html.group() + trailer_parent = re.compile(r'\"contentUrl\":\"(.*?)\"') + trailer_url = trailer_parent.search(html) + if trailer_url: + trailer_url = trailer_url.group(1) + trailer_cuts = trailer_url.rsplit('_', 2) + trailer_url = trailer_cuts[0] + '_mhb_w.mp4' + return trailer_url + return '' def getFanzaString(self, expr): result1 = str(self.htmltree.xpath("//td[contains(text(),'"+expr+"')]/following-sibling::td/a/text()")).strip(" ['']")