Text Expander: use python script to fetch info from the internet

Share your favorite FastKeys commands
Post Reply
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Jan 16th, ’22, 01:55

Hi - I am wondering if it is possible to call a Python script as a text expansion replacement to fetch information from the web.

As an example - I would like to type :pq and have a python script fetch a quote from the internet and replace the trigger with that quote:

Type :pq and get:
The things that people do now in sports, you can't even believe. These are complete total athletes. To see what human beings can do in the highest level is amazing. -*Billie Joe Armstrong*

Here is an example script:

Code: Select all

import requests
import pyautogui

## function that gets the random quote
def get_random_quote():
    try:
        ## making the get request
        response = requests.get(
            "https://quote-garden.herokuapp.com/api/v3/quotes/random"
        )
        if response.status_code == 200:
            ## extracting the core data
            json_data = response.json()
            data = json_data["data"]
            # the quote
            pyautogui.typewrite(data[0]["quoteText"])
            pyautogui.typewrite("  -")
            pyautogui.press("*")
            # the author
            pyautogui.typewrite(data[0]["quoteAuthor"])
            pyautogui.press("*")
            pyautogui.press(["enter"])
        else:
            print("Error while getting quote")
    except:
        print("Something went wrong! Try Again!")


get_random_quote()

Thanks!
User avatar
Marko
Posts: 1718
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Jan 17th, ’22, 10:29

Why not? Try using the Text Expander with the Type: Run to run the external script.
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Jan 18th, ’22, 05:48

Hi - I have tried three methods, both have failed.

1. I entered the python script into the text area for TextExpander > Run -- Result: a web page opened with the raw unprocessed json string that my script fetches. I had hoped to see the parsed string.
2. I pointed to a file. Result: the .py file was opened in my default editor
3. I tried a more obvious method: python file.py in the text area - and a command prompt opened, then close - so - closer!

Is there any way to replace the trigger with the output? Thanks for the help!
User avatar
Marko
Posts: 1718
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Jan 18th, ’22, 06:28

You cannot use the Python code in FastKeys directly. You need to compile the code to exe and then run in.
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Jan 18th, ’22, 23:25

Marko wrote: Jan 18th, ’22, 06:28 You cannot use the Python code in FastKeys directly. You need to compile the code to exe and then run in.
Doh - thanks - I missed that. This will work in cases where I don’t need to feed the code with an input.

Thanks!
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Aug 20th, ’22, 05:17

I've come back to Fastkeys after some time, and I'd like some help getting a Python script working.

I compiled the script to an .exe but executing with Fastkeys does nothing but briefly open and close a command prompt windows. If I run the .exe in a command window directly the text I am getting from the internet is displayed.

Is there a specific way I need to print the text so that Fastkeys will put the text into the text field I am targeting?

Code: Select all

import requests
import pyautogui


## function that gets the random quote
def get_random_quote():
    try:
        ## making the get request
        response = requests.get(
            "https://quote-garden.herokuapp.com/api/v3/quotes/random"
        )
        if response.status_code == 200:
            # ## extracting the core data
            json_data = response.json()
            data = json_data["data"]

            print(
                data[0]["quoteText"] + " —*" + data[0]["quoteAuthor"] + "*" + " #Quotes"
            )
        else:
            print("Error while getting quote")
    except:
        print("Something went wrong! Try Again!")


get_random_quote()
User avatar
Marko
Posts: 1718
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Aug 21st, ’22, 08:45

Use this method to keep the cmd window open:
Type: Command

Code: Select all

run %ComSpec% /k "C:\MyFile.exe"
Post Reply