Skip to content

Commit 34724ab

Browse files
authored
Merge pull request #19 from Galarzaa90/dev
v2.3.0
2 parents da069d0 + 9674696 commit 34724ab

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Changelog
66
Due to this library relying on external content, older versions are not guaranteed to work.
77
Try to always use the latest version.
88

9+
.. _v2.3.0:
10+
11+
2.3.0 (2019-09-16)
12+
==================
13+
- Added proxy option to client.
14+
915
.. _v2.2.6:
1016

1117
2.2.6 (2019-09-01)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
beautifulsoup4
22
lxml
3-
aiohttp
3+
aiohttp
4+
aiohttp-socks

serve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def middleware_handler(request):
143143

144144

145145
async def init_client(app):
146-
app["tibiapy"] = tibiapy.Client()
146+
app["tibiapy"] = tibiapy.Client(proxy_url='socks5://127.0.0.1:7744')
147147

148148
if __name__ == "__main__":
149149
app = web.Application(middlewares=[error_middleware])

tibiapy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tibiapy.creature import *
1414
from tibiapy.client import *
1515

16-
__version__ = '2.2.6'
16+
__version__ = '2.3.0'
1717

1818
from logging import NullHandler
1919

tibiapy/client.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33

44
import aiohttp
5+
import aiohttp_socks
56

67
import tibiapy
78
from tibiapy import Character, Guild, World, House, KillStatistics, ListedGuild, Highscores, Category, VocationFilter, \
@@ -14,7 +15,7 @@
1415

1516

1617
class Client:
17-
"""An asynchronous client that fetches information from Tibia.com.ArithmeticError
18+
"""An asynchronous client that fetches information from Tibia.com
1819
1920
The client uses a :class:`aiohttp.ClientSession` to request the information.
2021
A single session is shared across all operations.
@@ -29,20 +30,26 @@ class Client:
2930
The event loop to use. The default one will be used if not defined.
3031
session: :class:`aiohttp.ClientSession`
3132
The client session that will be used for the requests. One will be created by default.
33+
proxy_url: :class:`str`
34+
The URL of the SOCKS proxy to use for requests.
35+
Note that if a session is passed, the SOCKS proxy won't be used and must be applied when creating the session.
3236
"""
33-
def __init__(self, loop=None, session=None):
37+
38+
def __init__(self, loop=None, session=None, *, proxy_url=None):
3439
self.loop = asyncio.get_event_loop() if loop is None else loop # type: asyncio.AbstractEventLoop
3540
if session is not None:
3641
self.session = session # type: aiohttp.ClientSession
3742
else:
38-
self.loop.create_task(self._initialize_session())
43+
self.loop.create_task(self._initialize_session(proxy_url))
3944

40-
async def _initialize_session(self):
45+
async def _initialize_session(self, proxy_url=None):
4146
headers = {
4247
'User-Agent': "Tibia.py/%s (+https://github.com/Galarzaa90/tibia.py" % tibiapy.__version__,
4348
'Accept-Encoding': "deflate, gzip"
4449
}
45-
self.session = aiohttp.ClientSession(loop=self.loop, headers=headers) # type: aiohttp.ClientSession
50+
connector = aiohttp_socks.SocksConnector.from_url(proxy_url) if proxy_url else None
51+
self.session = aiohttp.ClientSession(loop=self.loop, headers=headers,
52+
connector=connector) # type: aiohttp.ClientSession
4653

4754
@classmethod
4855
def _handle_status(cls, status_code):
@@ -81,6 +88,8 @@ async def _get(self, url):
8188
return await resp.text()
8289
except aiohttp.ClientError as e:
8390
raise NetworkError("aiohttp.ClientError: %s" % e, e)
91+
except aiohttp_socks.SocksConnectionError as e:
92+
raise NetworkError("aiohttp_socks.SocksConnectionError: %s" % e, e)
8493

8594
async def _post(self, url, data):
8695
"""Base POST request, handling possible error statuses.

0 commit comments

Comments
 (0)