vibegif 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vibegif-0.1.0/MANIFEST.in +1 -0
- vibegif-0.1.0/PKG-INFO +48 -0
- vibegif-0.1.0/README.md +35 -0
- vibegif-0.1.0/pyproject.toml +26 -0
- vibegif-0.1.0/setup.cfg +4 -0
- vibegif-0.1.0/tests/catalog.json +7 -0
- vibegif-0.1.0/tests/test_catalog.py +131 -0
- vibegif-0.1.0/vibegif/__init__.py +3 -0
- vibegif-0.1.0/vibegif/catalog.py +149 -0
- vibegif-0.1.0/vibegif.egg-info/PKG-INFO +48 -0
- vibegif-0.1.0/vibegif.egg-info/SOURCES.txt +12 -0
- vibegif-0.1.0/vibegif.egg-info/dependency_links.txt +1 -0
- vibegif-0.1.0/vibegif.egg-info/requires.txt +3 -0
- vibegif-0.1.0/vibegif.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include tests/catalog.json
|
vibegif-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vibegif
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Search vibegif's public domain animated webp/gifs and get hotlinkable URLs
|
|
5
|
+
License-Expression: CC0-1.0
|
|
6
|
+
Project-URL: Homepage, https://vibegif.direct-img.link
|
|
7
|
+
Project-URL: Source, https://github.com/direct-img/vibegif.direct-img.link/tree/main/packages/python
|
|
8
|
+
Keywords: gif,webp,animation,cc0,public-domain
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Provides-Extra: test
|
|
12
|
+
Requires-Dist: pytest; extra == "test"
|
|
13
|
+
|
|
14
|
+
<img src="https://vibegif.direct-img.link/cat/blink.webp" align="right" width="96" alt="blinking cat">
|
|
15
|
+
|
|
16
|
+
# vibegif (Python)
|
|
17
|
+
|
|
18
|
+
Search [vibegif](https://vibegif.direct-img.link)'s 1,400+ public domain (CC0) animations from Python and get URLs you can hotlink. No dependencies, Python 3.10+.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install vibegif
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from vibegif import Catalog
|
|
26
|
+
|
|
27
|
+
catalog = Catalog()
|
|
28
|
+
for item in catalog.search("rainbow brain", limit=3):
|
|
29
|
+
print(item.id, item.url()) # brain/glow https://vibegif.direct-img.link/brain/glow.webp
|
|
30
|
+
|
|
31
|
+
catalog.search("spinner", section="loader") # section is a hard filter
|
|
32
|
+
catalog.search("cat", tags=["calm", "night"]) # so are tags, every one required
|
|
33
|
+
catalog.random(section="loader") # one random match, or None
|
|
34
|
+
catalog.imfeelinglucky() # any anim at all
|
|
35
|
+
catalog.get("cat/blink") # Item, or None
|
|
36
|
+
catalog.sections() # [("animedev", 150), ("inkwash", 150), ...]
|
|
37
|
+
catalog.tags() # [("calm", 369), ("happy", 329), ...]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
<img src="https://vibegif.direct-img.link/python/charm.webp" align="right" width="128" alt="girl playing a flute as a blue and yellow snake rises from a basket">
|
|
41
|
+
|
|
42
|
+
An `Item` has `id`, `section`, `description` and `tags`. `item.url()` is its webp, which every item has, and `item.url("gif")` its gif, which most have.
|
|
43
|
+
|
|
44
|
+
Search scores each query term: an exact tag beats a tag prefix, which beats a word in the description. Results come best first, ties break by id, and an empty query returns everything that passes the filters, in id order. It returns 50 at most unless you pass `limit` (`limit=None` for every match).
|
|
45
|
+
|
|
46
|
+
The catalog comes from [catalog.json](https://vibegif.direct-img.link/catalog.json). It's fetched on first use and checked again once it's an hour old (`Catalog(ttl=seconds)`, or `catalog.refresh(force=True)` to check now). An unchanged catalog costs a 304, and a failed check keeps the items already fetched.
|
|
47
|
+
|
|
48
|
+
Tests: `uv run --with pytest pytest`
|
vibegif-0.1.0/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<img src="https://vibegif.direct-img.link/cat/blink.webp" align="right" width="96" alt="blinking cat">
|
|
2
|
+
|
|
3
|
+
# vibegif (Python)
|
|
4
|
+
|
|
5
|
+
Search [vibegif](https://vibegif.direct-img.link)'s 1,400+ public domain (CC0) animations from Python and get URLs you can hotlink. No dependencies, Python 3.10+.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
pip install vibegif
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from vibegif import Catalog
|
|
13
|
+
|
|
14
|
+
catalog = Catalog()
|
|
15
|
+
for item in catalog.search("rainbow brain", limit=3):
|
|
16
|
+
print(item.id, item.url()) # brain/glow https://vibegif.direct-img.link/brain/glow.webp
|
|
17
|
+
|
|
18
|
+
catalog.search("spinner", section="loader") # section is a hard filter
|
|
19
|
+
catalog.search("cat", tags=["calm", "night"]) # so are tags, every one required
|
|
20
|
+
catalog.random(section="loader") # one random match, or None
|
|
21
|
+
catalog.imfeelinglucky() # any anim at all
|
|
22
|
+
catalog.get("cat/blink") # Item, or None
|
|
23
|
+
catalog.sections() # [("animedev", 150), ("inkwash", 150), ...]
|
|
24
|
+
catalog.tags() # [("calm", 369), ("happy", 329), ...]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
<img src="https://vibegif.direct-img.link/python/charm.webp" align="right" width="128" alt="girl playing a flute as a blue and yellow snake rises from a basket">
|
|
28
|
+
|
|
29
|
+
An `Item` has `id`, `section`, `description` and `tags`. `item.url()` is its webp, which every item has, and `item.url("gif")` its gif, which most have.
|
|
30
|
+
|
|
31
|
+
Search scores each query term: an exact tag beats a tag prefix, which beats a word in the description. Results come best first, ties break by id, and an empty query returns everything that passes the filters, in id order. It returns 50 at most unless you pass `limit` (`limit=None` for every match).
|
|
32
|
+
|
|
33
|
+
The catalog comes from [catalog.json](https://vibegif.direct-img.link/catalog.json). It's fetched on first use and checked again once it's an hour old (`Catalog(ttl=seconds)`, or `catalog.refresh(force=True)` to check now). An unchanged catalog costs a 304, and a failed check keeps the items already fetched.
|
|
34
|
+
|
|
35
|
+
Tests: `uv run --with pytest pytest`
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vibegif"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Search vibegif's public domain animated webp/gifs and get hotlinkable URLs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "CC0-1.0"
|
|
12
|
+
keywords = ["gif", "webp", "animation", "cc0", "public-domain"]
|
|
13
|
+
dependencies = []
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://vibegif.direct-img.link"
|
|
17
|
+
Source = "https://github.com/direct-img/vibegif.direct-img.link/tree/main/packages/python"
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
test = ["pytest"]
|
|
21
|
+
|
|
22
|
+
[tool.setuptools]
|
|
23
|
+
packages = ["vibegif"]
|
|
24
|
+
|
|
25
|
+
[tool.pytest.ini_options]
|
|
26
|
+
testpaths = ["tests"]
|
vibegif-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{"count":5,"items":[
|
|
2
|
+
{"id":"brain/glow","section":"psychedelic","description":"side view brain with a rainbow glow","tags":["brain","rainbow","glow"]},
|
|
3
|
+
{"id":"brain/zap","section":"science","description":"neurons firing in a brain","tags":["brain","neuron"]},
|
|
4
|
+
{"id":"cat/blink","section":"animals","description":"a cat blinking slowly, glow of a screen","tags":["cat","night-sky"]},
|
|
5
|
+
{"id":"cat/loaf","section":"animals","description":"cat loaf under the night sky","tags":["cat","cozy"]},
|
|
6
|
+
{"id":"misc/dot","section":"animals","description":"a plain dot","tags":["catnip"]}
|
|
7
|
+
]}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import gzip
|
|
2
|
+
import json
|
|
3
|
+
import threading
|
|
4
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from vibegif import SITE, Catalog, Item
|
|
10
|
+
|
|
11
|
+
FIXTURE = Path(__file__).with_name("catalog.json")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.fixture(scope="module")
|
|
15
|
+
def catalog():
|
|
16
|
+
return Catalog(FIXTURE.as_uri())
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@pytest.fixture
|
|
20
|
+
def server():
|
|
21
|
+
"""Serves `state["body"]` gzipped with an ETag, honoring If-None-Match."""
|
|
22
|
+
state = {"body": FIXTURE.read_bytes(), "fail": False, "hits": []}
|
|
23
|
+
|
|
24
|
+
class H(BaseHTTPRequestHandler):
|
|
25
|
+
def do_GET(self):
|
|
26
|
+
sent, etag = self.headers.get("If-None-Match"), f'"{hash(state["body"])}"'
|
|
27
|
+
state["hits"].append(sent)
|
|
28
|
+
if state["fail"]:
|
|
29
|
+
return self.send_error(500)
|
|
30
|
+
if sent == etag:
|
|
31
|
+
self.send_response(304)
|
|
32
|
+
return self.end_headers()
|
|
33
|
+
body = gzip.compress(state["body"])
|
|
34
|
+
self.send_response(200)
|
|
35
|
+
for k, v in ("ETag", etag), ("Content-Encoding", "gzip"), ("Content-Length", len(body)):
|
|
36
|
+
self.send_header(k, str(v))
|
|
37
|
+
self.end_headers()
|
|
38
|
+
self.wfile.write(body)
|
|
39
|
+
|
|
40
|
+
def log_message(self, *_):
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
srv = ThreadingHTTPServer(("127.0.0.1", 0), H)
|
|
44
|
+
threading.Thread(target=srv.serve_forever, daemon=True).start()
|
|
45
|
+
yield f"http://127.0.0.1:{srv.server_port}/catalog.json", state
|
|
46
|
+
srv.shutdown()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def ids(items):
|
|
50
|
+
return [i.id for i in items]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_get_and_item_helpers(catalog):
|
|
54
|
+
item = catalog.get("brain/glow")
|
|
55
|
+
assert item == Item(
|
|
56
|
+
"brain/glow", "psychedelic", "side view brain with a rainbow glow",
|
|
57
|
+
("brain", "rainbow", "glow"),
|
|
58
|
+
)
|
|
59
|
+
assert catalog.get("nope/x") is None
|
|
60
|
+
assert len(catalog) == 5 and ids(catalog)[0] == "brain/glow"
|
|
61
|
+
assert item.url() == f"{SITE}/brain/glow.webp"
|
|
62
|
+
assert item.url("gif", "https://x.test/") == "https://x.test/brain/glow.gif"
|
|
63
|
+
with pytest.raises(AttributeError):
|
|
64
|
+
item.id = "other"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_tags(catalog):
|
|
68
|
+
assert catalog.tags() == [
|
|
69
|
+
("brain", 2), ("cat", 2), ("catnip", 1), ("cozy", 1), ("glow", 1),
|
|
70
|
+
("neuron", 1), ("night-sky", 1), ("rainbow", 1),
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_sections(catalog):
|
|
75
|
+
assert catalog.sections() == [("animals", 3), ("psychedelic", 1), ("science", 1)]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_random(catalog):
|
|
79
|
+
assert catalog.random(section="science").id == "brain/zap"
|
|
80
|
+
assert catalog.random("zzz") is None
|
|
81
|
+
assert {catalog.random("cat").id for _ in range(200)} == {"cat/blink", "cat/loaf", "misc/dot"}
|
|
82
|
+
assert {catalog.random().id for _ in range(200)} == set(ids(catalog))
|
|
83
|
+
assert {catalog.imfeelinglucky().id for _ in range(200)} == set(ids(catalog))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_search_ranking(catalog):
|
|
87
|
+
# glow: exact tag (brain/glow) beats description word (cat/blink)
|
|
88
|
+
assert ids(catalog.search("glow")) == ["brain/glow", "cat/blink"]
|
|
89
|
+
# "bra": tag prefix on both brains, id order; "cat" has no match
|
|
90
|
+
assert ids(catalog.search("bra")) == ["brain/glow", "brain/zap"]
|
|
91
|
+
# terms add up: 6 for brain/glow, 3 for brain/zap, 1 for cat/blink (description word)
|
|
92
|
+
assert ids(catalog.search("Brain, GLOW!")) == ["brain/glow", "brain/zap", "cat/blink"]
|
|
93
|
+
# exact tag (cat/blink, cat/loaf) beats tag prefix (misc/dot's catnip)
|
|
94
|
+
assert ids(catalog.search("cat")) == ["cat/blink", "cat/loaf", "misc/dot"]
|
|
95
|
+
# hyphenated term: exact tag, then a description with every part
|
|
96
|
+
assert ids(catalog.search("night-sky")) == ["cat/blink", "cat/loaf"]
|
|
97
|
+
assert catalog.search("zzz") == []
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_search_filters_and_limit(catalog):
|
|
101
|
+
assert ids(catalog.search()) == [
|
|
102
|
+
"brain/glow", "brain/zap", "cat/blink", "cat/loaf", "misc/dot",
|
|
103
|
+
]
|
|
104
|
+
assert ids(catalog.search(tags=["Brain"], section="science")) == ["brain/zap"]
|
|
105
|
+
assert ids(catalog.search(tags=("cat", "cozy"))) == ["cat/loaf"]
|
|
106
|
+
assert ids(catalog.search(tags="cat")) == ["cat/blink", "cat/loaf"]
|
|
107
|
+
assert ids(catalog.search("glow", section="animals")) == ["cat/blink"]
|
|
108
|
+
assert ids(catalog.search(limit=2)) == ["brain/glow", "brain/zap"]
|
|
109
|
+
assert len(catalog.search(limit=None)) == 5
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_fetches_once_per_ttl(server):
|
|
113
|
+
url, state = server
|
|
114
|
+
c = Catalog(url)
|
|
115
|
+
c.get("cat/loaf"), c.search("cat"), c.tags(), len(c)
|
|
116
|
+
assert state["hits"] == [None]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_refresh(server):
|
|
120
|
+
url, state = server
|
|
121
|
+
c = Catalog(url, ttl=0)
|
|
122
|
+
assert len(c) == 5
|
|
123
|
+
assert len(c) == 5 and state["hits"][-1] # sent the ETag, got a 304, kept the items
|
|
124
|
+
state["body"] = json.dumps({"items": [{"id": "a/b", "section": "s", "description": "new"}]}).encode()
|
|
125
|
+
assert ids(c) == ["a/b"]
|
|
126
|
+
state["fail"] = True
|
|
127
|
+
assert ids(c) == ["a/b"] # a failed check keeps what it has
|
|
128
|
+
with pytest.raises(OSError):
|
|
129
|
+
c.refresh(force=True)
|
|
130
|
+
with pytest.raises(OSError):
|
|
131
|
+
Catalog(url).get("a/b") # nothing to fall back on
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Search the vibegif catalog, fetched from the site and refetched once stale."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import gzip
|
|
6
|
+
import json
|
|
7
|
+
import re
|
|
8
|
+
import time
|
|
9
|
+
from collections import Counter
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from random import choice
|
|
12
|
+
from typing import Iterable, Iterator
|
|
13
|
+
from urllib.request import Request, urlopen
|
|
14
|
+
|
|
15
|
+
SITE = "https://vibegif.direct-img.link"
|
|
16
|
+
_TERM = re.compile(r"[a-z0-9][a-z0-9-]*")
|
|
17
|
+
_WORD = re.compile(r"[a-z0-9]+")
|
|
18
|
+
# Per query term the best match wins; a term's scores add up across the query.
|
|
19
|
+
_TAG_EXACT, _TAG_PREFIX, _DESC_WORD = 3, 2, 1
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class Item:
|
|
24
|
+
id: str
|
|
25
|
+
section: str
|
|
26
|
+
description: str
|
|
27
|
+
tags: tuple[str, ...] = ()
|
|
28
|
+
|
|
29
|
+
def url(self, fmt: str = "webp", site: str = SITE) -> str:
|
|
30
|
+
"""Hotlinkable URL. Every item has a webp; most also have a gif."""
|
|
31
|
+
return f"{site.rstrip('/')}/{self.id}.{fmt}"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Catalog:
|
|
35
|
+
"""The live catalog, fetched on first use and refetched once older than `ttl` seconds."""
|
|
36
|
+
|
|
37
|
+
def __init__(self, url: str = f"{SITE}/catalog.json", ttl: float = 3600):
|
|
38
|
+
self.url, self.ttl = url, ttl
|
|
39
|
+
self._items: tuple[Item, ...] = ()
|
|
40
|
+
self._etag: str | None = None
|
|
41
|
+
self._at = 0.0
|
|
42
|
+
|
|
43
|
+
def refresh(self, force: bool = False) -> Catalog:
|
|
44
|
+
"""Refetch if stale or `force`d; an unchanged catalog costs a 304.
|
|
45
|
+
|
|
46
|
+
A failed refetch keeps the items already held, unless forced or there are none yet.
|
|
47
|
+
"""
|
|
48
|
+
if not force and self._items and time.monotonic() - self._at < self.ttl:
|
|
49
|
+
return self
|
|
50
|
+
h = {"User-Agent": "vibegif-python", "Accept-Encoding": "gzip"}
|
|
51
|
+
if self._items and self._etag:
|
|
52
|
+
h["If-None-Match"] = self._etag
|
|
53
|
+
try:
|
|
54
|
+
with urlopen(Request(self.url, headers=h), timeout=30) as r:
|
|
55
|
+
body, etag = r.read(), r.headers.get("ETag")
|
|
56
|
+
if r.headers.get("Content-Encoding") == "gzip":
|
|
57
|
+
body = gzip.decompress(body)
|
|
58
|
+
self._index(json.loads(body)["items"])
|
|
59
|
+
self._etag = etag
|
|
60
|
+
except (OSError, ValueError, KeyError) as e:
|
|
61
|
+
if getattr(e, "code", None) != 304 and (force or not self._items):
|
|
62
|
+
raise
|
|
63
|
+
self._at = time.monotonic()
|
|
64
|
+
return self
|
|
65
|
+
|
|
66
|
+
def _index(self, raw: list[dict]) -> None:
|
|
67
|
+
items = tuple(
|
|
68
|
+
Item(d["id"], d["section"], d["description"], tuple(d.get("tags", ())))
|
|
69
|
+
for d in raw
|
|
70
|
+
)
|
|
71
|
+
self._by_id = {i.id: i for i in items}
|
|
72
|
+
self._rows = [
|
|
73
|
+
(i, frozenset(i.tags), frozenset(_WORD.findall(i.description.lower())))
|
|
74
|
+
for i in items
|
|
75
|
+
]
|
|
76
|
+
self._items = items
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def items(self) -> tuple[Item, ...]:
|
|
80
|
+
return self.refresh()._items
|
|
81
|
+
|
|
82
|
+
def __len__(self) -> int:
|
|
83
|
+
return len(self.items)
|
|
84
|
+
|
|
85
|
+
def __iter__(self) -> Iterator[Item]:
|
|
86
|
+
return iter(self.items)
|
|
87
|
+
|
|
88
|
+
def get(self, id: str) -> Item | None:
|
|
89
|
+
return self.refresh()._by_id.get(id)
|
|
90
|
+
|
|
91
|
+
def sections(self) -> list[tuple[str, int]]:
|
|
92
|
+
"""(section, item count) pairs, biggest first, then alphabetical."""
|
|
93
|
+
return _ranked(i.section for i in self.items)
|
|
94
|
+
|
|
95
|
+
def tags(self) -> list[tuple[str, int]]:
|
|
96
|
+
"""(tag, item count) pairs, most used first, then alphabetical."""
|
|
97
|
+
return _ranked(t for i in self.items for t in i.tags)
|
|
98
|
+
|
|
99
|
+
def random(
|
|
100
|
+
self, query: str = "", *, tags: Iterable[str] = (), section: str | None = None
|
|
101
|
+
) -> Item | None:
|
|
102
|
+
"""One random item out of everything `search` would match, or None."""
|
|
103
|
+
hits = self.search(query, tags=tags, section=section, limit=None)
|
|
104
|
+
return choice(hits) if hits else None
|
|
105
|
+
|
|
106
|
+
def imfeelinglucky(self) -> Item:
|
|
107
|
+
"""Any item at all."""
|
|
108
|
+
return choice(self.items)
|
|
109
|
+
|
|
110
|
+
def search(
|
|
111
|
+
self,
|
|
112
|
+
query: str = "",
|
|
113
|
+
*,
|
|
114
|
+
tags: Iterable[str] = (),
|
|
115
|
+
section: str | None = None,
|
|
116
|
+
limit: int | None = 50,
|
|
117
|
+
) -> list[Item]:
|
|
118
|
+
"""Items matching `query`, best first.
|
|
119
|
+
|
|
120
|
+
`tags` (all required) and `section` are hard filters. Each query term
|
|
121
|
+
scores an exact tag match above a tag-prefix match above a description
|
|
122
|
+
word; items matching no term are dropped, and ties break by id. An empty
|
|
123
|
+
query returns everything that passes the filters, in id order. `limit=None`
|
|
124
|
+
returns every match.
|
|
125
|
+
"""
|
|
126
|
+
need = {t.lower() for t in ((tags,) if isinstance(tags, str) else tags)}
|
|
127
|
+
terms = _TERM.findall(query.lower())
|
|
128
|
+
scored = []
|
|
129
|
+
for item, item_tags, words in self.refresh()._rows:
|
|
130
|
+
if section not in (None, item.section) or not need <= item_tags:
|
|
131
|
+
continue
|
|
132
|
+
score = sum(_score(t, item_tags, words) for t in terms)
|
|
133
|
+
if score or not terms:
|
|
134
|
+
scored.append((-score, item.id, item))
|
|
135
|
+
scored.sort(key=lambda s: s[:2])
|
|
136
|
+
return [s[2] for s in scored[:limit]]
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _ranked(names: Iterable[str]) -> list[tuple[str, int]]:
|
|
140
|
+
return sorted(Counter(names).items(), key=lambda kv: (-kv[1], kv[0]))
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _score(term: str, tags: frozenset[str], words: frozenset[str]) -> int:
|
|
144
|
+
if term in tags:
|
|
145
|
+
return _TAG_EXACT
|
|
146
|
+
if any(t.startswith(term) for t in tags):
|
|
147
|
+
return _TAG_PREFIX
|
|
148
|
+
# A hyphenated term matches a description that has all its parts.
|
|
149
|
+
return _DESC_WORD if all(p in words for p in term.split("-")) else 0
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vibegif
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Search vibegif's public domain animated webp/gifs and get hotlinkable URLs
|
|
5
|
+
License-Expression: CC0-1.0
|
|
6
|
+
Project-URL: Homepage, https://vibegif.direct-img.link
|
|
7
|
+
Project-URL: Source, https://github.com/direct-img/vibegif.direct-img.link/tree/main/packages/python
|
|
8
|
+
Keywords: gif,webp,animation,cc0,public-domain
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Provides-Extra: test
|
|
12
|
+
Requires-Dist: pytest; extra == "test"
|
|
13
|
+
|
|
14
|
+
<img src="https://vibegif.direct-img.link/cat/blink.webp" align="right" width="96" alt="blinking cat">
|
|
15
|
+
|
|
16
|
+
# vibegif (Python)
|
|
17
|
+
|
|
18
|
+
Search [vibegif](https://vibegif.direct-img.link)'s 1,400+ public domain (CC0) animations from Python and get URLs you can hotlink. No dependencies, Python 3.10+.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install vibegif
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from vibegif import Catalog
|
|
26
|
+
|
|
27
|
+
catalog = Catalog()
|
|
28
|
+
for item in catalog.search("rainbow brain", limit=3):
|
|
29
|
+
print(item.id, item.url()) # brain/glow https://vibegif.direct-img.link/brain/glow.webp
|
|
30
|
+
|
|
31
|
+
catalog.search("spinner", section="loader") # section is a hard filter
|
|
32
|
+
catalog.search("cat", tags=["calm", "night"]) # so are tags, every one required
|
|
33
|
+
catalog.random(section="loader") # one random match, or None
|
|
34
|
+
catalog.imfeelinglucky() # any anim at all
|
|
35
|
+
catalog.get("cat/blink") # Item, or None
|
|
36
|
+
catalog.sections() # [("animedev", 150), ("inkwash", 150), ...]
|
|
37
|
+
catalog.tags() # [("calm", 369), ("happy", 329), ...]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
<img src="https://vibegif.direct-img.link/python/charm.webp" align="right" width="128" alt="girl playing a flute as a blue and yellow snake rises from a basket">
|
|
41
|
+
|
|
42
|
+
An `Item` has `id`, `section`, `description` and `tags`. `item.url()` is its webp, which every item has, and `item.url("gif")` its gif, which most have.
|
|
43
|
+
|
|
44
|
+
Search scores each query term: an exact tag beats a tag prefix, which beats a word in the description. Results come best first, ties break by id, and an empty query returns everything that passes the filters, in id order. It returns 50 at most unless you pass `limit` (`limit=None` for every match).
|
|
45
|
+
|
|
46
|
+
The catalog comes from [catalog.json](https://vibegif.direct-img.link/catalog.json). It's fetched on first use and checked again once it's an hour old (`Catalog(ttl=seconds)`, or `catalog.refresh(force=True)` to check now). An unchanged catalog costs a 304, and a failed check keeps the items already fetched.
|
|
47
|
+
|
|
48
|
+
Tests: `uv run --with pytest pytest`
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
MANIFEST.in
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
tests/catalog.json
|
|
5
|
+
tests/test_catalog.py
|
|
6
|
+
vibegif/__init__.py
|
|
7
|
+
vibegif/catalog.py
|
|
8
|
+
vibegif.egg-info/PKG-INFO
|
|
9
|
+
vibegif.egg-info/SOURCES.txt
|
|
10
|
+
vibegif.egg-info/dependency_links.txt
|
|
11
|
+
vibegif.egg-info/requires.txt
|
|
12
|
+
vibegif.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vibegif
|