From 3fbc80c68b5551fff814979bfd998a3c9e172144 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 19 Aug 2019 20:02:28 +0100 Subject: [PATCH] Emoji search --- daemon.py | 15 +++++++++++++++ webinterface.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/daemon.py b/daemon.py index e070ac6f..e1455215 100644 --- a/daemon.py +++ b/daemon.py @@ -87,6 +87,7 @@ from webinterface import htmlGetLoginCredentials from webinterface import htmlNewPost from webinterface import htmlFollowConfirm from webinterface import htmlSearch +from webinterface import htmlSearchEmoji from webinterface import htmlUnfollowConfirm from webinterface import htmlProfileAfterSearch from webinterface import htmlEditProfile @@ -2404,6 +2405,20 @@ class PubServer(BaseHTTPRequestHandler): self.wfile.write(msg) self.server.POSTbusy=False return + elif searchStr.startswith(':') or \ + searchStr.lower().strip('\n').endswith(' emoji'): + # eg. "cat emoji" + if searchStr.lower().strip('\n').endswith(' emoji'): + searchStr=searchStr.lower().strip('\n').replace(' emoji','') + # emoji search + emojiStr= \ + htmlSearchEmoji(self.server.baseDir,searchStr) + if emojiStr: + msg=sharedItemsStr.encode('utf-8') + self._login_headers('text/html',len(msg)) + self.wfile.write(msg) + self.server.POSTbusy=False + return else: # shared items search sharedItemsStr= \ diff --git a/webinterface.py b/webinterface.py index 4ab0fff0..62dbd39c 100644 --- a/webinterface.py +++ b/webinterface.py @@ -46,6 +46,46 @@ def getPersonAvatarUrl(personUrl: str,personCache: {}) -> str: return personJson['icon']['url'] return None +def htmlSearchEmoji(baseDir: str,searchStr: str) -> str: + """Search results for emoji + """ + + searchStr=searchStr.lower().replace(':','').strip('\n') + with open(baseDir+'/epicyon-profile.css', 'r') as cssFile: + emojiCSS=cssFile.read() + emojiLookupFilename=baseDir+'/emoji/emoji.json' + + # create header + emojiForm=htmlHeader(emojiCSS) + emojiForm+='

Emoji Search

' + + # does the lookup file exist? + if not os.path.isfile(emojiLookupFilename): + emojiForm+='
No results
' + emojiForm+=htmlFooter() + return emojiForm + + with open(emojiLookupFilename, 'r') as fp: + emojiJson=commentjson.load(fp) + results={} + if emojiJson.get(searchStr): + # exact match + results[searchStr] = emojiJson[searchStr]+'.png' + else: + for emojiName,filename in emojiJson.items(): + if searchStr in emojiName: + results[emojiName] = filename+'.png' + for emojiName,filename in emojiJson.items(): + if emojiName in searchStr: + results[emojiName] = filename+'.png' + emojiForm+='
' + for emojiName,filename in results.items(): + emojiForm+='
:'+emojiName+':
' + emojiForm+='
' + + emojiForm+=htmlFooter() + return emojiForm + def htmlSearchSharedItems(baseDir: str,searchStr: str,pageNumber: int,resultsPerPage: int,actor: str) -> str: """Search results for shared items """