-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a report of Youtube API keys usage
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
dispatcher/backend/maint-scripts/list_youtube_api_keys_used.conf.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"cc319f3e8ee3f586646b98c55bc0a7b3d9f27ac3393d1bc982aaef5930cc313a": "large-crashcourse-1", | ||
"9dc879afd8f9a95e7eb30167311dd7346bcc6bdfafc6bd1e217460830323c7ef": "large-mathtiques-2", | ||
"d48ebff8fa1891f9531ab21c3bbfd0d6f1c91b301addb2bda1cea48d52531ef6": "large-sorcier-1", | ||
"79e8cfc372f6fb20769a8339eb606bb17ed6a480279ff02a50f41f6cf944efd4": "large-teded-1", | ||
"480e51b5ee93ee2b209906c8bf8362ddac8bd6d543f87674144ce5e66d167ebb": "large-univers-1", | ||
"659fec208e08d2c8edd96a4ae7a16e71bd824c2ce569a80918fa8f4ab8e06ad7": "medium-youtubes-2", | ||
"db29b7d06057fb992db99430ff19522e4781d88e2a2a2fbb2d3565296d45f722": "madrasa-playlists-2", | ||
"3e2413945d668d47ab151ee1df9cb51e65360ed57c1b89eb8ee435cd47f37baf": "medium-youtubes-1", | ||
"1060528e283299cc54de2f67ea9ab918b1e1ddb461b12b25eff1aba135ea458e": "small-youtubes-1", | ||
"99fe8cfb95cfcf9e8851f01e567d9dd2b246a708aa7fc6b1752feb7320725c0f": "small-youtubes-2", | ||
"6a0cfba941cfc1a4e85952bda5aff424cf95217d3b772777c60ec2d184112025": "madrasa-playlists-1", | ||
"1c94fe405309067bd53b125f5f0c55e1640414a89e5f0075028bb313dde374eb": "large-mathtiques-1" | ||
} |
78 changes: 78 additions & 0 deletions
78
dispatcher/backend/maint-scripts/list_youtube_api_keys_used.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""List usage of API keys for youtube recipes | ||
./list_youtube_api_keys_used.py | ||
Configuration file named "list_youtube_api_keys_used.conf.json" must be placed in | ||
same folder as script and contain a dictionnary of sha256(api_key) => | ||
api_key_display_name | ||
""" | ||
|
||
import hashlib | ||
import json | ||
import pathlib | ||
|
||
import sqlalchemy as sa | ||
import sqlalchemy.orm as so | ||
|
||
import db.models as dbm | ||
from db import dbsession | ||
|
||
|
||
@dbsession | ||
def list_youtube_api_keys_used(session: so.Session, *, display_secrets=False): | ||
known_api_keys = json.loads( | ||
pathlib.Path("list_youtube_api_keys_used.conf.json").read_text() | ||
) | ||
print("Listing schedules") | ||
stmt = ( | ||
sa.select(dbm.Schedule) | ||
.where(dbm.Schedule.config["flags"]["api-key"].astext.is_not(None)) | ||
.order_by(dbm.Schedule.config["flags"]["api-key"].astext) | ||
) | ||
|
||
schedules = list(session.execute(stmt).scalars()) | ||
|
||
print(f"{len(schedules)} schedules found") | ||
print() | ||
|
||
schedules_by_api_key = {} | ||
for schedule in schedules: | ||
api_key = schedule.config["flags"]["api-key"] | ||
hashed_api_key = hashlib.sha256(api_key.encode("utf-8")).hexdigest() | ||
if hashed_api_key not in schedules_by_api_key.keys(): | ||
schedules_by_api_key[hashed_api_key] = { | ||
"api_key": api_key, | ||
"key_name": known_api_keys[hashed_api_key] | ||
if hashed_api_key in known_api_keys | ||
else "unknown", | ||
"schedules": [], | ||
} | ||
schedules_by_api_key[hashed_api_key]["schedules"].append(schedule.name) | ||
|
||
for hashed_api_key, data in sorted( | ||
schedules_by_api_key.items(), key=lambda item: item[1]["key_name"] | ||
): | ||
if hashed_api_key in known_api_keys.keys(): | ||
print(f"Key {known_api_keys[hashed_api_key]}:") | ||
else: | ||
print("Unknown key:") | ||
if display_secrets: | ||
print(f"API key: {data['api_key']}") | ||
for schedule_name in sorted(data["schedules"]): | ||
print(f"- {schedule_name}") | ||
print() | ||
|
||
for hashed_key, key_name in sorted( | ||
known_api_keys.items(), key=lambda item: item[1] | ||
): | ||
if hashed_key not in schedules_by_api_key.keys(): | ||
print(f"Key {key_name} is not used") | ||
|
||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
list_youtube_api_keys_used(display_secrets=False) | ||
print("FINISH!") |