1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
4
###############################################################################
5
# OpenLP - Open Source Lyrics Projection #
6
# --------------------------------------------------------------------------- #
7
# Copyright (c) 2008-2016 OpenLP Developers #
8
# --------------------------------------------------------------------------- #
9
# This program is free software; you can redistribute it and/or modify it #
10
# under the terms of the GNU General Public License as published by the Free #
11
# Software Foundation; version 2 of the License. #
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
18
# You should have received a copy of the GNU General Public License along #
19
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
21
###############################################################################
23
The :mod:`openlp.core.utils` module provides the utility libraries for OpenLP.
32
from http.client import HTTPException
33
from random import randint
35
from openlp.core.common import Registry
37
log = logging.getLogger(__name__ + '.__init__')
41
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
42
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
43
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36'
46
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) '
47
'Chrome/26.0.1410.43 Safari/537.31',
48
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) '
49
'Chrome/20.0.1132.57 Safari/536.11',
50
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) '
51
'Chrome/20.0.1132.47 Safari/536.11',
54
'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 '
55
'Chrome/25.0.1364.160 Safari/537.22',
56
'Mozilla/5.0 (X11; CrOS armv7l 2913.260.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.99 '
58
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.27 (KHTML, like Gecko) Chrome/26.0.1389.0 Safari/537.27'
61
'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0'
64
CONNECTION_TIMEOUT = 30
65
CONNECTION_RETRIES = 2
68
class HTTPRedirectHandlerFixed(urllib.request.HTTPRedirectHandler):
70
Special HTTPRedirectHandler used to work around http://bugs.python.org/issue22248
71
(Redirecting to urls with special chars)
73
def redirect_request(self, req, fp, code, msg, headers, new_url):
76
Test if the new_url can be decoded to ascii
87
new_url.encode('latin1').decode('ascii')
90
# The url could not be decoded to ascii, so we do some url encoding
91
fixed_url = urllib.parse.quote(new_url.encode('latin1').decode('utf-8', 'replace'), safe='/:')
92
return super(HTTPRedirectHandlerFixed, self).redirect_request(req, fp, code, msg, headers, fixed_url)
95
def _get_user_agent():
97
Return a user agent customised for the platform the user is on.
99
browser_list = USER_AGENTS.get(sys.platform, None)
101
browser_list = USER_AGENTS['default']
102
random_index = randint(0, len(browser_list) - 1)
103
return browser_list[random_index]
106
def get_web_page(url, header=None, update_openlp=False):
108
Attempts to download the webpage at url and returns that page or None.
110
:param url: The URL to be downloaded.
111
:param header: An optional HTTP header to pass in the request to the web server.
112
:param update_openlp: Tells OpenLP to update itself if the page is successfully downloaded.
115
# TODO: Add proxy usage. Get proxy info from OpenLP settings, add to a
116
# proxy_handler, build into an opener and install the opener into urllib2.
117
# http://docs.python.org/library/urllib2.html
120
# This is needed to work around http://bugs.python.org/issue22248 and https://bugs.launchpad.net/openlp/+bug/1251437
121
opener = urllib.request.build_opener(HTTPRedirectHandlerFixed())
122
urllib.request.install_opener(opener)
123
req = urllib.request.Request(url)
124
if not header or header[0].lower() != 'user-agent':
125
user_agent = _get_user_agent()
126
req.add_header('User-Agent', user_agent)
128
req.add_header(header[0], header[1])
129
log.debug('Downloading URL = %s' % url)
131
while retries <= CONNECTION_RETRIES:
135
page = urllib.request.urlopen(req, timeout=CONNECTION_TIMEOUT)
136
log.debug('Downloaded page {}'.format(page.geturl()))
138
except urllib.error.URLError as err:
139
log.exception('URLError on {}'.format(url))
140
log.exception('URLError: {}'.format(err.reason))
142
if retries > CONNECTION_RETRIES:
144
except socket.timeout:
145
log.exception('Socket timeout: {}'.format(url))
147
if retries > CONNECTION_RETRIES:
149
except socket.gaierror:
150
log.exception('Socket gaierror: {}'.format(url))
152
if retries > CONNECTION_RETRIES:
154
except ConnectionRefusedError:
155
log.exception('ConnectionRefused: {}'.format(url))
157
if retries > CONNECTION_RETRIES:
160
except ConnectionError:
161
log.exception('Connection error: {}'.format(url))
163
if retries > CONNECTION_RETRIES:
165
except HTTPException:
166
log.exception('HTTPException error: {}'.format(url))
168
if retries > CONNECTION_RETRIES:
171
# Don't know what's happening, so reraise the original
174
Registry().get('application').process_events()
176
log.exception('{} could not be downloaded'.format(url))
182
__all__ = ['get_application_version', 'check_latest_version',