Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.parse
- import quotes
- import os
- import json
- import requests
- import sys
- from memoshuf import memoshuf
- def randomquote(search: str = None, text_mogrify=False):
- quote_ids = quotes.search_quotes(search)
- fetch_id = memoshuf('randomquote', str(search), quote_ids)
- quote = quotes.fetch_quote(fetch_id)
- return quote
- #return quotes.render_quote(quote, text_mogrify)
- def translate(data):
- langs = ["en", "cy", "zh-CN", "es", "en"]
- data = data.replace("|", ":")
- for i in range(1, len(langs)):
- sourceLang = langs[i-1]
- targetLang = langs[i]
- url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" \
- + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + urllib.parse.quote(data)
- resp = requests.get(url)
- print(resp.json(), file=sys.stderr)
- for r in resp.json()[0]:
- print(r, file=sys.stderr)
- data = resp.json()[0][0][0]
- return data
- s = os.environ['T9_INPUT']
- if s.isdigit():
- data = quotes.fetch_quote(s)
- else:
- data = randomquote(s)
- data["quote"] = translate(data["quote"])
- print("Translated " + quotes.render_quote(data, False))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement