Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Migrate `utilities/dead_link` to PDM (#4623)

Olga Bulatf6cdfe15a0a6
diff --git a/utilities/dead_links/Pipfile b/utilities/dead_links/Pipfile
deleted file mode 100644
index 70444b6ee..000000000
--- a/utilities/dead_links/Pipfile
+++ /dev/null
@@ -1,13 +0,0 @@
-[[source]]
-url = "https://pypi.org/simple"
-verify_ssl = true
-name = "pypi"
-
-[packages]
-py-redis = "*"
-tqdm = "*"
-
-[dev-packages]
-
-[requires]
-python_version = "3.10"
diff --git a/utilities/dead_links/Pipfile.lock b/utilities/dead_links/Pipfile.lock
deleted file mode 100644
index 602dd1540..000000000
--- a/utilities/dead_links/Pipfile.lock
+++ /dev/null
@@ -1,54 +0,0 @@
-{
- "_meta": {
- "hash": {
- "sha256": "b4d6d7bd108fe6a44b7d0cbfed7e7745674a28ff87acf6bdde203a0f0126c524"
- },
- "pipfile-spec": 6,
- "requires": {
- "python_version": "3.10"
- },
- "sources": [
- {
- "name": "pypi",
- "url": "https://pypi.org/simple",
- "verify_ssl": true
- }
- ]
- },
- "default": {
- "async-timeout": {
- "hashes": [
- "sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15",
- "sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"
- ],
- "markers": "python_version >= '3.6'",
- "version": "==4.0.2"
- },
- "py-redis": {
- "hashes": [
- "sha256:c6ec84fc8251398236f1c0b98f6ad09dc61667c29664efb7d44e95dc5763782e",
- "sha256:f64cfa44dcd69af19348234ba96a2a943e3793248dcbdb267bdefb1751f4002f"
- ],
- "index": "pypi",
- "version": "==1.1.1"
- },
- "redis": {
- "hashes": [
- "sha256:68226f7ede928db8302f29ab088a157f41061fa946b7ae865452b6d7838bbffb",
- "sha256:da92a39fec86438d3f1e2a1db33c312985806954fe860120b582a8430e231d8f"
- ],
- "index": "pypi",
- "version": "==4.4.4"
- },
- "tqdm": {
- "hashes": [
- "sha256:23097a41eba115ba99ecae40d06444c15d1c0c698d527a01c6c8bd1c5d0647e5",
- "sha256:4f41d54107ff9a223dca80b53efe4fb654c67efaba7f47bada3ee9d50e05bd53"
- ],
- "index": "pypi",
- "markers": "python_version >= '3.7'",
- "version": "==4.66.3"
- }
- },
- "develop": {}
-}
diff --git a/utilities/dead_links/dead_link_tally.py b/utilities/dead_links/dead_link_tally.py
index 77562a2a5..620b8830b 100644
--- a/utilities/dead_links/dead_link_tally.py
+++ b/utilities/dead_links/dead_link_tally.py
@@ -10,19 +10,12 @@ import pprint
from collections import defaultdict
from urllib.parse import urlparse
+import click
from redis import Redis
from tqdm import tqdm
-redis = Redis("localhost", decode_responses=True)
-
-cursor = 0
-
-tallies = defaultdict(dict)
-errors = dict()
-
-
-def handle_matches(matches):
+def handle_matches(redis, matches, tallies, errors):
values = redis.mget(matches)
for value, match in zip(values, matches):
try:
@@ -45,26 +38,62 @@ def handle_matches(matches):
errors[value] = e
-total_to_process = redis.eval("return #redis.pcall('keys', 'valid:*')", 0)
-
-with tqdm(total=total_to_process, miniters=10) as pbar:
- cursor, matches = redis.scan(cursor=0, match="valid:*", count=250)
- handle_matches(matches)
- pbar.update(len(matches))
- iter_count = 1
-
- while cursor != 0:
- cursor, matches = redis.scan(cursor=cursor, match="valid:*", count=250)
- handle_matches(matches)
+ "--host",
+ help="Redis host to connect to",
+ type=str,
+ default="localhost",
+)
+ "--port",
+ help="Port to connect to",
+ type=int,
+ default=None,
+ show_default=True,
+)
+def main(host: str, port: int | None):
+ port_str = f":{port}" if port is not None else ""
+ click.echo(f"Connecting to Redis cluster at {host}{port_str}")
+
+ redis_params = {"host": host, "decode_responses": True}
+ if port is not None:
+ redis_params["port"] = port
+
+ redis = Redis(**redis_params)
+ try:
+ redis.ping()
+ except Exception as e:
+ click.echo(f"Error connecting to Redis: {e}")
+ return
+
+ tallies = defaultdict(dict)
+ errors = dict()
+
+ total_to_process = redis.eval("return #redis.pcall('keys', 'valid:*')", 0)
+
+ with tqdm(total=total_to_process, miniters=10) as pbar:
+ cursor, matches = redis.scan(cursor=0, match="valid:*", count=250)
+ handle_matches(redis, matches, tallies, errors)
pbar.update(len(matches))
- iter_count += 1
- if iter_count % 10 == 0:
- # only print each 10 iterations to ease I/O time spent
- pprint.pprint(dict(cursor=cursor, **tallies), compact=True)
-
-
-print("\n\n\n\n============= FINAL RESULTS ============= \n\n")
-pprint.pprint(tallies)
-
-print("\n\n\n==================== ERRORS ===============\n\n")
-pprint.pprint(errors)
+ iter_count = 1
+
+ while cursor != 0:
+ cursor, matches = redis.scan(cursor=cursor, match="valid:*", count=250)
+ handle_matches(redis, matches, tallies, errors)
+ pbar.update(len(matches))
+ iter_count += 1
+ if iter_count % 10 == 0:
+ # only print each 10 iterations to ease I/O time spent
+ tqdm.write(
+ pprint.pformat(dict(cursor=cursor, **tallies), compact=True) + "\n"
+ )
+ print("\n\n\n\n============= FINAL RESULTS ============= \n\n")
+ pprint.pprint(tallies)
+
+ print("\n\n\n==================== ERRORS ===============\n\n")
+ pprint.pprint(errors)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/utilities/dead_links/pdm.lock b/utilities/dead_links/pdm.lock
new file mode 100644
index 000000000..37d37a9a1
--- /dev/null
+++ b/utilities/dead_links/pdm.lock
@@ -0,0 +1,59 @@
+# This file is @generated by PDM.
+# It is not intended for manual editing.
+
+[metadata]
+groups = ["default"]
+strategy = ["cross_platform", "inherit_metadata"]
+lock_version = "4.4.2"
+content_hash = "sha256:f70988441f8f0c8ceb658fcdc107d4e2e2b8e0d6956c0f32d9e4ae525a3ac0d3"
+
+[[package]]
+name = "click"
+version = "8.1.7"
+requires_python = ">=3.7"
+summary = "Composable command line interface toolkit"
+groups = ["default"]
+dependencies = [
+ "colorama; platform_system == \"Windows\"",
+]
+files = [
+ {file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
+ {file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"},
+]
+
+[[package]]
+name = "colorama"
+version = "0.4.6"
+requires_python = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
+summary = "Cross-platform colored terminal text."
+groups = ["default"]
+marker = "platform_system == \"Windows\""
+files = [
+ {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
+ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
+]
+
+[[package]]
+name = "redis"
+version = "5.0.7"
+requires_python = ">=3.7"
+summary = "Python client for Redis database and key-value store"
+groups = ["default"]
+files = [
+ {file = "redis-5.0.7-py3-none-any.whl", hash = "sha256:0e479e24da960c690be5d9b96d21f7b918a98c0cf49af3b6fafaa0753f93a0db"},
+ {file = "redis-5.0.7.tar.gz", hash = "sha256:8f611490b93c8109b50adc317b31bfd84fff31def3475b92e7e80bf39f48175b"},
+]
+
+[[package]]
+name = "tqdm"
+version = "4.66.4"
+requires_python = ">=3.7"
+summary = "Fast, Extensible Progress Meter"
+groups = ["default"]
+dependencies = [
+ "colorama; platform_system == \"Windows\"",
+]
+files = [
+ {file = "tqdm-4.66.4-py3-none-any.whl", hash = "sha256:b75ca56b413b030bc3f00af51fd2c1a1a5eac6a0c1cca83cbb37a5c52abce644"},
+ {file = "tqdm-4.66.4.tar.gz", hash = "sha256:e4d936c9de8727928f3be6079590e97d9abfe8d39a590be678eb5919ffc186bb"},
+]
diff --git a/utilities/dead_links/pyproject.toml b/utilities/dead_links/pyproject.toml
new file mode 100644
index 000000000..8d7feff77
--- /dev/null
+++ b/utilities/dead_links/pyproject.toml
@@ -0,0 +1,16 @@
+[project]
+name = "dead_links"
+version = "0.0.0"
+description = "Retrieves dead link tallies from Openverse API Redis"
+authors = [
+ {name = "Openverse Contributors", email = "[email protected]"},
+]
+dependencies = [
+ "redis >= 5.0",
+ "tqdm >= 4.66",
+ "click >=8.1.7",
+]
+requires-python = "==3.12.*"
+
+[tool.pdm]
+distribution = false