2018-11-27 15:44:53 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-02-08 13:45:40 +01:00
|
|
|
# Copyright 2018-2019 Mike Fährmann
|
2018-11-27 15:44:53 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Extractors for https://www.newgrounds.com/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2019-11-15 23:54:07 +01:00
|
|
|
from .. import text, exception
|
|
|
|
from ..cache import cache
|
2018-11-27 15:44:53 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
class NewgroundsExtractor(Extractor):
|
|
|
|
"""Base class for newgrounds extractors"""
|
|
|
|
category = "newgrounds"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{user}")
|
2018-11-27 15:44:53 +01:00
|
|
|
filename_fmt = "{category}_{index}_{title}.{extension}"
|
|
|
|
archive_fmt = "{index}"
|
2019-11-15 23:54:07 +01:00
|
|
|
root = "https://www.newgrounds.com"
|
|
|
|
cookiedomain = ".newgrounds.com"
|
|
|
|
cookienames = ("NG_GG_username", "vmk1du5I8m")
|
2018-11-27 15:44:53 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
Extractor.__init__(self, match)
|
2018-11-27 15:44:53 +01:00
|
|
|
self.user = match.group(1)
|
2019-11-15 23:54:07 +01:00
|
|
|
self.user_root = "https://{}.newgrounds.com".format(self.user)
|
2018-11-27 15:44:53 +01:00
|
|
|
|
|
|
|
def items(self):
|
2019-11-15 23:54:07 +01:00
|
|
|
self.login()
|
2019-11-14 23:17:14 +01:00
|
|
|
data = self.metadata()
|
2018-11-27 15:44:53 +01:00
|
|
|
yield Message.Version, 1
|
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
for post_url in self.posts():
|
|
|
|
file = self.extract_post_data(post_url)
|
|
|
|
file.update(data)
|
|
|
|
url = file["url"]
|
|
|
|
yield Message.Directory, file
|
|
|
|
yield Message.Url, url, text.nameext_from_url(url, file)
|
2018-11-27 15:44:53 +01:00
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def metadata(self):
|
2018-11-27 15:44:53 +01:00
|
|
|
"""Collect metadata for extractor-job"""
|
|
|
|
return {"user": self.user}
|
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def posts(self):
|
2018-11-27 15:44:53 +01:00
|
|
|
"""Return urls of all relevant image pages"""
|
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def extract_post_data(self, post_url):
|
|
|
|
"""Collect url and metadata from an image post"""
|
|
|
|
extr = text.extract_from(self.request(post_url).text)
|
2019-05-08 18:09:17 +02:00
|
|
|
full = text.extract_from(json.loads(extr('"full_image_text":', '});')))
|
|
|
|
data = {
|
2019-07-08 17:53:16 +02:00
|
|
|
"title" : text.unescape(extr('"og:title" content="', '"')),
|
2019-05-08 18:09:17 +02:00
|
|
|
"description": text.unescape(extr(':description" content="', '"')),
|
2019-07-08 17:53:16 +02:00
|
|
|
"date" : text.parse_datetime(extr(
|
|
|
|
'itemprop="datePublished" content="', '"')),
|
2019-05-08 18:09:17 +02:00
|
|
|
"rating" : extr('class="rated-', '"'),
|
|
|
|
"favorites" : text.parse_int(extr('id="faves_load">', '<')),
|
|
|
|
"score" : text.parse_float(extr('id="score_number">', '<')),
|
2019-07-08 17:53:16 +02:00
|
|
|
"tags" : text.split_html(extr(
|
|
|
|
'<dd class="tags momag">', '</dd>')),
|
2019-05-08 18:09:17 +02:00
|
|
|
"url" : full('src="', '"'),
|
|
|
|
"width" : text.parse_int(full('width="', '"')),
|
|
|
|
"height" : text.parse_int(full('height="', '"')),
|
|
|
|
}
|
2019-07-08 17:53:16 +02:00
|
|
|
data["tags"].sort()
|
2019-05-08 18:09:17 +02:00
|
|
|
data["index"] = text.parse_int(
|
|
|
|
data["url"].rpartition("/")[2].partition("_")[0])
|
|
|
|
return data
|
2018-11-27 15:44:53 +01:00
|
|
|
|
2019-11-15 23:54:07 +01:00
|
|
|
def login(self):
|
|
|
|
username, password = self._get_auth_info()
|
|
|
|
if username:
|
|
|
|
self._update_cookies(self._login_impl(username, password))
|
|
|
|
|
|
|
|
@cache(maxage=360*24*3600, keyarg=1)
|
|
|
|
def _login_impl(self, username, password):
|
|
|
|
self.log.info("Logging in as %s", username)
|
|
|
|
|
|
|
|
url = self.root + "/passport/"
|
|
|
|
page = self.request(url).text
|
|
|
|
headers = {"Origin": self.root, "Referer": url}
|
|
|
|
|
|
|
|
url = text.urljoin(self.root, text.extract(page, 'action="', '"')[0])
|
|
|
|
data = {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
"remember": "1",
|
|
|
|
"login" : "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = self.request(url, method="POST", headers=headers, data=data)
|
|
|
|
if not response.history:
|
|
|
|
raise exception.AuthenticationError()
|
|
|
|
|
|
|
|
return {
|
|
|
|
cookie.name: cookie.value
|
|
|
|
for cookie in response.history[0].cookies
|
|
|
|
if cookie.expires and cookie.domain == self.cookiedomain
|
|
|
|
}
|
|
|
|
|
2018-11-27 15:44:53 +01:00
|
|
|
def _pagination(self, url):
|
|
|
|
headers = {
|
|
|
|
"Referer": self.root,
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
"Accept": "application/json, text/javascript, */*; q=0.01",
|
|
|
|
}
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = self.request(url, headers=headers).json()
|
|
|
|
|
|
|
|
for year in data["sequence"]:
|
|
|
|
for item in data["years"][str(year)]["items"]:
|
|
|
|
page_url = text.extract(item, 'href="', '"')[0]
|
|
|
|
yield text.urljoin(self.root, page_url)
|
|
|
|
|
|
|
|
if not data["more"]:
|
|
|
|
return
|
|
|
|
url = text.urljoin(self.root, data["more"])
|
|
|
|
|
|
|
|
|
|
|
|
class NewgroundsUserExtractor(NewgroundsExtractor):
|
|
|
|
"""Extractor for all images of a newgrounds user"""
|
|
|
|
subcategory = "user"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = r"(?:https?://)?([^.]+)\.newgrounds\.com(?:/art)?/?$"
|
|
|
|
test = (
|
2018-11-27 15:44:53 +01:00
|
|
|
("https://blitzwuff.newgrounds.com/art", {
|
|
|
|
"url": "24b19c4a135a09889fac7b46a74e427e4308d02b",
|
2019-11-14 23:17:14 +01:00
|
|
|
"keyword": "8330e1e563c8a5464938feaceac88aa9870aefe4",
|
2018-11-27 15:44:53 +01:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
("https://blitzwuff.newgrounds.com/"),
|
|
|
|
)
|
2018-11-27 15:44:53 +01:00
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def posts(self):
|
2018-11-27 15:44:53 +01:00
|
|
|
return self._pagination(self.root + "/art/page/1")
|
|
|
|
|
|
|
|
|
2019-01-14 16:06:23 +01:00
|
|
|
class NewgroundsImageExtractor(NewgroundsExtractor):
|
2018-11-27 15:44:53 +01:00
|
|
|
"""Extractor for a single image from newgrounds.com"""
|
|
|
|
subcategory = "image"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = (r"(?:https?://)?(?:"
|
2019-01-25 16:35:12 +01:00
|
|
|
r"(?:www\.)?newgrounds\.com/art/view/([^/?&#]+)/[^/?&#]+"
|
2019-02-08 13:45:40 +01:00
|
|
|
r"|art\.ngfiles\.com/images/\d+/\d+_([^_]+)_([^.]+))")
|
|
|
|
test = (
|
2019-01-25 16:35:12 +01:00
|
|
|
("https://www.newgrounds.com/art/view/blitzwuff/ffx", {
|
|
|
|
"url": "e7778c4597a2fb74b46e5f04bb7fa1d80ca02818",
|
2019-11-14 23:17:14 +01:00
|
|
|
"keyword": "3ffcdc8f54a46b8ee1c9b433627f66b750edff51",
|
2019-01-25 16:35:12 +01:00
|
|
|
"content": "cb067d6593598710292cdd340d350d14a26fe075",
|
|
|
|
}),
|
|
|
|
("https://art.ngfiles.com/images/587000/587551_blitzwuff_ffx.png", {
|
|
|
|
"url": "e7778c4597a2fb74b46e5f04bb7fa1d80ca02818",
|
2019-11-14 23:17:14 +01:00
|
|
|
"keyword": "3ffcdc8f54a46b8ee1c9b433627f66b750edff51",
|
2019-01-25 16:35:12 +01:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
)
|
2018-11-27 15:44:53 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-01-14 16:06:23 +01:00
|
|
|
NewgroundsExtractor.__init__(self, match)
|
2019-01-25 16:35:12 +01:00
|
|
|
if match.group(2):
|
|
|
|
self.user = match.group(2)
|
2019-11-14 23:17:14 +01:00
|
|
|
self.post_url = "https://www.newgrounds.com/art/view/{}/{}".format(
|
2019-01-25 16:35:12 +01:00
|
|
|
self.user, match.group(3))
|
|
|
|
else:
|
2019-11-14 23:17:14 +01:00
|
|
|
self.post_url = match.group(0)
|
2018-11-27 15:44:53 +01:00
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def posts(self):
|
|
|
|
return (self.post_url,)
|
2018-11-27 15:44:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NewgroundsVideoExtractor(NewgroundsExtractor):
|
|
|
|
"""Extractor for all videos of a newgrounds user"""
|
|
|
|
subcategory = "video"
|
|
|
|
filename_fmt = "{category}_{index}.{extension}"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = r"(?:https?://)?([^.]+)\.newgrounds\.com/movies/?$"
|
2019-07-01 20:02:47 +02:00
|
|
|
test = ("https://tomfulp.newgrounds.com/movies", {
|
2019-01-17 21:21:57 +01:00
|
|
|
"pattern": r"ytdl:https?://www\.newgrounds\.com/portal/view/\d+",
|
2019-07-01 20:02:47 +02:00
|
|
|
"count": ">= 32",
|
2019-02-08 13:45:40 +01:00
|
|
|
})
|
2018-11-27 15:44:53 +01:00
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def posts(self):
|
2018-11-27 15:44:53 +01:00
|
|
|
return self._pagination(self.root + "/movies/page/1")
|
|
|
|
|
2019-11-14 23:17:14 +01:00
|
|
|
def extract_post_data(self, page_url):
|
2019-05-08 18:09:17 +02:00
|
|
|
return {
|
|
|
|
"url" : "ytdl:" + page_url,
|
|
|
|
"index": text.parse_int(page_url.rpartition("/")[2]),
|
2018-11-27 15:44:53 +01:00
|
|
|
}
|