Skip to content

Commit

Permalink
Use requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jblakeman committed Oct 29, 2016
1 parent 01baae1 commit edad3ff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
6 changes: 3 additions & 3 deletions apt_select/__main__.py
Expand Up @@ -4,7 +4,7 @@
from sys import exit, stderr, version_info
from os import getcwd
from apt_select.arguments import get_args
from apt_select.utils import get_html, URLGetError
from apt_select.utils import get_text, URLGetTextError
from apt_select.mirrors import Mirrors
from apt_select.apt_system import AptSources, SourcesFileError

Expand Down Expand Up @@ -38,8 +38,8 @@ def get_mirrors(mirrors_url):
"""Fetch list of Ubuntu mirrors"""
stderr.write("Getting list of mirrors...")
try:
mirrors_list = get_html(mirrors_url)
except URLGetError as err:
mirrors_list = get_text(mirrors_url)
except URLGetTextError as err:
exit("Error getting list from %s:\n\t%s" % (mirrors_url, err))
stderr.write("done.\n")

Expand Down
10 changes: 5 additions & 5 deletions apt_select/mirrors.py
Expand Up @@ -7,7 +7,7 @@
from socket import (socket, AF_INET, SOCK_STREAM,
gethostbyname, error, timeout, gaierror)
from time import time
from apt_select.utils import get_html, URLGetError, progress_msg
from apt_select.utils import progress_msg, get_text, URLGetTextError
from apt_select.apt_system import AptSystem
try:
from urlparse import urlparse
Expand Down Expand Up @@ -73,8 +73,8 @@ def get_launchpad_urls(self):
"""Obtain mirrors' corresponding launchpad URLs"""
stderr.write("Getting list of launchpad URLs...")
try:
self._launchpad_html = get_html(self._launchpad_url)
except URLGetError as err:
self._launchpad_html = get_text(self._launchpad_url)
except URLGetTextError as err:
stderr.write((
"%s: %s\nUnable to retrieve list of launchpad sites\n"
"Reverting to latency only" % (self._launchpad_url, err)
Expand Down Expand Up @@ -302,8 +302,8 @@ def get_info(self):
Launchpad API doesn't support access to archivemirror statuses."""

try:
launch_html = get_html(self._launch_url)
except URLGetError as err:
launch_html = get_text(self._launch_url)
except URLGetTextError as err:
stderr.write("connection to %s: %s\n" % (self._launch_url, err))
self._data_queue.put_nowait((self._url, None))
else:
Expand Down
32 changes: 12 additions & 20 deletions apt_select/utils.py
@@ -1,35 +1,27 @@
#!/usr/bin/env python
"""Collection of module netural utility functions"""
"""Collection of module neutral utility functions"""

from sys import stderr
from ssl import SSLError
from socket import timeout
try:
from urllib.request import urlopen, HTTPError, URLError
except ImportError:
from urllib2 import urlopen, HTTPError, URLError
import requests


def utf8_decode(encoded):
return encoded.decode('utf-8')

class URLGetError(Exception):
"""Error class for retreiving and reading content from remote URL"""
pass

class URLGetTextError(Exception):
"""Error class for fetching text from a URL"""
pass

def get_html(url):
"""Retrieve and read HTML from URL"""
try:
html = urlopen(url)
except (HTTPError, URLError, SSLError, timeout) as err:
raise URLGetError(err)

def get_text(url):
"""Return text from GET request response content"""
try:
html = html.read()
except (SSLError, IOError, OSError) as err:
raise URLGetError(err)
text = requests.get(url).text
except requests.HTTPError as err:
raise URLGetTextError(err)

return utf8_decode(html)
return text


def progress_msg(processed, total):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -43,7 +43,7 @@
],
keywords='latency status rank reporting apt configuration',
packages=find_packages(exclude=['tests']),
install_requires=['beautifulsoup4'],
install_requires=['requests', 'beautifulsoup4'],
entry_points = {
'console_scripts': [
'apt-select = apt_select.__main__:main'
Expand Down

0 comments on commit edad3ff

Please sign in to comment.