-
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unicode encording problem. #19
Comments
Hi @hansu35 Thanks for raising. Were you using this notion search workflow successfully before the recent changes - or are you new to the workflow? Thanks |
If you update the code you are using to this:
Does it work? In this code, alfredquery is converted to unicode before being normalised in NFC. Thanks |
I have tried your code and faced another error like this
I am pretty sure that it is about encoding.
# add last part of function called "buildnotionsearchquerydata"
file = open('./log.txt','a')
file.write(jsonData)
file.write("\n")
file.close() -> this show me that word has been seperated when it through alfred. ( as I said I type '한글', it printed 'ㅎㅏㄴㄱㅡㄹ')
notionSpaceId = "my id"
cookie = "my cookie"
useDesktopClient = True
isNavigableOnly = True
enableIcons = True -> run in terminal like this |
Hmmm I think this is going to need someone who understands about Unicode encoding, I'm going to label this issue appropriately. |
I have the same problem as a Korean user. |
I don't know why.. but I got a solution. the main idea is using python3 instead of python2. So I just change code little bit. replaced all add import line add or change two line in the code. # Get query from Alfred
alfredQuery = str(sys.argv[1])
# add unicode type to NFC
alfredQuery = unicodedata.normalize('NFC',alfredQuery)
# Call Notion
headers = {"Content-type": "application/json",
"Cookie": cookie}
conn = http.client.HTTPSConnection("www.notion.so")
conn.request("POST", "/api/v3/search",
buildnotionsearchquerydata(), headers)
response = conn.getresponse()
# convert to str
data = str(response.read(),'utf-8')
data = data.replace("<gzkNfoUU>", "")
data = data.replace("</gzkNfoUU>", "") then as last alfred command replace to Great.. Now you can get the result!! |
@hansu35 Could you share the new Workflow File you changed on here? It would be so helpful. Thanks in advance. |
first of all, you have python3 on your machine. # coding=utf-8
import http.client
import json
import os
import os.path
import struct
import sys
import urllib
import unicodedata
from payload import Payload
from searchresult import SearchResult
# config
notionSpaceId = os.environ['notionSpaceId']
cookie = os.environ['cookie']
# get useDesktopClient env variable and convert to boolean for use later, default to false
useDesktopClient = os.environ['useDesktopClient']
if (useDesktopClient == 'true') | (useDesktopClient == 'True') | (useDesktopClient == 'TRUE'):
useDesktopClient = True
else:
useDesktopClient = False
# get isNavigableOnly env variable and convert to boolean for use later, default to true
isNavigableOnly = os.environ['isNavigableOnly']
if (isNavigableOnly == 'false') | (isNavigableOnly == 'False') | (isNavigableOnly == 'FALSE'):
isNavigableOnly = False
else:
isNavigableOnly = True
# get enableIcons env variable and convert to boolean for use later, default to true
enableIcons = os.environ['enableIcons']
if (enableIcons == 'false') | (enableIcons == 'False') | (enableIcons == 'FALSE'):
enableIcons = False
else:
enableIcons = True
def buildnotionsearchquerydata():
query = {}
query["type"] = "BlocksInSpace"
query["query"] = alfredQuery
query["spaceId"] = notionSpaceId
query["limit"] = 9
filters = {}
filters["isDeletedOnly"] = False
filters["excludeTemplates"] = False
filters["isNavigableOnly"] = isNavigableOnly
filters["requireEditPermissions"] = False
ancestors = []
filters["ancestors"] = ancestors
createdby = []
filters["createdBy"] = createdby
editedby = []
filters["editedBy"] = editedby
lasteditedtime = []
filters["lastEditedTime"] = lasteditedtime
createdtime = []
filters["createdTime"] = createdtime
query["filters"] = filters
query["sort"] = "Relevance"
query["source"] = "quick_find"
jsonData = json.dumps(query)
return jsonData
def getnotionurl():
if useDesktopClient:
return "notion://www.notion.so/"
else:
return "https://www.notion.so/"
def decodeemoji(emoji):
if emoji:
b = emoji.encode('utf_32_le')
count = len(b) // 4
# If count is over 10, we don't have an emoji
if count > 10:
return None
cp = struct.unpack('<%dI' % count, b)
hexlist = []
for x in cp:
hexlist.append(hex(x)[2:])
return hexlist
return None
def downloadandgetfilepath(searchresultobjectid, imageurl):
# create icons dir if it doesn't already exist
if not os.path.isdir('./icons'):
path = "./icons"
access_rights = 0o755
os.mkdir(path, access_rights)
downloadurl = "/image/" \
+ urllib.quote(imageurl.encode('utf8'), safe='') \
+ "?width=120&cache=v2"
filetype = downloadurl[downloadurl.rfind('.'):]
filetype = filetype[:filetype.rfind('?')]
if '%3F' in filetype:
filetype = filetype[:filetype.rfind('%3F')]
filepath = "icons/" + searchresultobjectid + filetype
headers = {"Cookie": cookie}
conn = http.client.HTTPSConnection("www.notion.so")
conn.request("GET", downloadurl, "", headers)
response = conn.getresponse()
data = response.read()
with open(filepath, 'wb') as f:
f.write(data)
return filepath
def geticonpath(searchresultobjectid, notionicon):
iconpath = None
# is icon an emoji? If so, get hex values and construct the matching image file path in emojiicons/
hexlist = decodeemoji(notionicon)
if hexlist:
emojicodepoints = ""
count = 0
for x in hexlist:
count += 1
if count > 1:
emojicodepoints += "_"
emojicodepoints += x
iconpath = "emojiicons/" + emojicodepoints + ".png"
# check if emoji image exists - if not, remove last unicode codepoint and try again
if not os.path.isfile(iconpath):
while emojicodepoints.count("_") > 0:
emojicodepoints = emojicodepoints.rsplit('_', 1)[0]
iconpath = "emojiicons/" + emojicodepoints + ".png"
if os.path.isfile(iconpath):
break
else:
# is icon a web url? If so, download it to icons/
if "http" in notionicon:
iconpath = downloadandgetfilepath(searchresultobjectid, notionicon)
return iconpath
# Get query from Alfred
alfredQuery = str(sys.argv[1])
alfredQuery = unicodedata.normalize('NFC',alfredQuery)
# Call Notion
headers = {"Content-type": "application/json",
"Cookie": cookie}
conn = http.client.HTTPSConnection("www.notion.so")
conn.request("POST", "/api/v3/search",
buildnotionsearchquerydata(), headers)
response = conn.getresponse()
data = str(response.read(),'utf-8')
data = data.replace("<gzkNfoUU>", "")
data = data.replace("</gzkNfoUU>", "")
conn.close()
# Extract search results from notion response
searchResultList = []
searchResults = Payload(data)
for x in searchResults.results:
searchResultObject = SearchResult(x.get('id'))
if "properties" in searchResults.recordMap.get('block').get(searchResultObject.id).get('value'):
searchResultObject.title = \
searchResults.recordMap.get('block').get(searchResultObject.id).get('value').get('properties').get('title')[
0][0]
else:
searchResultObject.title = x.get('highlight').get('text')
if "pathText" in x.get('highlight'):
searchResultObject.subtitle = x.get('highlight').get('pathText')
else:
searchResultObject.subtitle = " "
if "format" in searchResults.recordMap.get('block').get(searchResultObject.id).get('value'):
if "page_icon" in searchResults.recordMap.get('block').get(searchResultObject.id).get('value').get('format'):
if enableIcons:
searchResultObject.icon = geticonpath(searchResultObject.id,
searchResults.recordMap.get('block').get(searchResultObject.id)
.get('value').get('format').get('page_icon'))
else:
searchResultObject.icon = None
searchResultObject.title = searchResults.recordMap.get('block').get(searchResultObject.id).get(
'value').get('format').get('page_icon') + " " + searchResultObject.title
searchResultObject.link = getnotionurl() + searchResultObject.id.replace("-", "")
searchResultList.append(searchResultObject)
itemList = []
for searchResultObject in searchResultList:
item = {}
item["uid"] = searchResultObject.id
item["type"] = "default"
item["title"] = searchResultObject.title
item["arg"] = searchResultObject.link
item["subtitle"] = searchResultObject.subtitle
if searchResultObject.icon:
icon = {}
icon["path"] = searchResultObject.icon
item["icon"] = icon
item["autocomplete"] = searchResultObject.title
itemList.append(item)
items = {}
if not itemList:
item = {}
item["uid"] = 1
item["type"] = "default"
item["title"] = "No results - go to Notion homepage"
item["arg"] = getnotionurl()
itemList.append(item)
items["items"] = itemList
items_json = json.dumps(items)
sys.stdout.write(items_json) |
@hansu35 ns 검색어 를 평소처럼 해봤는데 검색이 되진 않고
맨 마지막의 ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: 에러가 핵심인 거 같은데 Eng)
May I ask a little tips to solve my problems? full error messages : |
@hansu35
|
안녕하세요. 아마 인증서 관련 문제 인것으로 보이는데 아래와 같은 링크를 찾았습니다. it looks like problem about certification.
|
Hi.
|
@hansu35 It works normally. Thank you! It's so useful.
|
Please check out this new version (https://github.com/wrjlewis/notion-search-alfred-workflow/releases/tag/0.3.3). I believe this is now fixed and handling Unicode characters properly, so you don't need to install python 3 or make any changes yourself. Can you let me know if it is working? |
Seems like the issue is solved. Thank you 👍 |
Awesome |
works great.. :) |
hi.
I'm a Koren user.
use Korean it dose not working
it always returns "no result"
(I have checked it works with English)
So I did my homework then I think I found out the problem.
here is the key.
unicode on Mac use "NFD"
So I type on Alfred "한글" but the query string is "ㅎㅏㄴㄱㅡㄹ"
I think that makes "no result".
Could you check this?
I have tried code like this.
but this error come out
TypeError: normalize() argument 2 must be unicode, not str
The text was updated successfully, but these errors were encountered: