~torresjrjr/linkchanbot

36cb97398f2b0d080a57b252480c39d301bdf0da — Byron Torres 2 years ago 78c4684
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.
3 files changed, 41 insertions(+), 38 deletions(-)

M linkchanbot
D sample.config/queries.json
M sample.config/services.json
M linkchanbot => linkchanbot +5 -8
@@ 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()

D sample.config/queries.json => sample.config/queries.json +0 -11
@@ 1,11 0,0 @@
{
    "twitter.com": [],
    "instagram.com": [],
    "youtube.com": [
        "v",
        "list"
    ],
    "reddit.com": [
		"context"
	]
}

M sample.config/services.json => sample.config/services.json +36 -19
@@ 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"
		]
	}
}