Create hashtag lookups for map links

merge-requests/28/head
Bob Mottram 2022-08-22 11:42:39 +01:00
parent 2b2fd4fde1
commit c0176832a9
3 changed files with 130 additions and 19 deletions

View File

@ -136,6 +136,9 @@ from content import load_dogwhistles
from content import valid_url_lengths
from content import remove_script
from threads import begin_thread
from maps import get_map_links_from_post_content
from maps import get_location_from_tags
from maps import add_tag_map_links
def cache_svg_images(session, base_dir: str, http_prefix: str,
@ -327,6 +330,31 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str,
print('Creating tags directory')
os.mkdir(tags_dir)
# obtain any map links and these can be associated with hashtags
# get geolocations from content
map_links = []
published = None
if post_json_object['object'].get('content'):
published = post_json_object['object']['published']
post_content = post_json_object['object']['content']
map_links += get_map_links_from_post_content(post_content)
# get geolocation from tags
location_str = \
get_location_from_tags(post_json_object['object']['tag'])
if location_str:
if '://' in location_str and '.' in location_str:
if location_str not in map_links:
map_links.append(location_str)
tag_maps_dir = base_dir + '/tagmaps'
if map_links:
# add tagmaps directory if it doesn't exist
if not os.path.isdir(tag_maps_dir):
print('Creating tagmaps directory')
os.mkdir(tag_maps_dir)
post_url = remove_id_ending(post_json_object['id'])
post_url = post_url.replace('/', '#')
hashtags_ctr = 0
for tag in post_json_object['object']['tag']:
if not tag.get('type'):
@ -341,12 +369,13 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str,
if not valid_hash_tag(tag_name):
continue
tags_filename = tags_dir + '/' + tag_name + '.txt'
post_url = remove_id_ending(post_json_object['id'])
post_url = post_url.replace('/', '#')
days_diff = datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)
days_since_epoch = days_diff.days
tag_line = \
str(days_since_epoch) + ' ' + nickname + ' ' + post_url + '\n'
if map_links and published:
add_tag_map_links(tag_maps_dir, tag_name, map_links,
published, post_url)
hashtag_added = False
if not os.path.isfile(tags_filename):
try:

97
maps.py
View File

@ -9,12 +9,29 @@ __module_group__ = "Core"
import os
import datetime
from utils import is_float
from utils import acct_dir
from utils import load_json
from utils import save_json
def get_location_from_tags(tags: []) -> str:
"""Returns the location from the tags list
"""
for tag_item in tags:
if not tag_item.get('type'):
continue
if tag_item['type'] != 'Place':
continue
if not tag_item.get('name'):
continue
if not isinstance(tag_item['name'], str):
continue
return tag_item['name'].replace('\n', ' ')
return None
def _geocoords_from_osm_link(url: str, osm_domain: str) -> (int, float, float):
"""Returns geocoordinates from an OSM map link
"""
@ -360,3 +377,83 @@ def get_map_preferences_coords(base_dir: str,
maps_json['longitude'], \
maps_json['zoom']
return None, None, None
def get_map_links_from_post_content(content: str) -> []:
"""Returns a list of map links
"""
osm_domain = 'openstreetmap.org'
sections = content.split('://')
map_links = []
ctr = 0
for link_str in sections:
if ctr == 0:
ctr += 1
continue
url = link_str
if '"' in link_str:
url = link_str.split('"')[0]
if '<' in link_str:
url = link_str.split('<')[0]
zoom, latitude, longitude = geocoords_from_map_link(url, osm_domain)
if not latitude:
continue
if not longitude:
continue
if not zoom:
continue
if url not in map_links:
map_links.append(url)
ctr += 1
return map_links
def add_tag_map_links(tag_maps_dir: str, tag_name: str,
map_links: [], published: str, post_url: str) -> None:
"""Appends to a hashtag file containing map links
This is used to show a map for a particular hashtag
"""
tag_map_filename = tag_maps_dir + '/' + tag_name + '.txt'
# read the existing map links
existing_map_links = []
if os.path.isfile(tag_map_filename):
try:
with open(tag_map_filename, 'r', encoding='utf-8') as fp_tag:
existing_map_links = fp_tag.read().split('\n')
except OSError:
print('EX: error reading tag map ' + tag_map_filename)
# combine map links with the existing list
secs_since_epoch = \
(datetime.datetime.strptime(published, '%Y-%m-%dT%H:%M:%SZ') -
datetime.datetime(1970, 1, 1)).total_seconds()
links_changed = False
for link in map_links:
line = str(secs_since_epoch) + ' ' + link + ' ' + post_url
if line in existing_map_links:
continue
links_changed = True
existing_map_links = [line] + existing_map_links
if not links_changed:
return
# sort the list of map links
existing_map_links.sort(reverse=True)
map_links_str = ''
ctr = 0
for link in existing_map_links:
if not link:
continue
map_links_str += link + '\n'
ctr += 1
# don't allow the list to grow indefinitely
if ctr >= 2000:
break
# save the tag
try:
with open(tag_map_filename, 'w+', encoding='utf-8') as fp_tag:
fp_tag.write(map_links_str)
except OSError:
print('EX: error writing tag map ' + tag_map_filename)

View File

@ -99,22 +99,7 @@ from maps import html_open_street_map
from maps import set_map_preferences_coords
from maps import set_map_preferences_url
from maps import geocoords_from_map_link
def _get_location_from_tags(tags: []) -> str:
"""Returns the location from the tags list
"""
for tag_item in tags:
if not tag_item.get('type'):
continue
if tag_item['type'] != 'Place':
continue
if not tag_item.get('name'):
continue
if not isinstance(tag_item['name'], str):
continue
return tag_item['name'].replace('\n', ' ')
return None
from maps import get_location_from_tags
def _html_post_metadata_open_graph(domain: str, post_json_object: {},
@ -2298,7 +2283,7 @@ def individual_post_as_html(signing_priv_key_pem: str,
# show embedded map if the location contains a map url
location_str = \
_get_location_from_tags(post_json_object['object']['tag'])
get_location_from_tags(post_json_object['object']['tag'])
if location_str:
if '://' in location_str and '.' in location_str:
bounding_box_degrees = 0.001