From c9dee18d70d79f88197bd6ef35aa1fb88318aea1 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 26 Aug 2020 10:18:10 +0100 Subject: [PATCH] system/station providers: Sanitise {system,station}_url logic * Make all plugins use `requests.utils.requote_uri()` * Make all plugins use roughly the same logic, without if/else trees (as the bodies do a `return ...`), ending with `return ''` if input parameters are None. This throws away the inara fallback to `this.station or this.system` as it's unlikely the in-plugin tracking did a better job than the monitor.py code. --- plugins/eddb.py | 11 ++++++----- plugins/edsm.py | 17 ++++++++++------- plugins/inara.py | 7 ++++--- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/plugins/eddb.py b/plugins/eddb.py index 211944cc..8460fa55 100644 --- a/plugins/eddb.py +++ b/plugins/eddb.py @@ -46,16 +46,17 @@ this.station_marketid = None def system_url(system_name: str) -> str: if this.system_address: return requests.utils.requote_uri(f'https://eddb.io/system/ed-address/{this.system_address}') - elif system_name: + + if system_name: return requests.utils.requote_uri(f'https://eddb.io/system/name/{system_name}') - else: - return '' + + return '' def station_url(system_name: str, station_name: str) -> str: if this.station_marketid: return requests.utils.requote_uri(f'https://eddb.io/station/market-id/{this.station_marketid}') - else: - return system_url('') + + return system_url(system_name) def plugin_start3(plugin_dir): return 'eddb' diff --git a/plugins/edsm.py b/plugins/edsm.py index bcab77b4..4be1ff29 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -14,8 +14,6 @@ import json import requests import sys -import time -import urllib.request, urllib.error, urllib.parse from queue import Queue from threading import Thread @@ -59,14 +57,19 @@ STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00 # Main window clicks def system_url(system_name): - return 'https://www.edsm.net/en/system?systemName=%s' % urllib.parse.quote(system_name) + if system_name: + return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}') + + return '' def station_url(system_name, station_name): - if station_name: - return 'https://www.edsm.net/en/system?systemName=%s&stationName=%s' % (urllib.parse.quote(system_name), urllib.parse.quote(station_name)) - else: - return 'https://www.edsm.net/en/system?systemName=%s&stationName=ALL' % urllib.parse.quote(system_name) + if system_name and station_name: + return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}&stationName={station_name}') + if system_name: + return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}&stationName=ALL') + + return '' def plugin_start3(plugin_dir): # Can't be earlier since can only call PhotoImage after window is created diff --git a/plugins/inara.py b/plugins/inara.py index e13e9d78..660c8c01 100644 --- a/plugins/inara.py +++ b/plugins/inara.py @@ -76,12 +76,13 @@ def system_url(system_name): return this.system def station_url(system_name, station_name): + if system_name and station_name: + return requests.utils.requote_uri(f'https://inara.cz/galaxy-station/?search={system_name}%20[{station_name}]') + if system_name: - if station_name: - return requests.utils.requote_uri(f'https://inara.cz/galaxy-station/?search={system_name}%20[{station_name}]') return system_url(system_name) - return this.station or this.system + return '' def plugin_start3(plugin_dir):