mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
9793b725e1
commit
43379ae568
|
@ -21,175 +21,175 @@ from webapp_utils import html_footer
|
||||||
|
|
||||||
|
|
||||||
def get_hashtag_categories_feed(base_dir: str,
|
def get_hashtag_categories_feed(base_dir: str,
|
||||||
hashtagCategories: {} = None) -> str:
|
hashtag_categories: {} = None) -> str:
|
||||||
"""Returns an rss feed for hashtag categories
|
"""Returns an rss feed for hashtag categories
|
||||||
"""
|
"""
|
||||||
if not hashtagCategories:
|
if not hashtag_categories:
|
||||||
hashtagCategories = get_hashtag_categories(base_dir)
|
hashtag_categories = get_hashtag_categories(base_dir)
|
||||||
if not hashtagCategories:
|
if not hashtag_categories:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
rssStr = \
|
rss_str = \
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + \
|
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + \
|
||||||
"<rss version=\"2.0\">\n" + \
|
"<rss version=\"2.0\">\n" + \
|
||||||
'<channel>\n' + \
|
'<channel>\n' + \
|
||||||
' <title>#categories</title>\n'
|
' <title>#categories</title>\n'
|
||||||
|
|
||||||
rssDateStr = \
|
rss_date_str = \
|
||||||
datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UT")
|
datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UT")
|
||||||
|
|
||||||
for categoryStr, hashtagList in hashtagCategories.items():
|
for category_str, hashtag_list in hashtag_categories.items():
|
||||||
rssStr += \
|
rss_str += \
|
||||||
'<item>\n' + \
|
'<item>\n' + \
|
||||||
' <title>' + categoryStr + '</title>\n'
|
' <title>' + category_str + '</title>\n'
|
||||||
listStr = ''
|
list_str = ''
|
||||||
for hashtag in hashtagList:
|
for hashtag in hashtag_list:
|
||||||
if ':' in hashtag:
|
if ':' in hashtag:
|
||||||
continue
|
continue
|
||||||
if '&' in hashtag:
|
if '&' in hashtag:
|
||||||
continue
|
continue
|
||||||
listStr += hashtag + ' '
|
list_str += hashtag + ' '
|
||||||
rssStr += \
|
rss_str += \
|
||||||
' <description>' + listStr.strip() + '</description>\n' + \
|
' <description>' + list_str.strip() + '</description>\n' + \
|
||||||
' <link/>\n' + \
|
' <link/>\n' + \
|
||||||
' <pubDate>' + rssDateStr + '</pubDate>\n' + \
|
' <pubDate>' + rss_date_str + '</pubDate>\n' + \
|
||||||
'</item>\n'
|
'</item>\n'
|
||||||
|
|
||||||
rssStr += \
|
rss_str += \
|
||||||
'</channel>\n' + \
|
'</channel>\n' + \
|
||||||
'</rss>\n'
|
'</rss>\n'
|
||||||
return rssStr
|
return rss_str
|
||||||
|
|
||||||
|
|
||||||
def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
|
def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
|
||||||
"""Returns a tag swarm of today's hashtags
|
"""Returns a tag swarm of today's hashtags
|
||||||
"""
|
"""
|
||||||
maxTagLength = 42
|
max_tag_length = 42
|
||||||
curr_time = datetime.utcnow()
|
curr_time = datetime.utcnow()
|
||||||
daysSinceEpoch = (curr_time - datetime(1970, 1, 1)).days
|
days_since_epoch = (curr_time - datetime(1970, 1, 1)).days
|
||||||
daysSinceEpochStr = str(daysSinceEpoch) + ' '
|
days_since_epoch_str = str(days_since_epoch) + ' '
|
||||||
daysSinceEpochStr2 = str(daysSinceEpoch - 1) + ' '
|
days_since_epoch_str2 = str(days_since_epoch - 1) + ' '
|
||||||
recently = daysSinceEpoch - 1
|
recently = days_since_epoch - 1
|
||||||
tagSwarm = []
|
tag_swarm = []
|
||||||
categorySwarm = []
|
category_swarm = []
|
||||||
domainHistogram = {}
|
domain_histogram = {}
|
||||||
|
|
||||||
# Load the blocked hashtags into memory.
|
# Load the blocked hashtags into memory.
|
||||||
# This avoids needing to repeatedly load the blocked file for each hashtag
|
# This avoids needing to repeatedly load the blocked file for each hashtag
|
||||||
blockedStr = ''
|
blocked_str = ''
|
||||||
globalBlockingFilename = base_dir + '/accounts/blocking.txt'
|
global_blocking_filename = base_dir + '/accounts/blocking.txt'
|
||||||
if os.path.isfile(globalBlockingFilename):
|
if os.path.isfile(global_blocking_filename):
|
||||||
with open(globalBlockingFilename, 'r') as fp:
|
with open(global_blocking_filename, 'r') as fp_block:
|
||||||
blockedStr = fp.read()
|
blocked_str = fp_block.read()
|
||||||
|
|
||||||
for subdir, dirs, files in os.walk(base_dir + '/tags'):
|
for _, _, files in os.walk(base_dir + '/tags'):
|
||||||
for f in files:
|
for fname in files:
|
||||||
if not f.endswith('.txt'):
|
if not fname.endswith('.txt'):
|
||||||
continue
|
continue
|
||||||
tagsFilename = os.path.join(base_dir + '/tags', f)
|
tags_filename = os.path.join(base_dir + '/tags', fname)
|
||||||
if not os.path.isfile(tagsFilename):
|
if not os.path.isfile(tags_filename):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# get last modified datetime
|
# get last modified datetime
|
||||||
modTimesinceEpoc = os.path.getmtime(tagsFilename)
|
mod_time_since_epoc = os.path.getmtime(tags_filename)
|
||||||
lastModifiedDate = datetime.fromtimestamp(modTimesinceEpoc)
|
last_modified_date = datetime.fromtimestamp(mod_time_since_epoc)
|
||||||
fileDaysSinceEpoch = (lastModifiedDate - datetime(1970, 1, 1)).days
|
file_days_since_epoch = \
|
||||||
|
(last_modified_date - datetime(1970, 1, 1)).days
|
||||||
|
|
||||||
# check if the file was last modified within the previous
|
# check if the file was last modified within the previous
|
||||||
# two days
|
# two days
|
||||||
if fileDaysSinceEpoch < recently:
|
if file_days_since_epoch < recently:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
hashTagName = f.split('.')[0]
|
hash_tag_name = fname.split('.')[0]
|
||||||
if len(hashTagName) > maxTagLength:
|
if len(hash_tag_name) > max_tag_length:
|
||||||
# NoIncrediblyLongAndBoringHashtagsShownHere
|
# NoIncrediblyLongAndBoringHashtagsShownHere
|
||||||
continue
|
continue
|
||||||
if '#' in hashTagName or \
|
if '#' in hash_tag_name or \
|
||||||
'&' in hashTagName or \
|
'&' in hash_tag_name or \
|
||||||
'"' in hashTagName or \
|
'"' in hash_tag_name or \
|
||||||
"'" in hashTagName:
|
"'" in hash_tag_name:
|
||||||
continue
|
continue
|
||||||
if '#' + hashTagName + '\n' in blockedStr:
|
if '#' + hash_tag_name + '\n' in blocked_str:
|
||||||
continue
|
continue
|
||||||
with open(tagsFilename, 'r') as fp:
|
with open(tags_filename, 'r') as fp_tags:
|
||||||
# only read one line, which saves time and memory
|
# only read one line, which saves time and memory
|
||||||
lastTag = fp.readline()
|
last_tag = fp_tags.readline()
|
||||||
if not lastTag.startswith(daysSinceEpochStr):
|
if not last_tag.startswith(days_since_epoch_str):
|
||||||
if not lastTag.startswith(daysSinceEpochStr2):
|
if not last_tag.startswith(days_since_epoch_str2):
|
||||||
continue
|
continue
|
||||||
with open(tagsFilename, 'r') as tagsFile:
|
with open(tags_filename, 'r') as fp_tags:
|
||||||
while True:
|
while True:
|
||||||
line = tagsFile.readline()
|
line = fp_tags.readline()
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
elif ' ' not in line:
|
if ' ' not in line:
|
||||||
break
|
break
|
||||||
sections = line.split(' ')
|
sections = line.split(' ')
|
||||||
if len(sections) != 3:
|
if len(sections) != 3:
|
||||||
break
|
break
|
||||||
postDaysSinceEpochStr = sections[0]
|
post_days_since_epoch_str = sections[0]
|
||||||
if not postDaysSinceEpochStr.isdigit():
|
if not post_days_since_epoch_str.isdigit():
|
||||||
break
|
break
|
||||||
postDaysSinceEpoch = int(postDaysSinceEpochStr)
|
post_days_since_epoch = int(post_days_since_epoch_str)
|
||||||
if postDaysSinceEpoch < recently:
|
if post_days_since_epoch < recently:
|
||||||
break
|
break
|
||||||
else:
|
post_url = sections[2]
|
||||||
postUrl = sections[2]
|
if '##' not in post_url:
|
||||||
if '##' not in postUrl:
|
break
|
||||||
break
|
post_domain = post_url.split('##')[1]
|
||||||
postDomain = postUrl.split('##')[1]
|
if '#' in post_domain:
|
||||||
if '#' in postDomain:
|
post_domain = post_domain.split('#')[0]
|
||||||
postDomain = postDomain.split('#')[0]
|
|
||||||
|
|
||||||
if domainHistogram.get(postDomain):
|
if domain_histogram.get(post_domain):
|
||||||
domainHistogram[postDomain] = \
|
domain_histogram[post_domain] = \
|
||||||
domainHistogram[postDomain] + 1
|
domain_histogram[post_domain] + 1
|
||||||
else:
|
else:
|
||||||
domainHistogram[postDomain] = 1
|
domain_histogram[post_domain] = 1
|
||||||
tagSwarm.append(hashTagName)
|
tag_swarm.append(hash_tag_name)
|
||||||
categoryFilename = \
|
category_filename = \
|
||||||
tagsFilename.replace('.txt', '.category')
|
tags_filename.replace('.txt', '.category')
|
||||||
if os.path.isfile(categoryFilename):
|
if os.path.isfile(category_filename):
|
||||||
categoryStr = \
|
category_str = \
|
||||||
get_hashtag_category(base_dir, hashTagName)
|
get_hashtag_category(base_dir, hash_tag_name)
|
||||||
if len(categoryStr) < maxTagLength:
|
if len(category_str) < max_tag_length:
|
||||||
if '#' not in categoryStr and \
|
if '#' not in category_str and \
|
||||||
'&' not in categoryStr and \
|
'&' not in category_str and \
|
||||||
'"' not in categoryStr and \
|
'"' not in category_str and \
|
||||||
"'" not in categoryStr:
|
"'" not in category_str:
|
||||||
if categoryStr not in categorySwarm:
|
if category_str not in category_swarm:
|
||||||
categorySwarm.append(categoryStr)
|
category_swarm.append(category_str)
|
||||||
break
|
break
|
||||||
break
|
break
|
||||||
|
|
||||||
if not tagSwarm:
|
if not tag_swarm:
|
||||||
return ''
|
return ''
|
||||||
tagSwarm.sort()
|
tag_swarm.sort()
|
||||||
|
|
||||||
# swarm of categories
|
# swarm of categories
|
||||||
categorySwarmStr = ''
|
category_swarm_str = ''
|
||||||
if categorySwarm:
|
if category_swarm:
|
||||||
if len(categorySwarm) > 3:
|
if len(category_swarm) > 3:
|
||||||
categorySwarm.sort()
|
category_swarm.sort()
|
||||||
for categoryStr in categorySwarm:
|
for category_str in category_swarm:
|
||||||
categorySwarmStr += \
|
category_swarm_str += \
|
||||||
'<a href="' + actor + '/category/' + categoryStr + \
|
'<a href="' + actor + '/category/' + category_str + \
|
||||||
'" class="hashtagswarm"><b>' + categoryStr + '</b></a>\n'
|
'" class="hashtagswarm"><b>' + category_str + '</b></a>\n'
|
||||||
categorySwarmStr += '<br>\n'
|
category_swarm_str += '<br>\n'
|
||||||
|
|
||||||
# swarm of tags
|
# swarm of tags
|
||||||
tagSwarmStr = ''
|
tag_swarm_str = ''
|
||||||
for tagName in tagSwarm:
|
for tag_name in tag_swarm:
|
||||||
tagSwarmStr += \
|
tag_swarm_str += \
|
||||||
'<a href="' + actor + '/tags/' + tagName + \
|
'<a href="' + actor + '/tags/' + tag_name + \
|
||||||
'" class="hashtagswarm">' + tagName + '</a>\n'
|
'" class="hashtagswarm">' + tag_name + '</a>\n'
|
||||||
|
|
||||||
if categorySwarmStr:
|
if category_swarm_str:
|
||||||
tagSwarmStr = \
|
tag_swarm_str = \
|
||||||
get_content_warning_button('alltags', translate, tagSwarmStr)
|
get_content_warning_button('alltags', translate, tag_swarm_str)
|
||||||
|
|
||||||
tagSwarmHtml = categorySwarmStr + tagSwarmStr.strip() + '\n'
|
tag_swarm_html = category_swarm_str + tag_swarm_str.strip() + '\n'
|
||||||
return tagSwarmHtml
|
return tag_swarm_html
|
||||||
|
|
||||||
|
|
||||||
def html_search_hashtag_category(css_cache: {}, translate: {},
|
def html_search_hashtag_category(css_cache: {}, translate: {},
|
||||||
|
@ -198,8 +198,8 @@ def html_search_hashtag_category(css_cache: {}, translate: {},
|
||||||
"""Show hashtags after selecting a category on the main search screen
|
"""Show hashtags after selecting a category on the main search screen
|
||||||
"""
|
"""
|
||||||
actor = path.split('/category/')[0]
|
actor = path.split('/category/')[0]
|
||||||
categoryStr = path.split('/category/')[1].strip()
|
category_str = path.split('/category/')[1].strip()
|
||||||
searchNickname = get_nickname_from_actor(actor)
|
search_nickname = get_nickname_from_actor(actor)
|
||||||
|
|
||||||
set_custom_background(base_dir, 'search-background', 'follow-background')
|
set_custom_background(base_dir, 'search-background', 'follow-background')
|
||||||
|
|
||||||
|
@ -207,37 +207,37 @@ def html_search_hashtag_category(css_cache: {}, translate: {},
|
||||||
if os.path.isfile(base_dir + '/search.css'):
|
if os.path.isfile(base_dir + '/search.css'):
|
||||||
css_filename = base_dir + '/search.css'
|
css_filename = base_dir + '/search.css'
|
||||||
|
|
||||||
instanceTitle = \
|
instance_title = \
|
||||||
get_config_param(base_dir, 'instanceTitle')
|
get_config_param(base_dir, 'instanceTitle')
|
||||||
htmlStr = \
|
html_str = \
|
||||||
html_header_with_external_style(css_filename, instanceTitle, None)
|
html_header_with_external_style(css_filename, instance_title, None)
|
||||||
|
|
||||||
# show a banner above the search box
|
# show a banner above the search box
|
||||||
searchBannerFile, searchBannerFilename = \
|
search_banner_file, search_banner_filename = \
|
||||||
get_search_banner_file(base_dir, searchNickname, domain, theme)
|
get_search_banner_file(base_dir, search_nickname, domain, theme)
|
||||||
|
|
||||||
if os.path.isfile(searchBannerFilename):
|
if os.path.isfile(search_banner_filename):
|
||||||
htmlStr += '<a href="' + actor + '/search">\n'
|
html_str += '<a href="' + actor + '/search">\n'
|
||||||
htmlStr += '<img loading="lazy" class="timeline-banner" src="' + \
|
html_str += '<img loading="lazy" class="timeline-banner" src="' + \
|
||||||
actor + '/' + searchBannerFile + '" alt="" /></a>\n'
|
actor + '/' + search_banner_file + '" alt="" /></a>\n'
|
||||||
|
|
||||||
htmlStr += \
|
html_str += \
|
||||||
'<div class="follow">' + \
|
'<div class="follow">' + \
|
||||||
'<center><br><br><br>' + \
|
'<center><br><br><br>' + \
|
||||||
'<h1><a href="' + actor + '/search"><b>' + \
|
'<h1><a href="' + actor + '/search"><b>' + \
|
||||||
translate['Category'] + ': ' + categoryStr + '</b></a></h1>'
|
translate['Category'] + ': ' + category_str + '</b></a></h1>'
|
||||||
|
|
||||||
hashtagsDict = get_hashtag_categories(base_dir, True, categoryStr)
|
hashtags_dict = get_hashtag_categories(base_dir, True, category_str)
|
||||||
if hashtagsDict:
|
if hashtags_dict:
|
||||||
for categoryStr2, hashtagList in hashtagsDict.items():
|
for _, hashtag_list in hashtags_dict.items():
|
||||||
hashtagList.sort()
|
hashtag_list.sort()
|
||||||
for tagName in hashtagList:
|
for tag_name in hashtag_list:
|
||||||
htmlStr += \
|
html_str += \
|
||||||
'<a href="' + actor + '/tags/' + tagName + \
|
'<a href="' + actor + '/tags/' + tag_name + \
|
||||||
'" class="hashtagswarm">' + tagName + '</a>\n'
|
'" class="hashtagswarm">' + tag_name + '</a>\n'
|
||||||
|
|
||||||
htmlStr += \
|
html_str += \
|
||||||
'</center>' + \
|
'</center>' + \
|
||||||
'</div>'
|
'</div>'
|
||||||
htmlStr += html_footer()
|
html_str += html_footer()
|
||||||
return htmlStr
|
return html_str
|
||||||
|
|
Loading…
Reference in New Issue