| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://www.lofter.com/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, util |
| 11 |
|
| 12 |
|
| 13 |
class LofterExtractor(Extractor): |
| 14 |
"""Base class for lofter extractors""" |
| 15 |
category = "lofter" |
| 16 |
root = "https://www.lofter.com" |
| 17 |
directory_fmt = ("{category}", "{blog_name}") |
| 18 |
filename_fmt = "{id}_{num}.{extension}" |
| 19 |
archive_fmt = "{id}_{num}" |
| 20 |
|
| 21 |
def _init(self): |
| 22 |
self.api = LofterAPI(self) |
| 23 |
|
| 24 |
def items(self): |
| 25 |
for post in self.posts(): |
| 26 |
if post is None: |
| 27 |
continue |
| 28 |
if "post" in post: |
| 29 |
post = post["post"] |
| 30 |
|
| 31 |
post["blog_name"] = post["blogInfo"]["blogName"] |
| 32 |
post["date"] = self.parse_timestamp(post["publishTime"] / 1000) |
| 33 |
post_type = post["type"] |
| 34 |
|
| 35 |
# Article |
| 36 |
if post_type == 1: |
| 37 |
content = post["content"] |
| 38 |
image_urls = text.extract_iter(content, '<img src="', '"') |
| 39 |
image_urls = [text.unescape(x) for x in image_urls] |
| 40 |
image_urls = [x.partition("?")[0] for x in image_urls] |
| 41 |
|
| 42 |
# Photo |
| 43 |
elif post_type == 2: |
| 44 |
photo_links = util.json_loads(post["photoLinks"]) |
| 45 |
image_urls = [x["orign"] for x in photo_links] |
| 46 |
image_urls = [x.partition("?")[0] for x in image_urls] |
| 47 |
|
| 48 |
# Video |
| 49 |
elif post_type == 4: |
| 50 |
embed = util.json_loads(post["embed"]) |
| 51 |
image_urls = [embed["originUrl"]] |
| 52 |
|
| 53 |
# Answer |
| 54 |
elif post_type == 5: |
| 55 |
images = util.json_loads(post["images"]) |
| 56 |
image_urls = [x["orign"] for x in images] |
| 57 |
image_urls = [x.partition("?")[0] for x in image_urls] |
| 58 |
|
| 59 |
else: |
| 60 |
image_urls = () |
| 61 |
self.log.warning( |
| 62 |
"%s: Unsupported post type '%s'.", |
| 63 |
post["id"], post_type) |
| 64 |
|
| 65 |
post["count"] = len(image_urls) |
| 66 |
yield Message.Directory, "", post |
| 67 |
for post["num"], url in enumerate(image_urls, 1): |
| 68 |
yield Message.Url, url, text.nameext_from_url(url, post) |
| 69 |
|
| 70 |
def posts(self): |
| 71 |
return () |
| 72 |
|
| 73 |
|
| 74 |
class LofterPostExtractor(LofterExtractor): |
| 75 |
"""Extractor for a lofter post""" |
| 76 |
subcategory = "post" |
| 77 |
pattern = r"(?:https?://)?[\w-]+\.lofter\.com/post/([0-9a-f]+)_([0-9a-f]+)" |
| 78 |
example = "https://BLOG.lofter.com/post/12345678_90abcdef" |
| 79 |
|
| 80 |
def posts(self): |
| 81 |
blog_id, post_id = self.groups |
| 82 |
post = self.api.post(int(blog_id, 16), int(post_id, 16)) |
| 83 |
return (post,) |
| 84 |
|
| 85 |
|
| 86 |
class LofterBlogPostsExtractor(LofterExtractor): |
| 87 |
"""Extractor for a lofter blog's posts""" |
| 88 |
subcategory = "blog-posts" |
| 89 |
pattern = (r"(?:https?://)?(?:" |
| 90 |
# https://www.lofter.com/front/blog/home-page/<blog_name> |
| 91 |
r"www\.lofter\.com/front/blog/home-page/([\w-]+)|" |
| 92 |
# https://<blog_name>.lofter.com/ |
| 93 |
r"([\w-]+)\.lofter\.com" |
| 94 |
r")/?(?:$|\?|#)") |
| 95 |
example = "https://BLOG.lofter.com/" |
| 96 |
|
| 97 |
def posts(self): |
| 98 |
blog_name = self.groups[0] or self.groups[1] |
| 99 |
return self.api.blog_posts(blog_name) |
| 100 |
|
| 101 |
|
| 102 |
class LofterAPI(): |
| 103 |
|
| 104 |
def __init__(self, extractor): |
| 105 |
self.extractor = extractor |
| 106 |
|
| 107 |
def blog_posts(self, blog_name): |
| 108 |
endpoint = "/v2.0/blogHomePage.api" |
| 109 |
params = { |
| 110 |
"method": "getPostLists", |
| 111 |
"offset": 0, |
| 112 |
"limit": 200, |
| 113 |
"blogdomain": blog_name + ".lofter.com", |
| 114 |
} |
| 115 |
return self._pagination(endpoint, params) |
| 116 |
|
| 117 |
def post(self, blog_id, post_id): |
| 118 |
endpoint = "/oldapi/post/detail.api" |
| 119 |
params = { |
| 120 |
"targetblogid": blog_id, |
| 121 |
"postid": post_id, |
| 122 |
} |
| 123 |
return self._call(endpoint, params)["posts"][0] |
| 124 |
|
| 125 |
def _call(self, endpoint, data): |
| 126 |
url = "https://api.lofter.com" + endpoint |
| 127 |
params = { |
| 128 |
'product': 'lofter-android-7.9.10' |
| 129 |
} |
| 130 |
response = self.extractor.request( |
| 131 |
url, method="POST", params=params, data=data) |
| 132 |
info = response.json() |
| 133 |
|
| 134 |
if info["meta"]["status"] == 4200: |
| 135 |
raise self.extractor.exc.NotFoundError("blog") |
| 136 |
|
| 137 |
if info["meta"]["status"] != 200: |
| 138 |
self.extractor.log.debug("Server response: %s", info) |
| 139 |
raise self.extractor.exc.AbortExtraction("API request failed") |
| 140 |
|
| 141 |
return info["response"] |
| 142 |
|
| 143 |
def _pagination(self, endpoint, params): |
| 144 |
while True: |
| 145 |
data = self._call(endpoint, params) |
| 146 |
posts = data["posts"] |
| 147 |
|
| 148 |
yield from posts |
| 149 |
|
| 150 |
if data["offset"] < 0: |
| 151 |
break |
| 152 |
|
| 153 |
if params["offset"] + len(posts) < data["offset"]: |
| 154 |
break |
| 155 |
params["offset"] = data["offset"] |