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

⌈⌋ branch:  PageTranslate


Artifact [a4870cf51f]

Artifact a4870cf51fa7ad12d295537a0c49c15fc13b2512:

  • File pythonpath/httprequests.py — part of check-in [7b8ef174e0] at 2021-06-02 16:44:24 on branch trunk — Fix GogoleAjax to rejoin split up ssentences (from nested list). (user: mario size: 1833)

# 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 logging as log
import sys
import urllib
try:
    from urllib.parse import urlencode, quote, quote_plus
    from urllib.request import urlopen, Request
except:
    from urllib import urlencode, quote, quote_plus
    from urllib2 import urlopen, Request
#else:
#    from six.moves.urllib import urlencode, quote, quote_plus
#    from six.moves.urllib.request import urlopen, Request

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)")

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

        def __init__(self):
            import ssl
            if not hasattr(ssl, "create_default_context"):
                return
            context = ssl.create_default_context();
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE
            self.ssl_args["context"] = context
            
        def get(self, url):
            self.content = urlopen(
                Request(url, headers=self.headers), **self.ssl_args
            ).read()
            return self
    
    http = fake_requests()
    log.info("using fake_requests() for now")

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