1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-24 03:32:33 +01:00
gallery-dl/gallery_dl/extractor/imagefap.py

192 lines
7.1 KiB
Python
Raw Normal View History

2016-08-09 14:05:12 +02:00
# -*- coding: utf-8 -*-
# Copyright 2016-2018 Mike Fährmann
2016-08-09 14:05:12 +02: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.
"""Extract images from https://imagefap.com/"""
2016-08-09 14:05:12 +02:00
from .common import Extractor, Message
from .. import text
2016-08-10 10:27:32 +02:00
import json
2016-08-09 14:05:12 +02:00
2017-02-01 00:53:19 +01:00
class ImagefapExtractor(Extractor):
"""Base class for imagefap extractors"""
2016-08-09 14:05:12 +02:00
category = "imagefap"
directory_fmt = ["{category}", "{gallery_id} {title}"]
filename_fmt = "{category}_{gallery_id}_{name}.{extension}"
archive_fmt = "{gallery_id}_{image_id}"
root = "https://www.imagefap.com"
class ImagefapGalleryExtractor(ImagefapExtractor):
"""Extractor for image galleries from imagefap.com"""
subcategory = "gallery"
2016-08-09 14:05:12 +02:00
pattern = [(r"(?:https?://)?(?:www\.)?imagefap\.com/"
r"(?:gallery\.php\?gid=|gallery/|pictures/)(\d+)")]
test = [
("https://www.imagefap.com/pictures/7102714", {
"url": "268995eac5d01ddecd0fe58cfa9828390dc85a84",
"keyword": "b65c310d75269cb6dcc32c3fc1bdcf39bea45342",
"content": "694a0a57385980a6f90fbc296cadcd6c11ba2dab",
}),
("https://www.imagefap.com/gallery/5486966", {
"url": "14906b4f0b8053d1d69bc730a325acb793cbc898",
"keyword": "b84da0543c2d1f848bf5e4c2950dd4f4543a1e0c",
}),
("https://www.imagefap.com/gallery.php?gid=7102714", None),
]
2016-08-09 14:05:12 +02:00
def __init__(self, match):
ImagefapExtractor.__init__(self)
2016-08-09 14:05:12 +02:00
self.gid = match.group(1)
self.image_id = ""
2016-08-09 14:05:12 +02:00
def items(self):
url = "{}/pictures/{}/".format(self.root, self.gid)
2016-08-09 14:05:12 +02:00
page = self.request(url).text
data = self.get_job_metadata(page)
yield Message.Version, 1
yield Message.Directory, data
for url, image in self.get_images():
2016-08-09 14:05:12 +02:00
data.update(image)
2016-08-11 15:50:32 +02:00
yield Message.Url, url, data
2016-08-09 14:05:12 +02:00
def get_job_metadata(self, page):
"""Collect metadata for extractor-job"""
2016-08-11 15:50:32 +02:00
data, pos = text.extract_all(page, (
2016-08-09 14:05:12 +02:00
("section" , '<meta name="description" content="', '"'),
("title" , '<title>Porn pics of ', ' (Page 1)</title>'),
("uploader", '>Uploaded by ', '</font>'),
("count" , ' 1 of ', ' pics"'),
), values={"gallery_id": self.gid})
2016-08-11 15:50:32 +02:00
self.image_id = text.extract(page, 'id="img_ed_', '"', pos)[0]
2016-08-09 14:05:12 +02:00
data["title"] = text.unescape(data["title"])
return data
def get_images(self):
2016-08-11 15:50:32 +02:00
"""Collect image-urls and -metadata"""
2016-08-09 14:05:12 +02:00
num = 0
url = "{}/photo/{}/".format(self.root, self.image_id)
2016-08-11 15:50:32 +02:00
params = {"gid": self.gid, "idx": 0, "partial": "true"}
2016-08-09 14:05:12 +02:00
while True:
2016-08-11 15:50:32 +02:00
pos = 0
page = self.request(url, params=params).text
for _ in range(24):
2016-08-11 15:50:32 +02:00
imgurl, pos = text.extract(page, '<a href="', '"', pos)
if not imgurl:
return
2016-08-11 15:50:32 +02:00
num += 1
_, imgid, name = imgurl.rsplit("/", 2)
data = {"image_id": imgid, "num": num}
2016-08-11 15:50:32 +02:00
yield imgurl, text.nameext_from_url(name, data)
params["idx"] += 24
2016-08-10 10:27:32 +02:00
class ImagefapImageExtractor(ImagefapExtractor):
"""Extractor for single images from imagefap.com"""
2016-08-10 10:27:32 +02:00
subcategory = "image"
pattern = [r"(?:https?://)?(?:www\.)?imagefap\.com/photo/(\d+)"]
test = [("https://www.imagefap.com/photo/1369341772/", {
"url": "b31ee405b61ff0450020a1bf11c0581ca9adb471",
"keyword": "26ae84575067b8231878ec1a2d1e14a0fbcea865",
2016-08-10 10:27:32 +02:00
})]
def __init__(self, match):
ImagefapExtractor.__init__(self)
2016-08-10 10:27:32 +02:00
self.image_id = match.group(1)
def items(self):
info = self.load_json()
data = self.get_job_metadata(info)
yield Message.Version, 1
yield Message.Directory, data
yield Message.Url, info["contentUrl"], data
def get_job_metadata(self, info):
"""Collect metadata for extractor-job"""
parts = info["contentUrl"].rsplit("/", 3)
return text.nameext_from_url(parts[3], {
"title": text.unescape(info["name"]),
"section": info["section"],
"uploader": info["author"],
"date": info["datePublished"],
"width": info["width"],
"height": info["height"],
"gallery_id": parts[1],
"image_id": parts[2],
2016-08-10 10:27:32 +02:00
})
def load_json(self):
"""Load the JSON dictionary associated with the image"""
url = "{}/photo/{}/".format(self.root, self.image_id)
2016-08-10 10:27:32 +02:00
page = self.request(url).text
2017-02-01 00:53:19 +01:00
section , pos = text.extract(
page, '<meta name="description" content="', '"')
2017-02-01 00:53:19 +01:00
json_data, pos = text.extract(
page, '<script type="application/ld+json">', '</script>', pos)
2016-08-10 10:27:32 +02:00
json_dict = json.loads(json_data)
json_dict["section"] = section
return json_dict
2016-08-10 12:54:18 +02:00
class ImagefapUserExtractor(ImagefapExtractor):
"""Extractor for all galleries from a user at imagefap.com"""
2016-08-10 12:54:18 +02:00
subcategory = "user"
categorytransfer = True
2017-02-01 00:53:19 +01:00
pattern = [(r"(?:https?://)?(?:www\.)?imagefap\.com/"
r"profile(?:\.php\?user=|/)([^/]+)"),
(r"(?:https?://)?(?:www\.)?imagefap\.com/"
r"usergallery\.php\?userid=(\d+)")]
test = [("https://www.imagefap.com/profile/Mr%20Bad%20Example/galleries", {
"url": "2d79fe2bd98d02d30378fe30e966d47f72bd4760",
2016-08-10 12:54:18 +02:00
})]
def __init__(self, match):
ImagefapExtractor.__init__(self)
2016-08-10 12:54:18 +02:00
try:
self.user_id = int(match.group(1))
self.user = None
except ValueError:
self.user_id = None
self.user = match.group(1)
def items(self):
yield Message.Version, 1
for gid, name in self.get_gallery_data():
url = "{}/gallery/{}".format(self.root, gid)
data = {"gallery_id": text.parse_int(gid), "title": name}
yield Message.Queue, url, data
2016-08-10 12:54:18 +02:00
def get_gallery_data(self):
"""Yield all gallery_ids of a specific user"""
2016-08-10 12:54:18 +02:00
folders = self.get_gallery_folders()
url = "{}/ajax_usergallery_folder.php".format(self.root)
2016-08-10 12:54:18 +02:00
params = {"userid": self.user_id}
for folder_id in folders:
params["id"] = folder_id
page = self.request(url, params=params).text
pos = 0
while True:
gid, pos = text.extract(page, '<a href="/gallery/', '"', pos)
if not gid:
break
name, pos = text.extract(page, "<b>", "<", pos)
yield gid, name
2016-08-10 12:54:18 +02:00
def get_gallery_folders(self):
"""Create a list of all folder_ids of a specific user"""
2016-08-10 12:54:18 +02:00
if self.user:
url = "{}/profile/{}/galleries".format(self.root, self.user)
2016-08-10 12:54:18 +02:00
else:
url = "{}/usergallery.php?userid={}".format(
self.root, self.user_id)
2016-08-10 12:54:18 +02:00
page = self.request(url).text
self.user_id, pos = text.extract(page, '?userid=', '"')
2017-02-01 00:53:19 +01:00
folders, pos = text.extract(page, ' id="tgl_all" value="', '"', pos)
2016-08-10 12:54:18 +02:00
return folders.split("|")[:-1]