Python asyncio.exceptions.CancelledError - python

I'm trying to write a program that receives data about the lots of a marketplace via a websocket. Then the program compares each lot with my list of lots and if the lot is profitable it buys it. After starting the program runs for 5-10 minutes after which I get an error.
Traceback (most recent call last):
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\protocol.py", line 968, in transfer_data
message = await self.read_message()
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\protocol.py", line 1038, in read_message
frame = await self.read_data_frame(max_size=self.max_size)
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\protocol.py", line 1113, in read_data_frame
frame = await self.read_frame(max_size)
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\protocol.py", line 1170, in read_frame
frame = await Frame.read(
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\framing.py", line 99, in read
data = await reader(length)
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\asyncio\streams.py", line 707, in readexactly
await self._wait_for_data('readexactly')
File "C:\Users\Администратор.WIN-TJGJ26MRVI4\AppData\Local\Programs\Python\Python310\lib\asyncio\streams.py", line 501, in _wait_for_data
await self._waiter
asyncio.exceptions.CancelledError`
with open('steam.json', encoding="utf-8") as f:
items_em = json.load(f)
async def main():
async with websockets.connect("wss://wsn.dota2.net/wsn/") as client:
await client.send('newitems_go')
while True:
data = json.loads(await client.recv())
data = json.loads(data['data'])
if float(data.get('ui_price')) >= float(min_price):
for item_em in items_em["items"]:
if item_em['name'] == data.get('i_market_hash_name'):
name_tm = data.get('i_market_hash_name')
price_tm = data.get('ui_price')
try:
price_em = item_em['prices'].get('steam').get('price')
except AttributeError:
continue
if price_tm and price_em != 0 and price_em is not None:
discount = round(float(price_tm)/73/float(price_em)*(-10000)+100, 1)
if discount >= float(min_discount):
resp = requests.get(f'https://market.csgo.com/api/v2/buy-for?key={your_secret_key}&hash_name={name_tm}&price={price_tm * 100}&partner={partner}&token={token}')
print(f'{name_tm} {price_tm} {discount}% {resp.text}')
asyncio.run(main())
What is my mistake?

Related

can't loop through chats and get premissions in telethon

I'm trying to get my group/channel permissions with the help of telethon but it gives an error whenever I run the get_permissions function inside a for-loop. This is the code:
client = TelegramClient(phone, apiId, apiHash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input("Enter the verification code:"))
chats = []
last_date = None
chunk_size = 200
groups = []
channels = []
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash=0
))
chats.extend(result.chats)
me = client.get_me()
for chat in chats:
permissions = client.get_permissions(chat, client.get_me())
if permissions.is_admin():
client.get_participants(chat)
else:
print("you don't have access to this channel/group")
and this is the error that it's giving me:
Request caused struct.error: required argument is not an integer: GetFullChatRequest(chat_id=InputPeerChat(chat_id=701294847))
Traceback (most recent call last):
File "/Users/artin/Documents/coding/work/Cryptal/cryptal-aggregator/src/sample.py", line 45, in <module>
permissions = client.get_permissions(chat, client.get_me())
File "/opt/homebrew/lib/python3.10/site-packages/telethon/sync.py", line 39, in syncified
return loop.run_until_complete(coro)
File "/opt/homebrew/Cellar/python#3.10/3.10.5/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/opt/homebrew/lib/python3.10/site-packages/telethon/client/chats.py", line 1278, in get_permissions
chat = await self(functions.messages.GetFullChatRequest(
File "/opt/homebrew/lib/python3.10/site-packages/telethon/client/users.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "/opt/homebrew/lib/python3.10/site-packages/telethon/client/users.py", line 63, in _call
future = sender.send(request, ordered=ordered)
File "/opt/homebrew/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 176, in send
state = RequestState(request)
File "/opt/homebrew/lib/python3.10/site-packages/telethon/network/requeststate.py", line 17, in __init__
self.data = bytes(request)
File "/opt/homebrew/lib/python3.10/site-packages/telethon/tl/tlobject.py", line 194, in __bytes__
return self._bytes()
File "/opt/homebrew/lib/python3.10/site-packages/telethon/tl/functions/messages.py", line 2065, in _bytes
struct.pack('<q', self.chat_id),
struct.error: required argument is not an integer

Error when saving data to MongoDB database

I want to make a role assignment using the support approval on the discord server, in general, a complex system.. To do this, I need to save the message id and the id of the message author who needs to be assigned a role, all the code is working, an error in the line of code associated with the database.
event code
cluster = MongoClient('mongodb+srv://corry:pa4-pD6-3Dj-AdT#cluster0.gi8x6.mongodb.net/?retryWrites=true&w=majority')
db = cluster.RolesData
roles = db.RolesCollection
#bot.event
async def on_message(message):
msg = message.content.lower()
request = ['роль', 'хочу роль', 'дайте роль', 'нужна роль']
if msg in request:
for i in orgs:
if not message.author.display_name.split(' ')[0].replace('[', '') in orgs:
await message.channel.send(f'`[ROLES]`: {message.author.mention}, тег организации не найден!')
break
if i == message.author.display_name.split(' ')[0].replace('[', ''):
guild = bot.get_guild(831511376425123882)
role = discord.utils.get(guild.roles, id=orgs[i])
await message.channel.send(f'`[ROLES]`: {message.author.mention}, ваш запрос на получение роли был отправлен саппортам!')
embed = discord.Embed(
title='Запрос на получение роли!',
description='✅ - выдать роль\n❌ - отклонить запрос\n🖊️ - изменить никнейм\n📋 - запросить статистику',
color=0x000000
)
embed.add_field(name=f'Пользователь:', value=f'{message.author.mention}', inline=False)
embed.add_field(name=f'Никнейм:', value=f'{message.author.display_name}', inline=False)
embed.add_field(name=f'Роль для выдачи:', value=f'{role.mention}', inline=False)
embed.set_thumbnail(url='https://psv4.userapi.com/c537232/u356224017/docs/d1/416eb92aec38/rodina.png?extra=2E9CwKZ0PIjnG8aaNkwehlHwjxlycBfGx-4p20ABm3mPI4jNpdV1OXaUUA9zGA4Q04VM21UezrXsjtqC411Xbh-Ro7rW1L4OGgNxpcQv3lvfOYCb-Irn-_-51AaQa2fpNDZhHm80dvZm1HlR1ZWoDigw')
embed.set_image(url=message.author.avatar_url)
msgemb = await bot.get_channel(851413212485779466).send(embed=embed)
await msgemb.add_reaction('✅')
await msgemb.add_reaction('❌')
await msgemb.add_reaction('🖊️')
await msgemb.add_reaction('📋')
roles_post = {
'm_id':msg_db.id,
'user':msg_db.author
}
if roles.count_documents({'m_id':message.id}) == 0:
roles.insert_one(roles_post)
else:
await message.channel.send(f'`[ROLES]`: {message.author.mention}, вы уже отправили запрос на получение роли!')
error code
Traceback (most recent call last):
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 89, in _resolve_uri
results = _resolve(
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 43, in _resolve
return resolver.resolve(*args, **kwargs)
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\dns\resolver.py", line 1305, in resolve
return get_default_resolver().resolve(qname, rdtype, rdclass, tcp, source,
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\dns\resolver.py", line 1176, in resolve
timeout = self._compute_timeout(start, lifetime)
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\dns\resolver.py", line 997, in _compute_timeout
raise Timeout(timeout=duration)
dns.exception.Timeout: The DNS operation timed out after 21.201510190963745 seconds
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Хозяин\Desktop\rodina\main.py", line 5, in <module>
cluster = MongoClient('mongodb+srv://corry:pa4-pD6-3Dj-AdT#cluster0.gi8x6.mongodb.net/?retryWrites=true&w=majority')
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\mongo_client.py", line 704, in __init__
res = uri_parser.parse_uri(
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\uri_parser.py", line 542, in parse_uri
nodes = dns_resolver.get_hosts()
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 121, in get_hosts
_, nodes = self._get_srv_response_and_hosts(True)
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 101, in _get_srv_response_and_hosts
results = self._resolve_uri(encapsulate_errors)
File "C:\Users\Хозяин\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 97, in _resolve_uri
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: The DNS operation timed out after 21.201510190963745 seconds
Для продолжения нажмите любую клавишу . . .
Judging by this bit self._raise_connection_failure(error) in your error message, I believe the issue lies within the fact that you're not successfully connecting to your db in the first place.

How to resolve RuntimeError: await wasn't used with future using aiohttp and asyncio in python?

I made some pretty simple script which pulls data from clicky.com api but for some reason it does not work as expected from time to time.
Sometimes it gets results but another time I am getting the following errors which I cant debug. I am fairly new to asyncio and aiohttp
Traceback (most recent call last):
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/1_stable/asy/usable/baza_goals.py", line 118, in <module>
goals_results_last_week = asyncio.run(goals_clicky_results_last_week())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
return future.result()
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/1_stable/asy/usable/baza_goals.py", line 82, in goals_clicky_results_last_week
responses_clicky_goals = await asyncio.gather(*tasks_goals)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1122, in send
return self._coro.send(arg)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request
conn = await self._connector.connect(
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect
proto = await self._create_connection(req, traces, timeout)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "/Users/almeco/Downloads/python/_projekty/projekt_baza_review/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 1040, in create_connection
sock = await self._connect_sock(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 954, in _connect_sock
await self.sock_connect(sock, address)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/selector_events.py", line 502, in sock_connect
return await fut
RuntimeError: await wasn't used with future
How to debug this? Whats the problem here?
edited:
Here is my code for you to test:
import asyncio
import datetime
import aiohttp
import requests
start_operacji = datetime.datetime.now()
print('start', start_operacji)
date_filename = datetime.datetime.now().strftime('%d-%m_%H-%M')
def goals_clicky_tasks_last_week(session):
tasks_clicky_goals = []
# todo to mozna by jeszcze dać do asyncio
clicky_auth = requests.get(
'https://api.clicky.com/api/account/sites?username=meeffe&password=hAs!23$5cy&output=json')
auth_jsonised = clicky_auth.json()
list_site_id_sitekey_dict = []
for k in auth_jsonised:
site_id_sitekey_dict = {'site_id': k['site_id'], 'sitekey': k['sitekey']}
list_site_id_sitekey_dict.append(site_id_sitekey_dict)
for auth_item in list_site_id_sitekey_dict:
goal_url = f"https://api.clicky.com/api/stats/4?site_id={auth_item['site_id']}&sitekey={auth_item['sitekey']}&type=goals&date=today&limit=1000&output=json"
tasks_clicky_goals.append(asyncio.ensure_future(session.get(goal_url, ssl=False)))
return tasks_clicky_goals
async def goals_clicky_results_last_week():
list_final_goals = []
async with aiohttp.ClientSession() as session:
tasks_goals = goals_clicky_tasks_last_week(session)
responses_clicky_goals = await asyncio.gather(*tasks_goals)
for response_clicky_goal in responses_clicky_goals:
clicky_data = await response_clicky_goal.json(content_type=None)
goals_list = []
for url_item_goal in clicky_data[0]['dates'][0]['items']:
if url_item_goal['conversion'] != '':
if url_item_goal['title'].startswith(
'http'): # nie bierze pod uwagę goalsów które zawierają U - https:// etc
goals_dict = {'url': url_item_goal['title'].replace('http://', 'https://'),
'goals': url_item_goal['value'],
'ad_ctr': url_item_goal['conversion']
}
goals_list.append(goals_dict)
else:
continue
else:
continue
list_final_goals.append(goals_list)
flattened_list_final_goals = [val for sublist in list_final_goals for val in sublist]
return flattened_list_final_goals
print(asyncio.run(goals_clicky_results_last_week()), 'goals_clicky_results_last_week')
goals_results_last_week = asyncio.run(goals_clicky_results_last_week())
######################################################################
end = datetime.datetime.now() - start_operacji
print('Ready:)!')
print('It took: ', end)
I actually found a solution by myself.
Instead of aiohttp I used httpx
I used timeout with every request
I removed unnecessary await
Changes in an original code below. Now the script run 100% stable. To be frank I am not sure which of these changes had the biggest impact but it works as expected.
timeout = httpx.Timeout(60.0, connect=60.0)
async with httpx.AsyncClient(verify=False, timeout=timeout) as session:
tasks_goals = goals_clicky_tasks_last_week(session)
responses_clicky_goals = await asyncio.gather(*tasks_goals)
for response_clicky_goal in responses_clicky_goals:
clicky_data = response_clicky_goal.json()
...

Tuning async with aiohttp.ClientSession and await session.request

I need help figure out what I did wrong and guidance to debug issues.
The program keep throwing one of these 3 errors. I'm using python 3.6 on window 10.
The error starts with crawling only 200 urls and I need to verify up to 20000 urls
OSError: [WinError 10038] An operation was attempted on something that is not a socket
aiohttp.client_exceptions.ServerTimeoutError: Timeout on reading data from socket
Traceback (most recent call last):
File "test_asyncio.py", line 91, in <module>
def func():
File "E:\git\AzureRepo\SharePoint\applitools\timer.py", line 8, in wrapper
runs = timeit.repeat(func, number=number,
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\lib\timeit.py", line 238, in repeat
return Timer(stmt, setup, timer, globals).repeat(repeat, number)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\lib\timeit.py", line 205, in repeat
t = self.timeit(number)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\lib\timeit.py", line 177, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
File "test_asyncio.py", line 110, in func
res = loop.run_until_complete(du_main(all_urls, all_site))
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "test_asyncio.py", line 82, in du_main
result = await asyncio.gather(*tasks)
File "test_asyncio.py", line 43, in du_fetch
async with session.get(url=url, allow_redirects=False,
File "E:\git\AzureRepo\SharePoint\applitools\.venv\lib\site-packages\aiohttp\client.py", line 1117, in __aenter__
self._resp = await self._coro
File "E:\git\AzureRepo\SharePoint\applitools\.venv\lib\site-packages\aiohttp\client.py", line 544, in _request
await resp.start(conn)
File "E:\git\AzureRepo\SharePoint\applitools\.venv\lib\site-packages\aiohttp\client_reqrep.py", line 890, in start
message, payload = await self._protocol.read() # type: ignore
File "E:\git\AzureRepo\SharePoint\applitools\.venv\lib\site-packages\aiohttp\streams.py", line 604, in read
await self._waiter
aiohttp.client_exceptions.ServerTimeoutError: Timeout on reading data from socket
ERROR - Unclosed connection
async def du_fetch(session, site, url):
# try:
t0 = datetime.datetime.now()
async with session.get(url=url, allow_redirects=False,
timeout=ClientTimeout(total=1200, # 1200 works
sock_connect=5, # 600 works
sock_read=5)) as resp:
redirect_url = resp.headers.get('Location', None)
t1 = datetime.datetime.now()
time_diff = round((t1 - t0).total_seconds(), 3)
# resp.close()
# logging.debug(f'du_fetch {site} {url.encode("utf-8")}')
'''
except Exception as e:
t1 = datetime.datetime.now()
time_diff = round((t1 - t0).total_seconds(), 3)
logging.debug(e)
return (site, 999,
t0, time_diff, url, e.__class__.__name__)
'''
return (site, resp.status,
t0, time_diff, url, redirect_url)
async def du_main(urls, sites):
client_timeout = aiohttp.ClientTimeout(
total=120) # total 300 seconds (5min) timeout # 12000 works
connector = aiohttp.TCPConnector(limit=100, # default is 100 # 600 works
# limit_per_host=0,
ssl=False)
dummy_jar = aiohttp.DummyCookieJar()
tasks = []
async with aiohttp.ClientSession(connector=connector,
timeout=client_timeout,
cookie_jar=dummy_jar) as session:
tasks = [du_fetch(session, sites[i], url)
for i, url in enumerate(urls)]
result = await asyncio.gather(*tasks)
logging.error(f'du_main {session}')
await session.close()
return result
if __name__ == '__main__':
loop = asyncio.get_event_loop()
res = loop.run_until_complete(du_main(all_urls, all_site))
loop.close()

Discord.py printing a invite link

I'm having an issue where i try to print a invite link into console, but instead it prints out
<generator object Client.create_invite at 0x000001D310A183B8>
<generator object Client.create_invite at 0x000001D310A65410>
i am using this code:
#client.event
async def on_ready():
#await client.change_presence(game=Game(name="with humans"))
print("Logged in as " + client.user.name)
await asyncio.sleep(3)
for server in client.servers:
for channel in server.channels:
channel_type = channel.type
if str(channel_type) == 'text':
invitelinknew = client.create_invite(destination=channel, xkcd=True, max_uses=100)
print(str(invitelinknew))
break
i tried changing print(invitelinknew) to print(str(invitelinknew)), but it didn't change the outcome
EDIT:
New errors when consuming the genrator with invitelinknew2 = list(invitelinknew)
and print(invitelinknew2):
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Rasmus/Python/discordbot/botnoggi2.py", line 128, in on_ready
invitelinknew2 = list(invitelinknew)
File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 2628, in create_invite
data = yield from self.http.create_invite(destination.id, **options)
File "C:\Program Files\Python36\lib\site-packages\discord\http.py", line 137, in request
r = yield from self.session.request(method, url, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 555, in __iter__
resp = yield from self._coro
File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 202, in _request
yield from resp.start(conn, read_until_eof)
File "C:\Program Files\Python36\lib\site-packages\aiohttp\client_reqrep.py", line 640, in start
message = yield from httpstream.read()
File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 641, in read
result = yield from super().read()
File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 476, in read
yield from self._waiter
AssertionError: yield from wasn't used with future
Future exception was never retrieved
future: <Future finished exception=ServerDisconnectedError()>
aiohttp.errors.ServerDisconnectedError
According to the documentation create_invite is a coroutine and requires the await keyword
Change your code to include it such as
if channel.type == discord.ChannelType.text:
invitelinknew = await client.create_invite(destination=channel, xkcd=True, max_uses=100)
print(invitelinknew.url)

Categories