Jump to content

Need help with Python Script Filter and Umlaute (Ä,Ö,Ü)


Recommended Posts

Hey there,

 

I´ve got a running workflow that searches through all my documents on my Paperless-ngx Server, via a python script with requests and the paperless API. Most of the workflow is just shameless copied from this great tutorial here, and it is working without problems. Just one thing I can´t search for documents that have Umlaute like Ü,Ä etc. which makes this workflow almost useless for my german pdf documents :) Does anybody know how to fix this? I tried things like sys.argv[1].encode() or # encoding: utf-8 from __future__ import unicode_literals at the top, but without success. In the API documentation it says the search query must be like /api/documents/?query=your%20search%20query, so I guess I need to encode the sys.argv[1] somewhere in the script? Thanks in advance, here is my script so far:
 

import sys
import requests
import json


# Retrieve user input from Alfred
search_query = sys.argv[1]

api_key = 'APIKEY'

# URL für die Abfrage der Aufgaben
url = "https://paperless.XYZ.synology.me/api/documents/?query=" + search_query

# HTTP-Header mit API-Schlüssel
headers = {
    'Authorization': f'Basic {api_key}',
    'Content-Type': 'application/x-www-form-urlencoded'
}

# GET-Anfrage an API stellen
response = requests.get(url, headers=headers).json()


def get_formatted_results(search_results):
    formatted_results = []
    for item in search_results["results"]:
        result = {
            "title": item["title"],
            "subtitle": item["id"],
            "arg": item["id"],
            "autocomplete": item["id"],
            "icon": {
                "path": "./icon.png"
            }
        }
        formatted_results.append(result)

    return formatted_results

def get_alfred_items(search_results):
    if len(search_results["results"]) == 0:
        result = {
            "title": "Keine Dokumente gefunden.",
            "subtitle": "Versuche eine neue Suche",
        }
        return [result]
    else:
        return get_formatted_results(search_results)


if __name__ == "__main__":
   # Make API call to fetch the npm search results and assign it to a variable
    npm_search_results = requests.get(url, headers=headers).json()

    # A Script Filter is required to return an items array of zero or more items.
    # Each item describes a result row displayed in Alfred.
    alfred_json = json.dumps({
        "items": get_alfred_items(npm_search_results)
    }, indent=2)

    # Pass the formatted JSON data back to Alfred
    sys.stdout.write(alfred_json)
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...