# https://bot.sannysoft.com/
from pyppeteer import *
from pyppeteer_stealth import stealth
import asyncio
# --------------------------------------------------------------
def handle_price(some_price):
for element in some_price:
try:
int(element)
except:
some_price = some_price.replace(element, '')
return some_price
#-----
async def get_wildberries_price(browser):
page = await browser.newPage()
await page.goto('https://www.wildberries.ru/catalog/98649739/detail.aspx')
await page.waitFor('.price-block__final-price')
raw_price = await page.evaluate('''
() => {
return document.querySelector('.price-block__final-price').textContent
}
''')
final_price = handle_price(raw_price)
await page.screenshot({'path': 'wildberries.png'})
return final_price
#-----
async def get_ozon_price(browser):
page = await browser.newPage()
await stealth(page)
await page.goto('https://www.ozon.ru/product/setka-sadovaya-plastikovaya-0-61-sm-opornaya-dlya-rasteniy-rulon-5-m-yacheyka-7-10-mm-zelenaya-746399567/?advert=LMWD3jhEugrU0NaiTNSLwpAIIbCdFMNn7pzKseZ54oiYJzffMkWL5dFUOvP2QxFxTe6r-62ds5iYdNtmm39GeVmWNx7Drk2RfA-NFruujb9wW_wbefQotjVI3Nw6nmw-7bz9eBuQf1jcWO9oKG03ATRZlVPYDWDUpt-Vr6TP01QKzNpkDGeyKNFDYr0mQ6zHtTc2xumWB3-dv2Gz-N1MubGGYv1HNBGFFtjS2sjma9sLgVekajUUaWojBH262l4QrAW1M9rCl-81e2UuCuA6e9e30jQi6V5VQGajDtoh7hlSqVBeuWTgQwBh93dIZprdLVVLO8wjhyroY4zH5kCXsPosmppupEfHuVI3WyxszZDRKZaftEvaKMKR1BDr1rtJDPW_pNvM1ZhWuCyJp9hsDQsED8fQ3RFRWR8LNZ5WYr8h_sjfYw1xlrl5-rjcS2MaBuBeYL3GQ4pXFwrkRKOEFuLsDScNPdIL2rDN9aeFT_lW5QnmTG_L177BU5QyZM2DLu7pN8H3K0To7QDSzvLpfVqCHWaudAT54N6mTU8GAtwBLp6sWpQNKDwnqqFLxmdC1s-3-U2t3h6jwkvEH5nue2hZdmrdu4Z-eD2qbnblV340tMRQZ3V8vcw-QO0BiDqNQupojM-i61GULUdv8a8qa12Uir-HZ1OOaKekgidc-IKoCUbTCa8YfpV5zXASOBlxvqs84FoG8vEg2yNkN4fRSOOkfcSp29rssttskpEkp14dZ3o_XnJ93SI1SR13ffRmFMGVZiwDjOyHjovEELGadA&avtc=1&avte=2&avts=1674570447&keywords=%D0%BF%D0%BB%D0%B0%D1%81%D1%82%D0%B8%D0%BA%D0%BE%D0%B2%D0%B0%D1%8F+%D1%81%D0%B5%D1%82%D0%BA%D0%B0+%D1%81%D0%BB%D0%B0%D0%B2&sh=6RhLQaLOhA')
await page.waitFor('[slot="content"] span')
raw_price = await page.evaluate('''
() => {
return document.querySelector('[slot="content"] span').textContent
}
''')
final_price = handle_price(raw_price)
await page.screenshot({'path': 'ozon.png'})
return final_price
# --------------------------------------------------------------
async def main():
main_browser = await launch({
# 'headless': False,
})
print(await get_wildberries_price(main_browser))
print(await get_ozon_price(main_browser))
await main_browser.close()
main_loop = asyncio.new_event_loop()
main_loop.run_until_complete(main())
Error: (Future exception was never retrieved
future:<Future finished exception=NetworkError('Protocol error Target.sendMessageToTarget: Target closed.')>
pyppeteer.errors.NetworkError: Protocol error Target.sendMessageToTarget: Target closed)
This error appears not often (about 1 time in a 4 starts).
But I noticed if I change places
print(await get_wildberries_price(main_browser))
print(await get_wildberries_price(main_browser))
code will be OK
I don't understand why place is important
I beg, help me, nothing works
Related
so i tried to make a spam function and run it but it just gives this error: RuntimeWarning: coroutine 'spam' was never awaited
spam(channel)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
and no message is being sent.
this is the code:
await guild.create_text_channel("NEEDLESS-NUKER")
for channel in guild.text_channels:
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
amount = 500
for i in range(amount):
await guild.create_text_channel(random.choice(SPAM_CHANNEL))
spam(channel)
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
print(f"{c.YELLOW}[{date}]{c.GREEN} Nuked {guild.name} Successfully.")
return
async def spam(channel):
for i in range(15):
try:
names = json.loads(open("words.json").read())
SPAM_MESSAGE = [
"#everyone LOL GET NUKED :skull::skull:",
"#everyone LOL IMAGINE GETTING NUKED :skull::skull:",
"lol hahaha get nuked",
f"im {random.choice(names)}",
f"Lol this is so random like {random.choice(names)}#{random.randint(1, 1000)} :skull:",
f"Made By {random.choice(names)}!",
]
msg = random.choice(SPAM_MESSAGE)
await channel.send(msg)
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
print(f"{c.YELLOW}[{date}]{c.GREEN} Succeffully send {msg}")
except Exception as e:
names = json.loads(open("words.json").read())
SPAM_MESSAGE = [
"#everyone LOL GET NUKED JOIN https://dsc.gg/pythondevs",
"#everyone LOL IMAGINE GETTING NUKED :skull::skull:",
"#everyone https://dsc.gg/pythondevs",
f"#everyone im {random.choice(names)}",
f"#everyone Lol this is so random like {random.choice(names)}!{random.randint(1000, 10000)}",
f"#everyone Made By {random.choice(names)}!",
]
msg = random.choice(SPAM_MESSAGE)
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
print(f"{c.YELLOW}[{date}]{c.RED} Failed Sending Message {msg} exc: {e}")
first it was #client.event and then on_guild_channel_create(channel) and it worked but now it wont work i changed it because everytime i created a channel with this bot it would send 15 messages in the channel and if i make a voice channel it just gives me an error.
so please help me fix this. Thanks.
You're calling an async function, so you need to create_task it or await it.
import asyncio
import time
async def wait:
time.sleep(1)
await asyncio.sleep(1)
return None
result = asyncio.create_task(wait()) #Wait 1 second.
await wait() #Wait 2 seconds because it will wait for execution to end.
In your case, you would need to change line 8 from (8th line of the answer)
spam(channel)
to
await spam(channel)
Good day, I wrote a bot that responds to a user's request, but it happens that a message (also a file) intended for one user goes to another.
In the code I update the message and with this I show the download percentage of the sent video, but it happens that the message is updated from another chat, and the video goes to another user.
I'm not experienced enough in asynchronous programming.
from telethon import events, sync
from telethon.tl.types import PeerUser
from telethon.sync import TelegramClient
from decoder import *
from vodfinder import vod_list_create
import time, json
#git add . && git commit -m "g" && git push heroku master && heroku logs -t
class BotUser:
def __init__(self, mess_id, id):
self.id = id
self.user = PeerUser(user_id=self.id)
self.mess_id = mess_id
self.mess_text = ""
def upd_text(self, text):
self.mess_text = text
def step(self):
self.mess_id += 1
constants = json.load(open('constants.json'))
app = TelegramClient("TwitchTgBot",
api_hash=constants['api_hash'],
api_id=constants['api_id'])
ses_list = []
old_ses=""
#app.on(events.NewMessage(incoming=False))
async def messs_handler(message):
print(f"Outgoing: {message.id}")
async def downloader(filename, tsfile):
opener = urllib.request.build_opener()
tsfile.write(opener.open(filename).read())
#app.on(events.NewMessage(pattern='(/start)'))
async def start_handler(message):
session = BotUser(message.id + 3, message.chat.id)
ses_list.append(session)
await app.send_message(session.user,
f"Sup, its bot who can send you VODs from twitch, send me request!")
#app.on(events.NewMessage(incoming=True))
async def mess_handler(message):
print(f"Incoming : {message.id}")
if message.text[0] == "/":
return
raw_mess = message.text.split()
mess_id = message.id
chat = message.chat
for ses in ses_list:
if ses.id == chat.id:
session = ses
break
else:
await app.send_message(chat,
f"Bot doesn't start, send /start.")
return
async def upload_prg(current, total):
global old_ses
if old_ses != session:
print(f"Session changed to {session.id}")
old_ses = session
procent = f'{current * 100 / total:.1f}'
if session.mess_text != procent:
await app.edit_message(session.user, session.mess_id,
f"Uploading {procent} % ...")
session.upd_text(procent)
await asyncio.sleep(2)
async def download_prg(num, seg_range):
procent = f'{num * 100 / len(seg_range):.1f}'
if procent != session.mess_text:
await app.edit_message(session.user, session.mess_id,
f"Downloading {procent} % ...")
session.upd_text(procent)
await asyncio.sleep(2)
if len(raw_mess) == 5:
start_tm = int(raw_mess[2])
end_tm = int(raw_mess[3])
res_fps = raw_mess[4]
elif len(raw_mess) == 2:
start_tm = 0
end_tm = -1
res_fps = '720p60'
elif len(raw_mess) == 3:
start_tm = 0
end_tm = -1
res_fps = raw_mess[2]
else:
return
channel = raw_mess[0]
vod_cnt = int(raw_mess[1])
vod = vod_list_create(channel)[vod_cnt]
link = vod.vod_link.format(res_fps=res_fps)
print('A>>>>>>A\n')
print(f'User: {chat.username}')
print(f"Request: {message.text}")
print(vod.__str__())
print('Loading M3U8...')
end_tm, dump = dump_m3u8(link, end_tm)
print('M3U8 playlist loded.')
chunk_dur, parts = get_chunk_dur(res_fps, end_tm, start_tm)
print(f'Chunk duration is {chunk_dur}, splitted to {parts} part.')
await app.send_message(session.user,
f"Sending | {vod.__str__()} | splitted to {parts} parts...")
session.step()
last_seg = 0
for id_ in range(0, parts):
chunk_end = start_tm + chunk_dur
print(f'Decoding segments from {start_tm} to {chunk_end}.')
last_seg, seg_range = sec_to_seg(start_tm, chunk_end, last_seg, dump)
start_tm = chunk_end
await app.send_message(session.user,
f"Downloading {id_ + 1} part ...")
session.step()
print('Decoded, downloading ...')
tsfile = open(f'Downloaded/mg{id_}.mp4', 'ab')
for num, seg in enumerate(seg_range):
await downloader(link + str(seg)+ '.ts', tsfile)
await download_prg(num, seg_range)
tsfile.close()
print(f'Download of mg{id_}.ts finished.')
print(f'Sending mg{id_}.ts ...')
await app.send_file(session.user, f'Downloaded/mg{id_}.mp4', supports_streaming=True,
progress_callback=upload_prg)
await app.delete_messages(session.user, session.mess_id)
session.step()
print(f'mg{id_}.ts sended.')
print(f"[Sucsess] {chat.username}'s session finished.")
ses_list.remove(session)
app.start(bot_token=constants['token'] )
app.run_until_disconnected()
it accepts the request, downloads the video, sends it to the user
My Discord bot allows users to play a song starting from a timestamp.
The problem is that playback is delayed and audio plays faster and is jumbled if start times >= 30s are set.
Results from testing different start times. Same URL, 30 second duration:
Entered Start Time (s)
Playback Delay (s)
Song Playback Time (s)
0
3
30
30
10
22
60
17
17
120
31
2
150
120
<1
I am setting the start time using ffmpeg_options as suggested in this question.
Does anyone understand why the audio playback is being delayed/jumbled? How can I improve playback delay and allow users to start in the middle of a multi-chapter YouTube video?
Code:
import discord
import youtube_dl
import asyncio
# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ""
ytdl_format_options = {
"format": "bestaudio/best",
"outtmpl": "%(extractor)s-%(id)s-%(title)s.%(ext)s",
"restrictfilenames": True,
"noplaylist": False,
"yesplaylist": True,
"nocheckcertificate": True,
"ignoreerrors": False,
"logtostderr": False,
"quiet": True,
"no_warnings": True,
"default_search": "auto",
"source_address": "0.0.0.0", # Bind to ipv4 since ipv6 addresses cause issues at certain times
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source: discord.AudioSource, *, data: dict, volume: float = 0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get("title")
self.url = data.get("url")
#classmethod
async def from_url(cls, url, *, loop=None, stream=False, timestamp = 0):
ffmpeg_options = {
"options": f"-vn -ss {timestamp}"}
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if "entries" in data:
# Takes the first item from a playlist
data = data["entries"][0]
filename = data["url"] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
intents = discord.Intents.default()
bot = discord.Bot(intents=intents)
#bot.slash_command()
async def play(ctx, audio: discord.Option(), seconds: discord.Option(), timestamp: discord.Option()):
channel = ctx.author.voice.channel
voice = await channel.connect()
player = await YTDLSource.from_url(audio, loop=bot.loop, stream=True, timestamp=int(timestamp))
voice.play(player)
await asyncio.sleep(int(seconds))
await voice.disconnect()
token = token value
bot.run(token)
I have a music bot that streams radio and each stream is a command for ease of use. The problem is that there are 100+ stations now and it's getting increasingly more difficult to update the code for each one.
#bot.command(aliases=["VAR1", "VAR2"])
#commands.check(if_channel_private)
async def VAR(ctx):
current_time = datetime.now().strftime("%H:%M")
station = 'VAR3'
if len(embed_history.fields) <= 4:
embed_history.add_field(inline=False, name=f"[{current_time}] Played:", value=f'`{station}`')
elif len(embed_history.fields) > 4:
embed_history.remove_field(0)
embed_history.add_field(inline=False, name=f"[{current_time}] Played:", value=f'`{station}`')
stream = 'URL VAR4'
resume = stream
if len(pause_list) != 0 or len(radio_station) != 0:
pause_list.clear()
radio_station.clear()
voice = get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.stop()
try:
voice.play(FFmpegPCMAudio(stream))
print(f'Playing {station}')
except:
print(f"An error occurred while trying to play {station}")
await ctx.send(error_message)
radio_station.append(station)
pause_list.append(resume)
embed_st.clear_fields()
embed_st.add_field(inline=False, name=embed_name,
value="VAR5")
await ctx.send(embed=embed_st)
#VAR.error
async def _pr_error(ctx, error):
if isinstance(error, commands.CheckFailure):
if ctx.message.author.voice is None:
return
await ctx.send(f"Only users in __the same private voice channel__ can change the station!")
else:
await ctx.send(f"```css\n"
"[ERROR: Unexpected error has occured!]"
"```")
print(error)
This is how it looks like for each station. Is it possible to write this only once and then call it for every station? And only to have to change the variables (VAR)? Otherwise, it bloats the whole text file with repetitive code...
I don't check this code, but may be it's work. Create function, and send to it your variables station and stream:
#bot.command(aliases=["VAR", "VAR"])
#commands.check(if_channel_private)
async def VAR(ctx):
await some_function('VAR', 'URL_VAR')
# Your function for all stations
async def some_function(station, stream):
current_time = datetime.now().strftime("%H:%M")
if len(embed_history.fields) <= 4:
embed_history.add_field(inline=False, name=f"[{current_time}] Played:", value=f'`{station}`')
elif len(embed_history.fields) > 4:
embed_history.remove_field(0)
embed_history.add_field(inline=False, name=f"[{current_time}] Played:", value=f'`{station}`')
resume = stream
if len(pause_list) != 0 or len(radio_station) != 0:
pause_list.clear()
radio_station.clear()
voice = get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.stop()
try:
voice.play(FFmpegPCMAudio(stream))
print(f'Playing {station}')
except:
print(f"An error occurred while trying to play {station}")
await ctx.send(error_message)
radio_station.append(station)
pause_list.append(resume)
embed_st.clear_fields()
embed_st.add_field(inline=False, name=embed_name,
value="VAR")
await ctx.send(embed=embed_st)
All the requests are working normally but after 90% of the tasks the responds get hung and timeout.
In the following code, if you change n_requests to 1000 or 10 the problem is still there and it seems always to be 10% which wont get a response.
It only seems to work with 2 requests (n_requests = 2):
import asyncio
import aiohttp
n_requests = 100
async def make_request(session, req_n):
url = f"https://tbit-web.de/exec/ximg.php?fid={req_n}"
print(req_n)
async with session.get(url) as resp:
print(req_n, "finish")
async def main():
async with aiohttp.ClientSession() as session:
counternr = 0
while counternr < 9999:
await asyncio.gather(
*[make_request(session, i) for i in range(counternr, counternr + n_requests)]
)
counternr= n_requests + counternr
loop = asyncio.get_event_loop()
loop.run_until_complete(main())