Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Changelog
Due to this library relying on external content, older versions are not guaranteed to work.
Try to always use the latest version.

.. _v2.3.0:

2.3.0 (2019-09-16)
==================
- Added proxy option to client.

.. _v2.2.6:

2.2.6 (2019-09-01)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
beautifulsoup4
lxml
aiohttp
aiohttp
aiohttp-socks
2 changes: 1 addition & 1 deletion serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def middleware_handler(request):


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

if __name__ == "__main__":
app = web.Application(middlewares=[error_middleware])
Expand Down
2 changes: 1 addition & 1 deletion tibiapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tibiapy.creature import *
from tibiapy.client import *

__version__ = '2.2.6'
__version__ = '2.3.0'

from logging import NullHandler

Expand Down
19 changes: 14 additions & 5 deletions tibiapy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime

import aiohttp
import aiohttp_socks

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


class Client:
"""An asynchronous client that fetches information from Tibia.com.ArithmeticError
"""An asynchronous client that fetches information from Tibia.com

The client uses a :class:`aiohttp.ClientSession` to request the information.
A single session is shared across all operations.
Expand All @@ -29,20 +30,26 @@ class Client:
The event loop to use. The default one will be used if not defined.
session: :class:`aiohttp.ClientSession`
The client session that will be used for the requests. One will be created by default.
proxy_url: :class:`str`
The URL of the SOCKS proxy to use for requests.
Note that if a session is passed, the SOCKS proxy won't be used and must be applied when creating the session.
"""
def __init__(self, loop=None, session=None):

def __init__(self, loop=None, session=None, *, proxy_url=None):
self.loop = asyncio.get_event_loop() if loop is None else loop # type: asyncio.AbstractEventLoop
if session is not None:
self.session = session # type: aiohttp.ClientSession
else:
self.loop.create_task(self._initialize_session())
self.loop.create_task(self._initialize_session(proxy_url))

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

@classmethod
def _handle_status(cls, status_code):
Expand Down Expand Up @@ -81,6 +88,8 @@ async def _get(self, url):
return await resp.text()
except aiohttp.ClientError as e:
raise NetworkError("aiohttp.ClientError: %s" % e, e)
except aiohttp_socks.SocksConnectionError as e:
raise NetworkError("aiohttp_socks.SocksConnectionError: %s" % e, e)

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