-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimbc.py
157 lines (127 loc) · 5.55 KB
/
imbc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Extractors for https://imbc.com"""
import re
from http import HTTPStatus
from gallery_dl import exception, text
from gallery_dl.extractor.common import Extractor, Message
class ImbcExtractor(Extractor):
category = "imbc"
filename_fmt = "{filename}.{extension}"
def _call(self, url, params=None):
if params is None:
params = {}
while True:
response = self.request(url, params=params, fatal=None, allow_redirects=False)
if response.status_code < HTTPStatus.MULTIPLE_CHOICES:
return response.text
if response.status_code == HTTPStatus.UNAUTHORIZED:
raise exception.AuthenticationError from None
if response.status_code == HTTPStatus.FORBIDDEN:
raise exception.AuthorizationError from None
if response.status_code == HTTPStatus.NOT_FOUND:
raise exception.NotFoundError(self.subcategory) from None
self.log.debug(response.text)
msg = "Request failed"
raise exception.StopExtraction(msg)
def metadata(self, page):
return {
"title": text.unescape(text.extr(page, "<title>", "</title>")),
"article_id": self.article_id,
"post_url": text.extr(page, 'property="og:url" content="', '"'),
}
class ImbcAdenewsArticleExtractor(ImbcExtractor):
"""Extractors for articles on adenews.imbc.com"""
subcategory = "adenews-article"
root = "https://adenews.imbc.com"
directory_fmt = ("{category}", "{subcategory}", "{article_id}")
archive_fmt = "{category}_{subcategory}_{article_id}_{filename}"
pattern = r"(?:https?://)?adenews\.imbc\.com/M/Detail/(\d+)"
example = "https://adenews.imbc.com/M/Detail/12345"
def __init__(self, match):
ImbcExtractor.__init__(self, match)
self.article_id = match.group(1)
def items(self):
page = self._call(self.url)
data = self.metadata(page)
data["post_url"] = f"{self.root}/M/Detail/{self.article_id}"
data["date"] = text.parse_datetime(
text.extr(text.extr(page, '<div class="date">', "</div>"), "<span>", "</span>"),
format="%Y-%m-%d %H:%M",
utcoffset=9,
)
article_content = text.extr(page, "<!-- 기사 상세 Start -->", "<!-- 네이티브 End -->")
urls = [
text.ensure_http_scheme(text.extr(image, 'src="', '"'))
for image in text.extract_iter(article_content, "<img", ">")
]
yield Message.Directory, data
for data["num"], url in enumerate(urls, 1):
image = {"url": url}
data["image"] = image
text.nameext_from_url(url, data)
yield Message.Url, url, data
class ImbcEnewsArticleExtractor(ImbcExtractor):
"""Extractors for articles on enews.imbc.com"""
subcategory = "enews-article"
root = "https://enews.imbc.com"
directory_fmt = ("{category}", "{subcategory}", "{article_id}")
archive_fmt = "{category}_{subcategory}_{article_id}_{filename}"
pattern = r"(?:https?://)?enews\.imbc\.com/News/RetrieveNewsInfo/(\d+)"
example = "https://enews.imbc.com/News/RetrieveNewsInfo/12345"
def __init__(self, match):
ImbcExtractor.__init__(self, match)
self.article_id = match.group(1)
def items(self):
page = self._call(self.url)
data = self.metadata(page)
data["date"] = text.parse_datetime(
re.sub(r"Z$", "", text.extr(page, 'property="article:published_time" content="', '"')),
format="%Y-%m-%dT%H:%M:%S",
utcoffset=9,
)
article_content = text.extr(page, "<!-- 기사 본문 내용 Start -->", "<!-- 기사 본문 내용 End -->")
urls = [
text.ensure_http_scheme(text.extr(image, 'src="', '"'))
for image in text.extract_iter(article_content, "<img", ">")
]
yield Message.Directory, data
for data["num"], url in enumerate(urls, 1):
image = {"url": url}
data["image"] = image
text.nameext_from_url(url, data)
yield Message.Url, url, data
class ImbcImnewsArticleExtractor(ImbcExtractor):
"""Extractors for articles on imnews.imbc.com"""
subcategory = "imnews-article"
root = "https://imnews.imbc.com"
directory_fmt = (
"{category}",
"{subcategory}",
"{article_id}",
)
archive_fmt = "{category}_{subcategory}_{article_id}_{filename}"
pattern = r"(?:https?://)?imnews\.imbc\.com/news/\d+/\w+/article/(\d+_\d+).html"
example = "https://imnews.imbc.com/news/2024/enter/article/12345_67890.html"
def __init__(self, match):
ImbcExtractor.__init__(self, match)
self.article_id = match.group(1)
def items(self):
page = self._call(self.url)
data = self.metadata(page)
data["date"] = text.parse_datetime(
text.extr(page, 'property="article:published_time" content="', '"'), format="%Y-%m-%dT%H:%M:%S%z"
)
article_content = text.extr(
page,
"<!-- // 전체재생 레이어 -->",
"<!-- iMBC 연예 데이블 본문 바로 밑 -->",
)
urls = [
text.ensure_http_scheme(text.extr(image, 'src="', '"'))
for image in text.extract_iter(article_content, "<img", ">")
]
yield Message.Directory, data
for data["num"], url in enumerate(urls, 1):
image = {"url": url}
data["image"] = image
text.nameext_from_url(url, data)
yield Message.Url, url, data