Load city categories

main
Bob Mottram 2023-09-02 15:42:59 +01:00
parent 9da08aeb6b
commit 37499e4417
2 changed files with 74 additions and 0 deletions

View File

@ -38,6 +38,76 @@ def get_hashtag_category(base_dir: str, hashtag: str) -> str:
return ''
def load_city_hashtags(base_dir: str, translate: {}) -> None:
"""create hashtag categories for cities
"""
category_str = 'places'
if translate.get(category_str):
category_str = translate[category_str]
for _, _, files in os.walk(base_dir + '/data/cities'):
for cities_file in files:
if not cities_file.endswith('.txt'):
continue
cities_filename = base_dir + '/data/cities/' + cities_file
if not os.path.isfile(cities_filename):
continue
cities = []
try:
with open(cities_filename, 'r', encoding='utf-8') as fp_cities:
cities = fp_cities.read().split('\n')
except OSError:
print('EX: unable to load cities file ' + cities_filename)
if not cities:
continue
for hashtag in cities:
hashtag = hashtag.lower().strip()
hashtag = hashtag.replace(' & ', ' and ')
hashtag2 = hashtag.replace('-', '').replace(' ', '')
city_filename = base_dir + '/tags/' + hashtag2 + '.category'
if not os.path.isfile(city_filename):
try:
with open(city_filename, 'w+',
encoding='utf-8') as fp_city:
fp_city.write(category_str)
except OSError:
print('EX: unable to write city category ' +
city_filename)
if '-' in hashtag:
section = hashtag.split('-')
new_hashtag = ''
for text in section:
new_hashtag += text.lower().title()
hashtag2 = new_hashtag
city_filename = \
base_dir + '/tags/' + hashtag2 + '.category'
if not os.path.isfile(city_filename):
try:
with open(city_filename, 'w+',
encoding='utf-8') as fp_city:
fp_city.write(category_str)
except OSError:
print('EX: unable to write city category2 ' +
city_filename)
if ' ' in hashtag:
section = hashtag.split(' ')
new_hashtag = ''
for text in section:
new_hashtag += text.lower().title()
hashtag2 = new_hashtag
city_filename = \
base_dir + '/tags/' + hashtag2 + '.category'
if not os.path.isfile(city_filename):
try:
with open(city_filename, 'w+',
encoding='utf-8') as fp_city:
fp_city.write(category_str)
except OSError:
print('EX: unable to write city category3 ' +
city_filename)
def get_hashtag_categories(base_dir: str,
recent: bool = False,
category: str = None) -> None:

View File

@ -287,6 +287,7 @@ from shares import expire_shares
from shares import shares_catalog_endpoint
from shares import shares_catalog_account_endpoint
from shares import shares_catalog_csv_endpoint
from categories import load_city_hashtags
from categories import set_hashtag_category
from categories import update_hashtag_categories
from languages import get_reply_language
@ -24136,6 +24137,9 @@ def run_daemon(max_shares_on_profile: int,
print('ERROR: no translations were loaded')
sys.exit()
# create hashtag categories for cities
load_city_hashtags(base_dir, httpd.translate)
# spoofed city for gps location misdirection
httpd.city = city