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

⌈⌋ ⎇ branch:  PageTranslate


Artifact [59d4168bac]

Artifact 59d4168bac744ad8ed93fdb4b1cf8290fca07862:

  • File pythonpath/httprequests.py — part of check-in [3c6f3e31f5] at 2020-05-29 15:05:17 on branch trunk — Strip log=None, because importing the module inherits the settings from __main__ anyway. (user: mario size: 1782)

# 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):
            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.4",
    "Accept-Language": "*; q=1.0",
    "Accept-Encoding": "utf-8"
})