| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for http://blog.livedoor.jp/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class LivedoorExtractor(Extractor): |
| 16 |
"""Base class for livedoor extractors""" |
| 17 |
category = "livedoor" |
| 18 |
root = "http://blog.livedoor.jp" |
| 19 |
filename_fmt = "{post[id]}_{post[title]}_{num:>02}.{extension}" |
| 20 |
directory_fmt = ("{category}", "{post[user]}") |
| 21 |
archive_fmt = "{post[id]}_{hash}" |
| 22 |
|
| 23 |
def __init__(self, match): |
| 24 |
Extractor.__init__(self, match) |
| 25 |
self.user = match[1] |
| 26 |
|
| 27 |
def items(self): |
| 28 |
for post in self.posts(): |
| 29 |
if images := self._images(post): |
| 30 |
yield Message.Directory, "", {"post": post} |
| 31 |
for image in images: |
| 32 |
yield Message.Url, image["url"], image |
| 33 |
|
| 34 |
def posts(self): |
| 35 |
"""Return an iterable with post objects""" |
| 36 |
|
| 37 |
def _load(self, data, body): |
| 38 |
extr = text.extract_from(data) |
| 39 |
tags = text.extr(body, 'class="article-tags">', '</dl>') |
| 40 |
about = extr('rdf:about="', '"') |
| 41 |
|
| 42 |
return { |
| 43 |
"id" : text.parse_int( |
| 44 |
about.rpartition("/")[2].partition(".")[0]), |
| 45 |
"title" : text.unescape(extr('dc:title="', '"')), |
| 46 |
"categories" : extr('dc:subject="', '"').partition(",")[::2], |
| 47 |
"description": extr('dc:description="', '"'), |
| 48 |
"date" : self.parse_datetime_iso(extr('dc:date="', '"')), |
| 49 |
"tags" : text.split_html(tags)[1:] if tags else [], |
| 50 |
"user" : self.user, |
| 51 |
"body" : body, |
| 52 |
} |
| 53 |
|
| 54 |
def _images(self, post): |
| 55 |
imgs = [] |
| 56 |
body = post.pop("body") |
| 57 |
|
| 58 |
for num, img in enumerate(text.extract_iter(body, "<img ", ">"), 1): |
| 59 |
src = text.extr(img, 'src="', '"') |
| 60 |
alt = text.extr(img, 'alt="', '"') |
| 61 |
|
| 62 |
if not src: |
| 63 |
continue |
| 64 |
if "://livedoor.blogimg.jp/" in src: |
| 65 |
url = src.replace("http:", "https:", 1).replace("-s.", ".") |
| 66 |
else: |
| 67 |
url = text.urljoin(self.root, src) |
| 68 |
name, _, ext = url.rpartition("/")[2].rpartition(".") |
| 69 |
|
| 70 |
imgs.append({ |
| 71 |
"url" : url, |
| 72 |
"num" : num, |
| 73 |
"hash" : name, |
| 74 |
"filename" : alt or name, |
| 75 |
"extension": ext, |
| 76 |
"post" : post, |
| 77 |
}) |
| 78 |
|
| 79 |
return imgs |
| 80 |
|
| 81 |
|
| 82 |
class LivedoorBlogExtractor(LivedoorExtractor): |
| 83 |
"""Extractor for a user's blog on blog.livedoor.jp""" |
| 84 |
subcategory = "blog" |
| 85 |
pattern = r"(?:https?://)?blog\.livedoor\.jp/(\w+)/?(?:$|[?#])" |
| 86 |
example = "http://blog.livedoor.jp/USER/" |
| 87 |
|
| 88 |
def posts(self): |
| 89 |
url = f"{self.root}/{self.user}" |
| 90 |
while url: |
| 91 |
extr = text.extract_from(self.request(url).text) |
| 92 |
while True: |
| 93 |
data = extr('<rdf:RDF', '</rdf:RDF>') |
| 94 |
if not data: |
| 95 |
break |
| 96 |
body = extr('class="article-body-inner">', |
| 97 |
'class="article-footer">') |
| 98 |
yield self._load(data, body) |
| 99 |
url = extr('<a rel="next" href="', '"') |
| 100 |
|
| 101 |
|
| 102 |
class LivedoorPostExtractor(LivedoorExtractor): |
| 103 |
"""Extractor for images from a blog post on blog.livedoor.jp""" |
| 104 |
subcategory = "post" |
| 105 |
pattern = r"(?:https?://)?blog\.livedoor\.jp/(\w+)/archives/(\d+)" |
| 106 |
example = "http://blog.livedoor.jp/USER/archives/12345.html" |
| 107 |
|
| 108 |
def __init__(self, match): |
| 109 |
LivedoorExtractor.__init__(self, match) |
| 110 |
self.post_id = match[2] |
| 111 |
|
| 112 |
def posts(self): |
| 113 |
url = f"{self.root}/{self.user}/archives/{self.post_id}.html" |
| 114 |
extr = text.extract_from(self.request(url).text) |
| 115 |
data = extr('<rdf:RDF', '</rdf:RDF>') |
| 116 |
body = extr('class="article-body-inner">', 'class="article-footer">') |
| 117 |
return (self._load(data, body),) |