# -*- coding: utf-8 -*-
# Copyright 2015-2017 Mike Fährmann
#
# 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://www.deviantart.com/"""
from .common import Extractor, Message
from .. import text, exception
from ..cache import cache
import itertools
import datetime
import time
import re
class DeviantartExtractor(Extractor):
"""Base class for deviantart extractors"""
category = "deviantart"
directory_fmt = ["{category}", "{author[username]}"]
filename_fmt = "{category}_{index}_{title}.{extension}"
def __init__(self):
Extractor.__init__(self)
self.api = DeviantartAPI(self)
self.offset = 0
def skip(self, num):
self.offset += num
return num
def items(self):
last_author = None
yield Message.Version, 1
for deviation in self.deviations():
self.prepare(deviation)
try:
author = deviation["author"]
except KeyError:
author = None
deviation["author"] = {"username": "", "userid": "",
"usericon": "", "type": ""}
if author != last_author:
yield Message.Directory, deviation
last_author = author
if "content" in deviation:
yield self.commit(deviation, deviation["content"])
if "videos" in deviation:
video = max(deviation["videos"],
key=lambda x: int(x["quality"][:-1]))
yield self.commit(deviation, video)
if "flash" in deviation:
yield self.commit(deviation, deviation["flash"])
if "excerpt" in deviation:
journal = self.api.deviation_content(deviation["deviationid"])
yield self.commit_journal(deviation, journal)
def deviations(self):
"""Return an iterable containing all relevant Deviation-objects"""
return []
@staticmethod
def prepare(deviation):
"""Adjust the contents of a Deviation-object"""
for key in ("stats", "preview", "is_favourited", "allows_comments"):
if key in deviation:
del deviation[key]
try:
deviation["index"] = deviation["url"].rsplit("-", 1)[1]
except KeyError:
deviation["index"] = 0
@staticmethod
def commit(deviation, target):
url = target["src"]
deviation["target"] = text.nameext_from_url(url, target.copy())
deviation["extension"] = deviation["target"]["extension"]
return Message.Url, url, deviation
@staticmethod
def commit_journal(deviation, journal):
title = text.escape(deviation["title"])
url = deviation["url"]
thumbs = deviation["thumbs"]
html = journal["html"]
date = datetime.datetime.utcfromtimestamp(deviation["published_time"])
shadow = SHADOW_TEMPLATE.format_map(thumbs[0]) if thumbs else ""
if "css" in journal:
css, cls = journal["css"], "withskin"
else:
css, cls = "", "journal-green"
if html.find('