2020-11-16 14:26:03 +00:00
|
|
|
# Copyright 2020 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-11-28 03:18:51 +00:00
|
|
|
import re
|
2020-11-28 03:56:19 +00:00
|
|
|
from dateparser import DateDataParser
|
|
|
|
from functools import lru_cache
|
2020-11-16 14:26:03 +00:00
|
|
|
|
2020-11-28 04:06:40 +00:00
|
|
|
parser = DateDataParser(languages=['en'])
|
2020-11-28 03:56:19 +00:00
|
|
|
|
2020-12-29 19:55:45 +00:00
|
|
|
def rank(l, user):
|
2020-12-04 21:11:44 +00:00
|
|
|
# hack hack
|
2020-12-29 19:55:45 +00:00
|
|
|
return sorted(l, key=lambda x: x.user)
|
2020-12-29 19:22:49 +00:00
|
|
|
|
2021-01-09 16:38:01 +00:00
|
|
|
def uprank(l, users):
|
2020-12-29 19:22:49 +00:00
|
|
|
# hack hack
|
|
|
|
def score(n):
|
2021-01-09 16:38:01 +00:00
|
|
|
if n.user in users:
|
|
|
|
# the earlier in the list a user comes, the more highly ranked it is.
|
|
|
|
return users.index(n.user) - len(users) - 1
|
2020-12-29 19:22:49 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
return sorted(l, key=score)
|
2020-12-04 21:11:44 +00:00
|
|
|
|
2021-01-09 16:38:01 +00:00
|
|
|
def filter(l, projection):
|
|
|
|
# hack hack
|
|
|
|
return [n for n in l if n.user == projection]
|
|
|
|
|
2020-11-16 14:26:03 +00:00
|
|
|
def canonical_wikilink(wikilink):
|
2020-11-28 03:56:19 +00:00
|
|
|
|
|
|
|
if is_journal(wikilink):
|
|
|
|
try:
|
2020-11-28 04:06:40 +00:00
|
|
|
wikilink = canonical_date(wikilink)
|
2020-11-28 03:56:19 +00:00
|
|
|
except:
|
|
|
|
# TODO: if we add logging, maybe log that we couldn't parse a date here
|
|
|
|
pass
|
|
|
|
|
2020-11-16 14:26:03 +00:00
|
|
|
# hack hack
|
2020-11-28 02:32:56 +00:00
|
|
|
wikilink = (
|
|
|
|
wikilink.lower()
|
2021-01-09 16:38:01 +00:00
|
|
|
# chars that convert to -, slug-like.
|
2020-11-28 02:32:56 +00:00
|
|
|
.replace(' ', '-')
|
2021-01-09 16:38:01 +00:00
|
|
|
.replace('/', '-')
|
2020-11-28 02:32:56 +00:00
|
|
|
.replace('\'', '')
|
2021-01-09 16:38:01 +00:00
|
|
|
# chars that are elided.
|
|
|
|
.replace('%', '')
|
2020-11-28 02:32:56 +00:00
|
|
|
.replace(',', '')
|
|
|
|
)
|
2020-11-16 14:26:03 +00:00
|
|
|
return wikilink
|
2020-11-28 03:18:51 +00:00
|
|
|
|
|
|
|
|
2020-11-28 03:56:19 +00:00
|
|
|
@lru_cache(maxsize=None)
|
2020-11-28 04:06:40 +00:00
|
|
|
def canonical_date(wikilink):
|
|
|
|
date = parser.get_date_data(wikilink).date_obj
|
|
|
|
try:
|
2020-11-29 02:18:48 +00:00
|
|
|
wikilink = date.isoformat().split("T")[0]
|
2020-11-28 04:06:40 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-11-29 02:18:48 +00:00
|
|
|
return wikilink
|
2020-11-28 03:56:19 +00:00
|
|
|
|
2020-11-28 04:06:40 +00:00
|
|
|
|
|
|
|
@lru_cache(maxsize=1) #memoize this
|
|
|
|
def get_combined_date_regex():
|
2020-11-28 03:18:51 +00:00
|
|
|
date_regexes = [
|
|
|
|
# iso format
|
|
|
|
'[0-9]{4}-[0-9]{2}-[0-9]{2}',
|
|
|
|
# roam format (what a monstrosity!)
|
|
|
|
'(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{1,2}(st|nd|th), [0-9]{4}',
|
2020-11-28 04:06:40 +00:00
|
|
|
# roam format (after filename sanitization)
|
2020-11-28 03:18:51 +00:00
|
|
|
'(january|february|march|april|may|june|july|august|september|october|november|december)-[0-9]{1,2}(st|nd|th)-[0-9]{4}',
|
|
|
|
]
|
|
|
|
|
|
|
|
# combine all the date regexes into one super regex
|
2020-11-28 03:56:19 +00:00
|
|
|
# TODO: it'd really be better to compile this regex once rather than on
|
|
|
|
# each request, but as the knuth would say premature optimization is the
|
|
|
|
# root of all evil, etc. etc.
|
2020-11-28 04:06:40 +00:00
|
|
|
return re.compile(f'^({"|".join(date_regexes)})$')
|
|
|
|
|
2020-11-28 03:18:51 +00:00
|
|
|
|
2020-11-28 04:06:40 +00:00
|
|
|
@lru_cache(maxsize=None)
|
|
|
|
def is_journal(wikilink):
|
|
|
|
return get_combined_date_regex().match(wikilink)
|