From 92edca35211e56c031a4d4c11b8e1a5a1f565fb3 Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 26 Jun 2019 17:11:46 +0200 Subject: [PATCH] Refactored selectors for easier extensibility --- htmlmodules.py | 146 +++++++------------ malojatime.py | 2 + rules/predefined/krateng_artistsingroups.tsv | 3 +- scrobblers/chromium-generic/background.js | 9 +- 4 files changed, 63 insertions(+), 97 deletions(-) diff --git a/htmlmodules.py b/htmlmodules.py index a7e63e1..a611354 100644 --- a/htmlmodules.py +++ b/htmlmodules.py @@ -579,7 +579,6 @@ def module_filterselection(keys,time=True,delimit=False): if time: # all other keys that will not be changed by clicking another filter - #keystr = "?" + compose_querystring(keys,exclude=["since","to","in"]) unchangedkeys = internal_to_uri({**filterkeys,**delimitkeys,**extrakeys}) @@ -601,28 +600,10 @@ def module_filterselection(keys,time=True,delimit=False): # html += "to " # html += "" - from malojatime import today, thisweek, thismonth, thisyear - - ### temp!!! this will not allow weekly rank changes - # weekday = ((now.isoweekday()) % 7) - # weekbegin = now - datetime.timedelta(days=weekday) - # weekend = weekbegin + datetime.timedelta(days=6) - # weekbegin = [weekbegin.year,weekbegin.month,weekbegin.day] - # weekend = [weekend.year,weekend.month,weekend.day] - # weekbeginstr = "/".join((str(num) for num in weekbegin)) - # weekendstr = "/".join((str(num) for num in weekend)) - - + from malojatime import today, thisweek, thismonth, thisyear, alltime # relative to current range - html += "
" - # if timekeys.get("timerange").next(-1) is not None: - # html += "«" - # if timekeys.get("timerange").next(-1) is not None or timekeys.get("timerange").next(1) is not None: - # html += " " + timekeys.get("timerange").desc() + " " - # if timekeys.get("timerange").next(1) is not None: - # html += "»" if timekeys.get("timerange").next(-1) is not None: prevrange = timekeys.get("timerange").next(-1) @@ -639,101 +620,78 @@ def module_filterselection(keys,time=True,delimit=False): # predefined ranges + delimit_ranges = { + "Today":today(), + "This Week":thisweek(), + "This Month":thismonth(), + "This Year":thisyear(), + "All Time":alltime() + } - html += "
" - if timekeys.get("timerange") == today(): - html += "Today" - else: - html += "Today" - html += " | " + optionlist = [] + for option in delimit_ranges: + value = delimit_ranges[option] + link = "?" + compose_querystring(unchangedkeys,internal_to_uri({"timerange":value})) - if timekeys.get("timerange") == thisweek(): - html += "This Week" - else: - html += "This Week" - html += " | " + if timekeys.get("timerange") == value: + optionlist.append("" + option + "") + else: + optionlist.append("" + option + "") - if timekeys.get("timerange") == thismonth(): - html += "This Month" - else: - html += "This Month" - html += " | " + html += "
" + " | ".join(optionlist) + "
" - if timekeys.get("timerange") == thisyear(): - html += "This Year" - else: - html += "This Year" - html += " | " - - if timekeys.get("timerange") is None or timekeys.get("timerange").unlimited(): - html += "All Time" - else: - html += "All Time" - - html += "
" if delimit: - #keystr = "?" + compose_querystring(keys,exclude=["step","stepn"]) unchangedkeys = internal_to_uri({**filterkeys,**timekeys,**extrakeys}) + # STEP # only for this element (delimit selector consists of more than one) unchangedkeys_sub = internal_to_uri({k:delimitkeys[k] for k in delimitkeys if k not in ["step","stepn"]}) - html += "
" - if delimitkeys.get("step") == "day" and delimitkeys.get("stepn") == 1: - html += "Daily" - else: - html += "Daily" - html += " | " + delimit_steps = { + "Daily":{"step":"day","stepn":1}, + "Weekly":{"step":"week","stepn":1}, + "Fortnightly":{"step":"week","stepn":2}, + "Monthly":{"step":"month","stepn":1}, + "Quarterly":{"step":"month","stepn":3}, + "Yearly":{"step":"year","stepn":1} + } - if delimitkeys.get("step") == "week" and delimitkeys.get("stepn") == 1: - html += "Weekly" - else: - html += "Weekly" - html += " | " + optionlist = [] + for option in delimit_steps: + values = delimit_steps[option] + link = "?" + compose_querystring(unchangedkeys,unchangedkeys_sub,internal_to_uri(values)) - if delimitkeys.get("step") == "month" and delimitkeys.get("stepn") == 1: - html += "Monthly" - else: - html += "Monthly" - html += " | " - - if delimitkeys.get("step") == "year" and delimitkeys.get("stepn") == 1: - html += "Yearly" - else: - html += "Yearly" - - html += "
" + if delimitkeys.get("step") == values["step"] and delimitkeys.get("stepn") == values["stepn"]: + optionlist.append("" + option + "") + else: + optionlist.append("" + option + "") + html += "
" + " | ".join(optionlist) + "
" + # TRAIL unchangedkeys_sub = internal_to_uri({k:delimitkeys[k] for k in delimitkeys if k != "trail"}) - html += "
" - if delimitkeys.get("trail") == 1: - html += "Standard" - else: - html += "Standard" - html += " | " + delimit_trails = { + "Standard":1, + "Trailing":2, + "Long Trailing":3, + "Inert":10, + "Cumulative":math.inf + } - if delimitkeys.get("trail") == 2: - html += "Trailing" - else: - html += "Trailing" - html += " | " + optionlist = [] + for option in delimit_trails: + value = delimit_trails[option] + link = "?" + compose_querystring(unchangedkeys,unchangedkeys_sub,internal_to_uri({"trail":value})) - if delimitkeys.get("trail") == 3: - html += "Long Trailing" - else: - html += "Long Trailing" - html += " | " + if delimitkeys.get("trail") == value: + optionlist.append("" + option + "") + else: + optionlist.append("" + option + "") - if delimitkeys.get("trail") == math.inf: - html += "Cumulative" - else: - html += "Cumulative" - - html += "
" + html += "
" + " | ".join(optionlist) + "
" return html diff --git a/malojatime.py b/malojatime.py index 98c635f..a77fd4a 100644 --- a/malojatime.py +++ b/malojatime.py @@ -620,6 +620,8 @@ def thismonth(): def thisyear(): tod = datetime.datetime.utcnow() return MTime(tod.year) +def alltime(): + return MRange(None,None) #def _get_start_of(timestamp,unit): # date = datetime.datetime.utcfromtimestamp(timestamp) diff --git a/rules/predefined/krateng_artistsingroups.tsv b/rules/predefined/krateng_artistsingroups.tsv index 9079410..39204cc 100644 --- a/rules/predefined/krateng_artistsingroups.tsv +++ b/rules/predefined/krateng_artistsingroups.tsv @@ -10,4 +10,5 @@ countas S Club 7 Tina Barrett countas RenoakRhythm Approaching Nirvana countas Shirley Manson Garbage countas Lewis Brindley The Yogscast -countas Sips The Yogscast +countas Sips The Yogscast +countas Sjin The Yogscast diff --git a/scrobblers/chromium-generic/background.js b/scrobblers/chromium-generic/background.js index 417fd59..32d4571 100644 --- a/scrobblers/chromium-generic/background.js +++ b/scrobblers/chromium-generic/background.js @@ -178,8 +178,13 @@ class Controller { actuallyupdate() { this.messageID++; //console.log("Update! Our page is " + this.page + ", our tab id " + this.tabId) - chrome.tabs.executeScript(this.tabId,{"file":"sites/" + pages[this.page]["script"]}); - chrome.tabs.executeScript(this.tabId,{"file":"sitescript.js"}); + try { + chrome.tabs.executeScript(this.tabId,{"file":"sites/" + pages[this.page]["script"]}); + chrome.tabs.executeScript(this.tabId,{"file":"sitescript.js"}); + } + catch (e) { + console.log("Could not run site script. Tab probably closed or something idk.") + } this.alreadyQueued = false; }