linkchanbot

Telegram bot frontend proxy link substituter
Log | Files | Refs | README | LICENSE

commit 36cb97398f2b0d080a57b252480c39d301bdf0da
parent 78c46842f8ad55171590e1889c8157e10590a07d
Author: Byron Torres <b@torresjrjr.com>
Date:   Wed, 31 Mar 2021 13:03:25 +0100

Remove queries.json, move config to services.json

Having two files for services is mistake prone. Similar to database
schemas, it's useful to put all services related data in the same file.

This commit moves the content of queries.json into services.json, and
refactors linkchanbot to handle that. This means one less global
variable, and less configuration logic.

This should not be breaking but a minor inconvenience. Simply remove
queries.json and remove (or backup) services.json, than reinstall.

Diffstat:
Mlinkchanbot | 13+++++--------
Dsample.config/queries.json | 11-----------
Msample.config/services.json | 55++++++++++++++++++++++++++++++++++++-------------------
3 files changed, 41 insertions(+), 38 deletions(-)

diff --git a/linkchanbot b/linkchanbot @@ -109,7 +109,7 @@ def init(args): sys_share_dir = pathlib.Path('/usr/local/share/linkchan') - config_files = ('bot.cfg', 'alts.json', 'services.json', 'queries.json') + config_files = ('bot.cfg', 'alts.json', 'services.json') # Copy system global config files to local XDG config dir. # Fail if files not found. @@ -159,15 +159,12 @@ def init(args): global ALTS global SERVICES - global QUERIES try: with open(config_dir/'alts.json', 'r') as file: ALTS = json.load(file) with open(config_dir/'services.json', 'r') as file: SERVICES = json.load(file) - with open(config_dir/'queries.json', 'r') as file: - QUERIES = json.load(file) except FileNotFoundError as e: stderr("Error: Missing config file:", e) exit(1) @@ -270,16 +267,16 @@ def mk_newlinks(link): if url.netloc in SERVICES.keys(): service = url.netloc else: - for main, others in SERVICES.items(): - if url.netloc in others: - service = main + for main_domain, service_data in SERVICES.items(): + if url.netloc in service_data['alt_domains']: + service = main_domain break else: # Fail if service is unrecognised return [False] # Keep only allowed URL queries - allowed_queries = QUERIES.get(service) or [] + allowed_queries = SERVICES[service].get('query_whitelist') or [] old_queries = parse_qs(url.query, keep_blank_values=True) new_queries = { query:v for (query,v) in old_queries.items() diff --git a/sample.config/queries.json b/sample.config/queries.json @@ -1,11 +0,0 @@ -{ - "twitter.com": [], - "instagram.com": [], - "youtube.com": [ - "v", - "list" - ], - "reddit.com": [ - "context" - ] -} diff --git a/sample.config/services.json b/sample.config/services.json @@ -1,21 +1,38 @@ { - "twitter.com": [ - "www.twitter.com", - "mobile.twitter.com", - "m.twitter.com" - ], - "instagram.com": [ - "www.instagram.com", - "m.instagram.com" - ], - "youtube.com": [ - "www.youtube.com", - "m.youtube.com", - "youtu.be" - ], - "reddit.com": [ - "www.reddit.com", - "m.reddit.com", - "redd.it" - ] + "twitter.com": { + "alt_domains": [ + "www.twitter.com", + "mobile.twitter.com", + "m.twitter.com" + ], + "query_whitelist": [] + }, + "instagram.com": { + "alt_domains": [ + "www.instagram.com", + "m.instagram.com" + ], + "query_whitelist": [] + }, + "youtube.com": { + "alt_domains": [ + "www.youtube.com", + "m.youtube.com", + "youtu.be" + ], + "query_whitelist": [ + "v", + "list" + ] + }, + "reddit.com": { + "alt_domains": [ + "www.reddit.com", + "m.reddit.com", + "redd.it" + ], + "query_whitelist": [ + "context" + ] + } }