LibreOffice plugin to pipe whole Writer documents through Google Translate, that ought to keep most of the page formatting.

⌈⌋ ⎇ branch:  PageTranslate


Artifact [ae2a8372fe]

Artifact ae2a8372febfa7cb9340633ba1344d2a4980d211:

  • File pythonpath/httprequests.py — part of check-in [9287fd7b5e] at 2020-05-24 08:51:50 on branch trunk — Extract via_* classes into pythonpath/translationbackends. As well as requests/http/urllib-fallback code into separate module. Apply new nodepath for ConfigurationUpdateAccess (but keep using .json config file for now). (user: mario size: 1510)

# encoding: utf-8
# api: python
# type: classes
# category: http
# title: request/fallback
# description: loads requests, or similuates API via urllib
# version: 0.5
# state: beta
# depends: python:requests (>= 2.5)
# config: -
#
# Wraps requests or fakes a http.get() implementation.
#


__all__ = ["http", "urllib", "urlencode", "quote", "quote_plus"]

# http preparations
import urllib
from urllib.parse import urlencode, quote, quote_plus

try:
    import requests
    http = requests.Session()
    
except Exception as e:

    log.error("Missing library: `pip install requests` (either system-wide, or in your libreoffice program/ folder)")
    from urllib.request import urlopen, Request

    class fake_requests:
        content = ""
        ssl_args = {}
        headers = {}

        def __init__(self):
            if sys.platform != 'win32':
                return
            import ssl
            myssl = ssl.create_default_context();
            myssl.check_hostname = False
            myssl.verify_mode = ssl.CERT_NONE
            self.ssl_args["context"] = myssl
            
        def get(self, url):
            self.content = urlopen(
                Request(url, headers=self.headers), **self.ssl_args
            ).read()
            return self
    
    http = fake_requests()

# headers
http.headers.update({
    "User-Agent": "Mozilla/5.0 (X11; Linux; LibreOffice/6.3), TradutorLibreText/1.3+PageTranslate/1.2",
    "Accept-Language": "*; q=1.0",
    "Accept-Encoding": "utf-8"
})