i'm setting up a dicord bot i have usernames on a database so i get the user name ftrom a method that connects to database and return username in a string what i want is to mention from that string i can't get ir to work
i've tried searching on documentaton but i didnt understood it at all
async def job(channelid,message):
print('init schedule')
channel = client.get_channel(channelid)
current_apps=current_app()
usersapps=apps_user(current_apps["APPS"])
counter=0
while True:
counter= counter + 1
for i in range(0,len(usersapps)):
await discord.client.send_message("{}".format(usersapps[i]['USER']),mentions=True)
await asyncio.sleep(60) # task runs every 60 seconds
i excpect my code to mention the given user by the string that comes from my method
the error i'm getting
init schedule
Task exception was never retrieved
future: <Task finished coro=<job() done, defined at c:\Users\Nte\Desktop\rainbow army\bot.py:136> exception=TypeError("send() got an unexpected keyword argument 'mentions'",)>
Traceback (most recent call last):
File "c:\Users\Nte\Desktop\rainbow army\bot.py", line 145, in job
await discord.client.send_message("{}".format(usersapps[i]['USER']),mentions=True)
TypeError: send() got an unexpected keyword argument 'mentions'
i was able to mention a user by saving userid into database and then get it with my method and sending message like this
await channel.send("<#!{}>".format(usersapps[i]['USERID']))
Related
I have been working on a python program for a little bit now that can take channel or video urls and convert them into a channel ID.
However my code doesn't seem to work with links that look like "http://youtube.com/#username"
if re.search ("/channel/", channelURL) or re.search ("#", channelURL) or re.search ("/user/", channelURL) or re.search ("/c/", channelURL):
# This code detects if the given URL is a channel. If the check comes back as True then it grabs the data using pytube.
c = Channel(channelURL)
channel_name = c.channel_name
channel_id = c.channel_id
channel_id_link = "http://youtube.com/channel/"+channel_id
print("Channel Name: "+channel_name)
print("Channel ID: "+channel_id)
print("Channel Link: "+channel_id_link)
You can see the full code here. https://github.com/flyinggoatman/YouTube-Link-Extractor/blob/master/QualityYouTube.py
What I expect, the code to be able pull the channel_name, channel_id and also the channel_id_link.
What happens?
The code runs but when I enter in a # youtube channel URL it returns the following
We have logged in as QualityYouTube Bot#2815
Using Discord channel: pending-channels
The bot has now fully booted up and may be used.
Please be advised this bot only supports one Discord server at a time. Future updates will allow for more than one server to be active at a time.
←[30;1m2022-12-30 02:20:50←[0m ←[31mERROR ←[0m ←[35mdiscord.client←[0m Ignoring exception in on_message
←[31mTraceback (most recent call last):
File "C:\Users[redacted]\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "c:\Users[redacted]\test\QualityYouTube.py", line 101, in on_message
c = Channel(channelURL)
File "C:\Users[redacted]\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\contrib\channel.py", line 24, in init
self.channel_uri = extract.channel_name(url)
File "C:\Users[redacted]\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\extract.py", line 185, in channel_name
raise RegexMatchError(
pytube.exceptions.RegexMatchError: channel_name: could not find match for patterns←[0m
I understand I don't think the code will run with the current workings. However, can i somehow take the url and use the regex "#(.*)" to grab the username, and then use pytube to find a video made by that channel? I could then take the video URL and use that to get the information I need instead.
Note I fixed the issue by coding new lines of code inside "C:\Users[redacted]\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\extract.py"
https://github.com/pytube/pytube/pull/1444
I added the following lines of code
- :samp:`https://youtube.com/#{channel_name}/*
r"(?:(#$[%\w\d_-]+)(.*)?)"
to this file inside the github for PyTube
https://github.com/pytube/pytube/blob/master/pytube/extract.py
The changes look like this...
def channel_name(url: str) -> str:
"""Extract the ``channel_name`` or ``channel_id`` from a YouTube url.
This function supports the following patterns:
- :samp:`https://youtube.com/c/{channel_name}/*`
- :samp:`https://youtube.com/channel/{channel_id}/*
- :samp:`https://youtube.com/u/{channel_name}/*`
- :samp:`https://youtube.com/user/{channel_id}/*
- :samp:`https://youtube.com/#{channel_name}/*`
:param str url:
A YouTube url containing a channel name.
:rtype: str
:returns:
YouTube channel name.
"""
patterns = [
r"(?:\/(c)\/([%\d\w_\-]+)(\/.*)?)",
r"(?:\/(channel)\/([%\w\d_\-]+)(\/.*)?)",
r"(?:\/(u)\/([%\d\w_\-]+)(\/.*)?)",
r"(?:\/(user)\/([%\w\d_\-]+)(\/.*)?)",
r"(?:(#[%\w\d_-]+)(.*)?)"
]
for pattern in patterns:
regex = re.compile(pattern)
function_match = regex.search(url)
if function_match:
logger.debug("finished regex search, matched: %s", pattern)
uri_style = function_match.group(1)
uri_identifier = function_match.group(2)
return f'/{uri_style}/{uri_identifier}'
raise RegexMatchError(
caller="channel_name", pattern="patterns"
)
Scenario:
I have a job created in azure batch against a pool. Now, I created another pool and want to point my job to the newly created pool.
I use the azure-batch SDK to write the following piece of code
import azure.batch.batch_service_client as batch
batch_service_client = batch.BatchServiceClient(credentials, batch_url = account_url)
job_id="LinuxTrainingJob"
pool_id="linux-e6a63ad4-9e52-4b9a-8b09-2a0249802981"
pool_info = batch.models.PoolInformation(pool_id=pool_id)
job_patch_param = batch.models.JobPatchParameter(pool_info=pool_info)
batch_service_client.job.patch(job_id, job_patch_param)
This gives me the following error
BatchErrorException Traceback (most recent call last)
<ipython-input-104-ada32b24d6a0> in <module>
2 pool_info = batch.models.PoolInformation(pool_id=pool_id)
3 job_patch_param = batch.models.JobPatchParameter(pool_info=pool_info)
----> 4 batch_service_client.job.patch(job_id, job_patch_param)
~/anaconda3/lib/python3.8/site-packages/azure/batch/operations/job_operations.py in patch(self, job_id, job_patch_parameter, job_patch_options, custom_headers, raw, **operation_config)
452
453 if response.status_code not in [200]:
--> 454 raise models.BatchErrorException(self._deserialize, response)
455
456 if raw:
BatchErrorException: {'additional_properties': {}, 'lang': 'en-US', 'value': 'The specified operation is not valid for the current state of the resource.\nRequestId:46074112-9a99-4569-a078-30a7f4ad2b91\nTime:2020-10-06T17:52:43.6924378Z'}
The credentials are set above and are working properly as I was able to create pool and jobs using the same client.
Environment details
azure-batch==9.0.0
python 3.8.3
Ubuntu 18.04
To assign a Job to another Pool you must call the disableJob API to drain currently running tasks from the Pool. Then you can call updateJob to assign a new poolId to run on. Once it is updated you can then call enableJob to continue the jobs execution.
I'm kinda new to celery as a whole and having the problem with retry case on a for loop:
i have the following task:
#app.task(bind=True, autoretry_for=(CustomException,), retry_kwargs={'max_retries': 10,'countdown': 30})
def call_to_apis(self):
api_list = [api1, api2, api3, api4, api5,...]
for api in api_list:
try:
response = requests.get(api)
if response.status_code == 500:
raise CustomException
except CustomException:
continue
From my understanding celery will retry on my CustomException get raised.
In the case of retry, will it only retry for the the failed api or will it just run the whole process of every api in the api_list again? If so is there anyway for it to only retry the failed api ?
Expected result: only retry the failed api
EDIT:
i have split it into 2 different tasks and 1 request function as follow:
#app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(300.0, call_to_apis.s())
print("setup_periodic_tasks")
def call_api(api):
response = requests.get(api)
if response.status_code == 500:
raise CustomException
elif response.status_code == 404:
raise CustomWrongLinkException
#app.task(default_retry_delay=30, max_retries=10)
def send_fail_api(api):
try:
call_api(api)
except NonceTooLowException:
try:
send_fail_api.retry()
except MaxRetriesExceededError:
print("reached max retry number")
pass
except Exception:
pass
#app.task()
def call_to_apis():
api_list = [api1, api2, api3, api4, api5,...]
for api in api_list:
try:
call_api(api)
except CustomException:
send_fail_api.delay(api)
except CustomWrongLinkException:
print("wrong link")
except Exception:
pass
it worked and the other apis get to complete, with failed api it supposed to call to another task and retry for 10 time each delay 30 seconds.
But i'm getting more than expected retries about 24 times(expected to only retry 10 times) and it also printed out reached max retry number at 10th retry but it still retry until 24 retries
What am i doing wrong ?
The whole task will be retried in case of a known exception (specified in autoretry_for decorator argument), see the documentation. Celery can't know by any means the state of the task when exception is raised, that's what you have to handle. I would suggest splitting the task into individual tasks (one per API) and call them separately, presumably creating some workflow.
I have a serverless function that's trying to send some data to kafka. Sometimes it works and sometimes the connection just drop and the data is lost.
The reason for this is that the library for kafka is not raising the exception but it's adding error logs instead. So i cannot add my piece of code in a try:except.
Here is the error that i am often getting in my logs:
<BrokerConnection node_id=11 host=... port=9092>: Error receiving network data closing socket
Traceback (most recent call last):
File "/var/task/kafka/conn.py", line 745, in _recv
data = self._sock.recv(SOCK_CHUNK_BYTES)
ConnectionResetError: [Errno 104] Connection reset by peer
and the function _recv mentioned above has definition as follows:
I am still looking for a solution but adding the code within try:except doesn't work.
def _recv(self):
responses = []
SOCK_CHUNK_BYTES = 4096
while True:
try:
data = self._sock.recv(SOCK_CHUNK_BYTES)
# We expect socket.recv to raise an exception if there is not
# enough data to read the full bytes_to_read
# but if the socket is disconnected, we will get empty data
# without an exception raised
if not data:
log.error('%s: socket disconnected', self)
self.close(error=Errors.ConnectionError('socket disconnected'))
break
else:
responses.extend(self.receive_bytes(data))
if len(data) < SOCK_CHUNK_BYTES:
break
except SSLWantReadError:
break
except ConnectionError as e:
if six.PY2 and e.errno == errno.EWOULDBLOCK:
break
log.exception('%s: Error receiving network data'
' closing socket', self)
self.close(error=Errors.ConnectionError(e))
break
except BlockingIOError:
if six.PY3:
break
raise
return responses
I expect when the connection is lost, an exception is raised and the application breaks but instead only there are only error logs and the application continues even if the kafka part fails.
I am searching for a method to test if the camera is on for PTG camera.
In PyCapture2 the below code works but the presumed PySpin cam.DeviceConnectionStatus() will not work because the function seems not to be present.
PySpin Camera library version: 1.23.0.27
The Error:
Error: Spinnaker: GenICam::AccessException= Feature not present (reference not valid) : AccessException thrown (file 'IEnumerationT.h', line 341) [-2006]
(False, SpinnakerException("Spinnaker: GenICam::AccessException= Feature not present (reference not valid) : AccessException thrown (file 'IEnumerationT.h', line 341) [-2006]"))
I've tried also PySpin.Camera.DeviceConnectionStatus() but it gives the following error whether prior or after cam.Init():
Traceback (most recent call last):
File "X.py", line 82, in YZ
print (PySpin.Camera.DeviceConnectionStatus())
TypeError: 'property' object is not callable
Working PyCapture2 code:
def cameraOn(self, cam):
# Power on the Camera
cameraPower = 0x610
powerVal = 0x80000000
cam.writeRegister(cameraPower, powerVal)
# Waiting for camera to power up
retries = 10
timeToSleep = 0.1 #seconds
for i in range(retries):
sleep(timeToSleep)
try:
regVal = cam.readRegister(cameraPower)
except PyCapture2.Fc2error: # Camera might not respond to register reads during powerup.
pass
awake = True
if regVal == powerVal:
break
awake = False
if not awake:
print ("Could not wake Camera. Exiting...")
exit()
As it seems there is an IsValid() function available from the CameraBase() class in the PySpin/Spinnaker library. This function returns either the bool True once a connection could be made, communication was successful and the camera is still valid for use or a "False" respectively. However, this function does not turn the camera ON or OFF. Nor does it power from a sleep/wake state.
For unknown reasings the IsValid() function does not report back tracebacks for logging or debug purpose. So keep in mind to implement try/except for certain methods.
try:
... your code ...
except PySpin.SpinnakerException as error:
print('Error: %s' % error)
return False, error