-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proxies support, docs,
UserAgentService
. v0.5.0
- Loading branch information
1 parent
22af4c1
commit 6c8f324
Showing
17 changed files
with
281 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""User agents service extension.""" | ||
|
||
from collections import UserList | ||
from random import choice | ||
|
||
from yarl import URL | ||
from aiohttp import ClientSession | ||
|
||
API_URL = URL("https://randua.somespecial.one") | ||
|
||
|
||
class UserAgentsService(UserList[str]): | ||
""" | ||
List-like class of user agents responsible for loading and getting random user agents. | ||
.. seealso:: https://github.com/somespecialone/random-user-agent | ||
""" | ||
|
||
__slots__ = ("_api_url",) | ||
|
||
def __init__(self, *, api_url=API_URL): | ||
""" | ||
:param api_url: url of `random user agent` backend service api | ||
""" | ||
|
||
super().__init__() | ||
|
||
self._api_url = api_url | ||
|
||
@property | ||
def agents(self) -> list[str]: | ||
return self.data | ||
|
||
async def load(self): | ||
async with ClientSession(raise_for_status=True) as sess: | ||
r = await sess.get(self._api_url / "all") | ||
agents: list[str] = await r.json() | ||
|
||
self.data = agents | ||
|
||
def get_random(self) -> str: | ||
return choice(self.agents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.