Created
October 5, 2021 12:31
-
-
Save SaidBySolo/75dceb812feaabac71e52f5297efed6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
# title: Hiyobi.me 북마크 다운로드 스크립트 | |
# comment: Hiyobi.me 북마크를 다운로드합니다 | |
# author: SaidBySolo | |
""" | |
MIT License | |
Copyright (c) 2020 Saebasol | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" | |
from io import BytesIO | |
import requests | |
import time | |
from utils import Downloader | |
from translator import tr_ | |
@Downloader.register | |
class DownloaderHiyobiBookmark(Downloader): | |
type = "hiyobibookmark" | |
def init(self) -> None: | |
self.url: str = self.url.replace("hiyobibookmark_", "") | |
self.bookmark_info_list: list = [] | |
self.username: str = "User" | |
def read(self) -> None: | |
account_info: list = self.url.split("/") | |
if not len(account_info) == 2: | |
return self.Invalid("이메일/비밀번호 형식으로 작성해주세요.") | |
email = account_info[0] | |
password = account_info[1] | |
result = self.main(email, password) | |
if result: | |
return result | |
f = BytesIO() | |
f.write("\n".join(self.bookmark_info_list).encode("UTF-8")) | |
f.seek(0) | |
self.title = self.username | |
self.urls.append(f) | |
self.filenames[f] = "bookmark.txt" | |
def main(self, email: str, password: str): | |
token_or_errorMsg = self.post_account_info(email, password) | |
if not isinstance(token_or_errorMsg, str): | |
return token_or_errorMsg | |
bookmark_info = self.post_hiyobi_bookmark(token_or_errorMsg) | |
bookmark_total_count: int = bookmark_info["count"] | |
if bookmark_total_count == 0: | |
return self.Invalid("북마크된 정보가 없는거같아요.") | |
count = ( | |
round(bookmark_total_count / 15) + 1 | |
if not (bookmark_total_count / 15).is_integer() | |
else round(bookmark_total_count / 15) | |
) + 1 | |
self.add_in_bookmark_info_list(count, token_or_errorMsg) | |
def add_in_bookmark_info_list(self, count: int, token: str) -> None: | |
for paging in range(1, count): | |
self.title = tr_("총 {}페이지 중 {}번 페이지 읽는 중...").format(count - 1, paging) | |
paged_bookmark_info = self.post_hiyobi_bookmark(token, paging) | |
self.parsing_bookmark(paged_bookmark_info["list"]) | |
def parsing_bookmark(self, bookmark_list: list) -> None: | |
for bookmark_element in bookmark_list: | |
search = bookmark_element.get("search") | |
number = bookmark_element.get("galleryid") | |
if search: | |
self.bookmark_info_list.append(search) | |
elif number: | |
self.bookmark_info_list.append(str(number)) | |
else: | |
# 추후 디버깅 | |
raise Exception(bookmark_element) | |
def post(self, url: str, **kwargs): | |
r = requests.post(url, **kwargs) | |
data = r.json() | |
return data | |
def post_hiyobi_bookmark(self, token: str, paging: int = 1): | |
time.sleep(1) | |
response = self.post( | |
f"https://api.hiyobi.me/bookmark/{paging}", | |
headers={ | |
"referrer": "https://hiyobi.me/", | |
"authorization": f"Bearer {token}", | |
}, | |
json={"paging": paging}, | |
) | |
return response | |
def post_account_info(self, email: str, password: str): | |
response = self.post( | |
"https://api.hiyobi.me/user/login", | |
headers={ | |
"referrer": "https://hiyobi.me/", | |
}, | |
json={"email": email, "password": password, "remember": True}, | |
) | |
if response.get("errorMsg"): | |
return self.Invalid(response["errorMsg"]) | |
self.username = response["data"]["name"] | |
return response["data"]["token"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment