Text Expander: use python script to fetch info from the internet
Posted: 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:
Thanks!
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!