| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://framedsc.com/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text |
| 11 |
|
| 12 |
BASE_PATTERN = r"(?:https?://)?(?:cdn\.|www\.)?framedsc.com" |
| 13 |
RELATIONS = "<=>" |
| 14 |
|
| 15 |
|
| 16 |
class FramedscExtractor(Extractor): |
| 17 |
"""Base class for framedsc extractors""" |
| 18 |
category = "framedsc" |
| 19 |
archive_fmt = "{filename}" |
| 20 |
|
| 21 |
def _load_db(self): |
| 22 |
base = ("https://raw.githubusercontent.com" |
| 23 |
"/originalnicodrgitbot/hall-of-framed-db/main/") |
| 24 |
return ( |
| 25 |
self.request_json(base + "shotsdb.json")["_default"], |
| 26 |
self.request_json(base + "authorsdb.json")["_default"], |
| 27 |
) |
| 28 |
|
| 29 |
def items(self): |
| 30 |
self.image_db, self.author_db = self.cache(self._load_db, _key=None) |
| 31 |
|
| 32 |
for image in self.images(): |
| 33 |
url = image["shotUrl"] |
| 34 |
image = text.nameext_from_url(url, image.copy()) |
| 35 |
image["date"] = self.parse_datetime_iso(image["date"]) |
| 36 |
yield Message.Directory, "", image |
| 37 |
yield Message.Url, url, image |
| 38 |
|
| 39 |
|
| 40 |
class FramedscImageExtractor(FramedscExtractor): |
| 41 |
"""Extractor for single framedsc image""" |
| 42 |
subcategory = "image" |
| 43 |
archive_fmt = "{filename}" |
| 44 |
pattern = BASE_PATTERN + r"/HallOfFramed/?\?[^#]*\bimageId=(\d+)" |
| 45 |
example = "https://framedsc.com/HallOfFramed/?imageId=12345" |
| 46 |
|
| 47 |
def images(self): |
| 48 |
image_id = int(self.groups[0]) |
| 49 |
for image in self.image_db.values(): |
| 50 |
if image["epochTime"] == image_id: |
| 51 |
return (image,) |
| 52 |
return () |
| 53 |
|
| 54 |
|
| 55 |
class FramedscRawExtractor(FramedscExtractor): |
| 56 |
"""Extractor for single framedsc image (raw image link)""" |
| 57 |
subcategory = "raw" |
| 58 |
pattern = BASE_PATTERN + r"/images(/[^/?#]+)" |
| 59 |
example = "https://cdn.framedsc.com/images/12345_NAME.EXT" |
| 60 |
|
| 61 |
def images(self): |
| 62 |
filename = self.groups[0] |
| 63 |
for image in self.image_db.values(): |
| 64 |
if image["shotUrl"].endswith(filename): |
| 65 |
return (image,) |
| 66 |
return () |
| 67 |
|
| 68 |
|
| 69 |
class FramedscSearchExtractor(FramedscExtractor): |
| 70 |
"""Extractor for framedsc image searches""" |
| 71 |
subcategory = "search" |
| 72 |
pattern = BASE_PATTERN + r"/HallOfFramed/?\?(?![^#]*\bimageId=)([^#]*)" |
| 73 |
example = "https://framedsc.com/HallOfFramed/?author=AUTHOR&title=TITLE" |
| 74 |
|
| 75 |
def clean_filters(self, unclean_filters): |
| 76 |
def valid_date(date): |
| 77 |
return bool(self.parse_datetime_iso(date)) |
| 78 |
|
| 79 |
def valid_int(integer): |
| 80 |
try: |
| 81 |
int(integer) |
| 82 |
return True |
| 83 |
except (ValueError): |
| 84 |
return False |
| 85 |
|
| 86 |
filters = { |
| 87 |
"author": [], |
| 88 |
"title": [], |
| 89 |
"on": [], |
| 90 |
"before": [], |
| 91 |
"after": [], |
| 92 |
"color": [], |
| 93 |
"width": { |
| 94 |
">": [], |
| 95 |
"<": [], |
| 96 |
"=": [] |
| 97 |
}, |
| 98 |
"height": { |
| 99 |
">": [], |
| 100 |
"<": [], |
| 101 |
"=": [] |
| 102 |
}, |
| 103 |
"score": { |
| 104 |
">": [], |
| 105 |
"<": [], |
| 106 |
"=": [] |
| 107 |
}, |
| 108 |
} |
| 109 |
|
| 110 |
for (field, value) in unclean_filters.items(): |
| 111 |
value = text.unquote(value) |
| 112 |
|
| 113 |
if field == "author": |
| 114 |
filters[field].append(self.find_author(value)) |
| 115 |
elif field == "title": |
| 116 |
filters[field].append(value.replace("+", " ")) |
| 117 |
elif field in {"width", "height", "score"}: |
| 118 |
if not value or value[0] not in "<>": |
| 119 |
filters[field]["="].append(value[1:]) |
| 120 |
else: |
| 121 |
filters[field][value[0]].append(value[1:]) |
| 122 |
else: |
| 123 |
if field in filters.keys(): |
| 124 |
filters[field].append(value) |
| 125 |
|
| 126 |
for field in ("on", "before", "after"): |
| 127 |
if filters[field]: |
| 128 |
filters[field] = [ |
| 129 |
self.parse_datetime_iso(date) |
| 130 |
for date in filters[field] |
| 131 |
if valid_date(date) |
| 132 |
] |
| 133 |
if not filters[field]: |
| 134 |
return |
| 135 |
|
| 136 |
for field in ("width", "height", "score"): |
| 137 |
field_exists = False |
| 138 |
for relation in RELATIONS: |
| 139 |
field_exists = len(filters[field][relation]) > 0 |
| 140 |
filters[field][relation] = [ |
| 141 |
int(integer) |
| 142 |
for integer in filters[field][relation] |
| 143 |
if valid_int(integer) |
| 144 |
] |
| 145 |
|
| 146 |
if field_exists and all( |
| 147 |
not filters[field][relation] |
| 148 |
for relation in RELATIONS |
| 149 |
): |
| 150 |
return |
| 151 |
|
| 152 |
return filters |
| 153 |
|
| 154 |
def find_author(self, author_nick): |
| 155 |
for author in self.author_db.values(): |
| 156 |
if author["authorNick"] == author_nick: |
| 157 |
return author["authorid"] |
| 158 |
raise self.exc.NotFoundError("author") |
| 159 |
|
| 160 |
def images(self): |
| 161 |
unclean_filters = text.parse_query(self.groups[0]) |
| 162 |
filters = self.clean_filters(unclean_filters) |
| 163 |
|
| 164 |
if not filters: |
| 165 |
return self.image_db.values() |
| 166 |
|
| 167 |
def any_match_strict(image_field, target_fields): |
| 168 |
return any( |
| 169 |
[image_field == target_field for target_field in target_fields] |
| 170 |
) |
| 171 |
|
| 172 |
def any_greater_strict(image_field, target_fields): |
| 173 |
return any( |
| 174 |
[image_field >= target_field for target_field in target_fields] |
| 175 |
) |
| 176 |
|
| 177 |
def any_less_strict(image_field, target_fields): |
| 178 |
return any( |
| 179 |
[image_field <= target_field for target_field in target_fields] |
| 180 |
) |
| 181 |
|
| 182 |
def any_match(image_field, target_fields): |
| 183 |
return not target_fields or any_match_strict( |
| 184 |
image_field, target_fields) |
| 185 |
|
| 186 |
def any_greater(image_field, target_fields): |
| 187 |
return not target_fields or any_greater_strict( |
| 188 |
image_field, target_fields) |
| 189 |
|
| 190 |
def any_less(image_field, target_fields): |
| 191 |
return not target_fields or any_less_strict( |
| 192 |
image_field, target_fields) |
| 193 |
|
| 194 |
def integer_field_check(image, field): |
| 195 |
filter_dne = all([len(filters[field][relation]) == |
| 196 |
0 for relation in RELATIONS]) |
| 197 |
|
| 198 |
equal = any_match_strict(image[field], filters[field]["="]) |
| 199 |
greater = any_greater_strict(image[field], filters[field][">"]) |
| 200 |
less = any_less_strict(image[field], filters[field]["<"]) |
| 201 |
|
| 202 |
return filter_dne or equal or greater or less |
| 203 |
|
| 204 |
def title_check(title, target_titles): |
| 205 |
return not target_titles or any( |
| 206 |
target_title in title |
| 207 |
for target_title in target_titles |
| 208 |
) |
| 209 |
|
| 210 |
for image in self.image_db.values(): |
| 211 |
author = any_match(image["author"], filters["author"]) |
| 212 |
title = title_check(image["gameName"], filters["title"]) |
| 213 |
color = any_match(image["colorName"], filters["color"]) |
| 214 |
|
| 215 |
date = self.parse_datetime_iso(image["date"][:10]) |
| 216 |
before = any_less(date, filters["before"]) |
| 217 |
on = any_match(date, filters["on"]) |
| 218 |
after = any_greater(date, filters["after"]) |
| 219 |
|
| 220 |
width = integer_field_check(image, "width") |
| 221 |
height = integer_field_check(image, "height") |
| 222 |
score = integer_field_check(image, "score") |
| 223 |
|
| 224 |
if author and title and color and before and on and after and \ |
| 225 |
width and height and score: |
| 226 |
yield image |