Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py3 #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

py3 #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
language: python

before_install: pip install -r requirements.txt
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7-dev"

script: nosetests
script: python setup.py test
6 changes: 3 additions & 3 deletions packtrack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from correios import EncomendaRepository
from royal import RoyalMail
from dhl_gm import DhlGmTracker
from .correios import EncomendaRepository
from .royal import RoyalMail
from .dhl_gm import DhlGmTracker


class Correios(object):
Expand Down
9 changes: 9 additions & 0 deletions packtrack/bs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import functools

try:
from bs4 import BeautifulSoup
BeautifulSoup = functools.partial(BeautifulSoup, features="lxml")
except ImportError:
from BeautifulSoup import BeautifulSoup

BeautifulSoup = BeautifulSoup
4 changes: 2 additions & 2 deletions packtrack/correios.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get(self, numero, auth=None):
return func(numero, **kwargs)

def _init_scraper(self, backend):
from scraping import CorreiosWebsiteScraper, CorreiosRastroService
from .scraping import CorreiosWebsiteScraper, CorreiosRastroService
if backend is None:
backend = 'www2'

Expand All @@ -37,7 +37,7 @@ def adicionar_status(self, status):
d = datetime
self.status.append(status)
t_format = self.validar_data(status.data)
self.status.sort(lambda x, y: 1 if d.strptime(x.data, t_format) > d.strptime(y.data, t_format) else -1)
self.status.sort(key=lambda x: d.strptime(x.data, t_format))

def validar_data(self, data):
if re.match('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$', data):
Expand Down
3 changes: 2 additions & 1 deletion packtrack/royal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
from bs4 import BeautifulSoup

from .bs import BeautifulSoup


class RoyalMail(object):
Expand Down
31 changes: 23 additions & 8 deletions packtrack/scraping.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import os
import re
from HTMLParser import HTMLParser

from BeautifulSoup import BeautifulSoup
try:
from html.parser import HTMLParser
from html import unescape
except ImportError:
from HTMLParser import HTMLParser
unescape = None

import requests
from requests.exceptions import RequestException
from zeep import Client as Zeep
from zeep.cache import InMemoryCache
from zeep.transports import Transport

from correios import Encomenda, Status
from .bs import BeautifulSoup
from .correios import Encomenda, Status


class CorreiosWebsiteScraper(object):
Expand Down Expand Up @@ -65,6 +71,8 @@ def _text(self, value):
def _get_all_status_from_html(self, html):
status = []
html_parser = HTMLParser()
_unescape = unescape or html_parser.unescape

if "<table" not in html:
return status
html_info = re.search('.*(<table.*</table>).*', html, re.S)
Expand All @@ -80,15 +88,22 @@ def _get_all_status_from_html(self, html):
except AttributeError:
continue
for td in tds:
content = td.renderContents().replace('\r', ' ') \
.split('<br />')
class_ = td['class']
content = td.renderContents()
content = content.replace(b'\r', b' ')
content = content.replace(b'\xa0', b' ')
content = content.replace(b'<br />', b'<br/>')
content = content.split(b'<br/>')

# bs4 return a list, bs3 return a string, join normalize
# this behaviour
class_ = "".join(td['class'])

if class_ == 'sroDtEvent':
data = '%s %s' % (content[0].strip(), content[1].strip())
local = '/'.join(self._text(content[2]).rsplit(' / ', 1)).upper()
elif class_ == 'sroLbEvent':
situacao = html_parser.unescape(self._text(content[0]))
detalhes = html_parser.unescape(self._text(content[1]))
situacao = _unescape(self._text(content[0]))
detalhes = _unescape(self._text(content[1]))
if detalhes:
detalhes = u'%s %s' % (situacao, detalhes)
status.append(Status(data=data, local=local,
Expand Down
8 changes: 0 additions & 8 deletions requirements.txt

This file was deleted.

25 changes: 18 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# coding: UTF-8
from setuptools import setup
import sys

install_requires = [
'requests >= 0.14.2',
'lxml >= 3.0.0',
'zeep >= 1.6.0',
]
if sys.version_info >= (3, 0):
install_requires.append('beautifulsoup4 >= 4.3.2')
else:
install_requires.append('BeautifulSoup >= 3.1.0')

tests_require = [
]


setup(
name='packtrack',
Expand All @@ -12,11 +27,7 @@
keywords='encomendas track api',
url='https://github.com/aleborba/packtrack',
long_description='API Python para obter informacoes de encomendas. Para mais detalhes veja a documentacao no Github: https://github.com/aleborba/packtrack/blob/master/README.textile',
install_requires=[
'BeautifulSoup >= 3.1.0',
'requests >= 0.14.2',
'beautifulsoup4 >= 4.3.2',
'lxml >= 2.3.5',
'zeep >= 1.6.0',
],
install_requires=install_requires,
test_suite="tests",
tests_require=tests_require,
)
14 changes: 8 additions & 6 deletions tests/correios_api_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest

from mock import Mock
from mockito import when
try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

from packtrack import Correios

Expand All @@ -10,20 +12,20 @@ class CorreiosTest(unittest.TestCase):

def test_should_use_repository_to_get_encomenda(self):
encomenda_repository_mock = Mock()
when(encomenda_repository_mock).get('123', auth=None) \
.thenReturn('encomenda123')
encomenda_repository_mock.get.return_value = "encomenda123"

Correios._backends[None] = encomenda_repository_mock

assert Correios.track('123') == 'encomenda123'
encomenda_repository_mock.get.assert_called_with("123", auth=None)

def test_service_should_receive_auth(self):
auth = ('mi', 'mimi')
encomenda_repository_mock = Mock()
when(encomenda_repository_mock).get('123', auth=auth) \
.thenReturn('encomenda123')
encomenda_repository_mock.get.return_value = "encomenda123"

Correios._backends['service'] = encomenda_repository_mock

assert Correios.track(
'123', backend='service', auth=auth) == 'encomenda123'
encomenda_repository_mock.get.assert_called_with("123", auth=auth)
10 changes: 6 additions & 4 deletions tests/correios_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest

from mock import Mock
from mockito import *
try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

from packtrack.correios import Encomenda, Status, EncomendaRepository

Expand All @@ -11,8 +13,8 @@ def test_should_get_encomenda_by_numero(self):
encomenda_123 = Status(data='2009-01-28 17:49:00')

correios_website_scraper_mock = Mock()
when(correios_website_scraper_mock).get_encomenda_info('123', auth=None).thenReturn(encomenda_123)
correios_website_scraper_mock.get_encomenda_info.return_value = encomenda_123

repository = EncomendaRepository()
repository.correios_website_scraper = correios_website_scraper_mock
encomenda = repository.get('123')
Expand Down
11 changes: 7 additions & 4 deletions tests/scraping_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import os
import unittest

import mock
try:
from unittest import mock
except ImportError:
import mock

from packtrack.scraping import CorreiosWebsiteScraper
from packtrack.dhl_gm import DhlGmTracker
Expand All @@ -17,9 +20,9 @@ def _assert_status(self, status, data, local, situacao, detalhes):
self.assertEqual(detalhes, status.detalhes)

def test_should_get_data_from_correios_website(self):
example_file = open('%s/tests/correios_website/exemplo_rastreamento_correios1.html' % os.getcwd())
sample_html = example_file.read()
example_file.close()
filename = '%s/tests/correios_website/exemplo_rastreamento_correios1.html' % os.getcwd()
with open(filename, "rb") as f:
sample_html = f.read()

http_client_mock = mock.Mock()
response_mock = mock.Mock()
Expand Down