From 730345b75edb6b02bce5f4ea0212dc64a52ae3df Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 13 Dec 2013 21:57:49 +0200 Subject: [PATCH 1/3] Fix bug #1211049: select a user agent from a list, depending on the platform. Fixes: https://launchpad.net/bugs/1211049 --- openlp/core/utils/__init__.py | 42 ++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index a33cd2604..422e9a48b 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -38,6 +38,7 @@ from subprocess import Popen, PIPE import sys import urllib2 import urlparse +from random import randint from openlp.core.lib.settings import Settings @@ -60,6 +61,29 @@ UNO_CONNECTION_TYPE = u'pipe' #UNO_CONNECTION_TYPE = u'socket' CONTROL_CHARS = re.compile(r'[\x00-\x1F\x7F-\x9F]', re.UNICODE) INVALID_FILE_CHARS = re.compile(r'[\\/:\*\?"<>\|\+\[\]%]', re.UNICODE) +USER_AGENTS = { + u'win32': [ + 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36', + 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36', + 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36', + '' + ], + u'darwin': [ + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31', + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11', + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11', + + ], + u'linux2': [ + 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22', + 'Mozilla/5.0 (X11; CrOS armv7l 2913.260.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.99 Safari/537.11', + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.27 (KHTML, like Gecko) Chrome/26.0.1389.0 Safari/537.27' + ], + u'default': [ + 'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0' + ] +} + class VersionThread(QtCore.QThread): """ @@ -92,7 +116,7 @@ class AppLocation(object): VersionDir = 5 CacheDir = 6 LanguageDir = 7 - + # Base path where data/config/cache dir is located BaseDir = None @@ -364,6 +388,7 @@ def get_images_filter(): visible_formats, actual_formats) return IMAGES_FILTER + def is_not_image_file(file_name): """ Validate that the file is not an image file. @@ -380,6 +405,7 @@ def is_not_image_file(file_name): return False return True + def join_url(base, *args): """ Join one or more url components with the base url. @@ -438,6 +464,17 @@ def delete_file(file_path_name): return False +def _get_user_agent(): + """ + Return a user agent customised for the platform the user is on. + """ + browser_list = USER_AGENTS.get(sys.platform, None) + if not browser_list: + browser_list = USER_AGENTS[u'default'] + random_index = randint(0, len(browser_list) - 1) + return browser_list[random_index] + + def get_web_page(url, header=None, update_openlp=False): """ Attempts to download the webpage at url and returns that page or None. @@ -458,6 +495,9 @@ def get_web_page(url, header=None, update_openlp=False): if not url: return None req = urllib2.Request(url) + user_agent = _get_user_agent() + log.debug(u'Using user agent: %s', unicode(user_agent)) + req.add_header('User-Agent', user_agent) if header: req.add_header(header[0], header[1]) page = None From 60b7f2dbbd3be17d6cb961fbd104fdb4272fce7a Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 13 Dec 2013 22:41:16 +0200 Subject: [PATCH 2/3] Some unnecessary things removed --- openlp/core/utils/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 422e9a48b..964513507 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -65,14 +65,12 @@ USER_AGENTS = { u'win32': [ 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36', - 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36', - '' + 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36' ], u'darwin': [ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11', - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11', - + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11' ], u'linux2': [ 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22', From a023e63f21a4bb2b40c4ab94cd9d0cd4045921b2 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 20 Dec 2013 21:36:31 +0200 Subject: [PATCH 3/3] Specify OpenLP as the user agent when querying SF.net --- openlp/core/ui/firsttimeform.py | 5 +++-- openlp/core/utils/__init__.py | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/openlp/core/ui/firsttimeform.py b/openlp/core/ui/firsttimeform.py index a24876e88..c0c8515b0 100644 --- a/openlp/core/ui/firsttimeform.py +++ b/openlp/core/ui/firsttimeform.py @@ -45,7 +45,7 @@ from openlp.core.lib import translate, PluginStatus, Receiver, build_icon, \ check_directory_exists from openlp.core.lib.settings import Settings from openlp.core.utils import get_web_page, AppLocation, join_url, \ - get_filesystem_encoding + get_filesystem_encoding, get_application_version from firsttimewizard import Ui_FirstTimeWizard, FirstTimePage log = logging.getLogger(__name__) @@ -106,7 +106,8 @@ class FirstTimeForm(QtGui.QWizard, Ui_FirstTimeWizard): # url is defined in 'download.cfg' file. self.baseurl = None # Check to see if we have web access - self.webAccess = get_web_page(u'%s%s' % (self.web, u'download.cfg')) + user_agent = u'OpenLP/' + get_application_version()[u'version'] + self.webAccess = get_web_page(u'%s%s' % (self.web, u'download.cfg'), header=(u'User-Agent', user_agent)) if self.webAccess: files = self.webAccess.read() self.config.readfp(io.BytesIO(files)) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 964513507..cead844fd 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -493,11 +493,11 @@ def get_web_page(url, header=None, update_openlp=False): if not url: return None req = urllib2.Request(url) - user_agent = _get_user_agent() - log.debug(u'Using user agent: %s', unicode(user_agent)) - req.add_header('User-Agent', user_agent) - if header: - req.add_header(header[0], header[1]) + if not header or header[0].lower() != u'user-agent': + user_agent = _get_user_agent() + req.add_header('User-Agent', str(user_agent)) + elif header: + req.add_header(str(header[0]), str(header[1])) page = None log.debug(u'Downloading URL = %s' % url) try: