get_user failing
Closed this issue · 11 comments
I had this code working for some time to get the avatar of the repl owner (for which my repl is running in)
from repltalk import Client
import asyncio
async def get_repl_avatar(user_name):
user = await Client().get_user(user_name)
return user.avatar
def main():
# replit avatar
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
global repl_avatar
repl_avatar = loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
Essentially await Client().get_user(user_name)
never completes.
Today it stopped working. Any idea why or do know a better way to get the avatar url?
Error message? Or is stuck indefintely?
Error message? Or is stuck indefintely?
It doesn't error out, it just doesn't complete.
I only get any type of message if I interrupt the program.
^CTraceback (most recent call last):
File "discord_bot.py", line 185, in <module>
main()
File "discord_bot.py", line 35, in main
repl_avatar = loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
self.run_forever()
File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
self._run_once()
File "/usr/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once
event_list = self._selector.select(timeout)
File "/usr/lib/python3.8/selectors.py", line 468, in select
fd_event_list = self._selector.poll(timeout, max_ev)
KeyboardInterrupt
Task was destroyed but it is pending!
task: <Task pending name='Task-1' coro=<SlashCommand.sync_all_commands() running at /opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/client.py:359>>
/usr/lib/python3.8/asyncio/base_events.py:641: RuntimeWarning: coroutine 'SlashCommand.sync_all_commands' was never awaited
I did put a print statement before and after the user = await ...
line and it never prints the second one.
Sorry for the late reply.
I'm not sure what is occurring. I tested your code and it seems to work fine, so there's likely something interfering.
It might be that repl.it was down (graphQL specifically), so it got stuck.
If you have a repl, please link it.
global repl_avatar
repl_avatar = loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
This is wrong. Replace it with this:
return loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
Then somewhere else you could do:
repl_avatar = main()
Check if this works.
global repl_avatar repl_avatar = loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))This is wrong. Replace it with this:
return loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))Then somewhere else you could do:
repl_avatar = main()Check if this works.
I made the suggested changes. I am not sure if this fixed the issue, but it is working again now. To be honest I am not sure why it broke in the first place as it was working fine for a month or longer. Thank you!
This is erroring out again.
Script 1 error:
Traceback (most recent call last):
File "discord_bot.py", line 190, in <module>
main()
File "discord_bot.py", line 40, in main
repl_avatar = loop_repl_avatar()
File "discord_bot.py", line 25, in loop_repl_avatar
return loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "discord_bot.py", line 18, in get_repl_avatar
user = await Client().get_user(user_name)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/repltalk/__init__.py", line 1264, in get_user
u = User(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/repltalk/__init__.py", line 774, in __init__
self.id = user['id']
TypeError: list indices must be integers or slices, not str
Script 2 error:
Traceback (most recent call last):
File "reddit_bot.py", line 284, in <module>
main()
File "reddit_bot.py", line 130, in main
repl_avatar = loop_repl_avatar()
File "reddit_bot.py", line 114, in loop_repl_avatar
return loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "reddit_bot.py", line 107, in get_repl_avatar
user = await Client().get_user(user_name)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/repltalk/__init__.py", line 1264, in get_user
u = User(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/repltalk/__init__.py", line 774, in __init__
self.id = user['id']
TypeError: list indices must be integers or slices, not str
I'll re-open this, but probably going to try and use requests
+ re
to just get the avatar from the user's profile page.
I would be happy to provide any other details.
Doesn't seem like you passed in something wrong. Did you make any changes?
I didn't change anything since the last time I fixed this.
This is the general part of my code responsible for getting the avatar.
#imports here
async def get_repl_avatar(user_name):
user = await Client().get_user(user_name)
return user.avatar
def loop_repl_avatar():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
def main():
# replit avatar
global repl_avatar
repl_avatar = loop_repl_avatar()
# more stuff here
Revisiting the original code, what I have above seems equivalent to the original.
original issue version:
from repltalk import Client
import asyncio
async def get_repl_avatar(user_name):
user = await Client().get_user(user_name)
return user.avatar
def main():
# replit avatar
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
global repl_avatar
repl_avatar = loop.run_until_complete(get_repl_avatar(os.environ['REPL_OWNER']))
To be honest, I am not well versed with asyncio. I probably did something stupid. Not sure if you'd be interested in looking at the whole script, but here is where it's located. https://github.com/RetroArcher/RetroArcher.reddit-bot/blob/ce56af7e76702c9bbaf3748aef2faa770cd2a6a0/reddit_bot.py#L130
This is how I'm getting the avatar image at this point. Pretty straight forward and should be stable unless the profile page undergoes a major redesign.
import re
import requests
def get_repl_avatar(user_name):
url = f'https://replit.com/@{user_name}'
repl_page = requests.get(url)
image_link = re.search(r'property=\"og:image\" content=\"(https://storage\.googleapis\.com/replit/images/[a-z_0-9]*\.png)\"', repl_page.text).group(1)
return image_link
user_name
can be os.environ['REPL_OWNER']
when running from a repl.
I've been doing some testing. There seems to be an internal server error.
[{'message': 'Internal server error', 'extensions': {'code': 'INTERNAL_SERVER_ERROR'}}]
It's causing an issue with your code.
For now, you might have to create your own method of accessing this data. Another possibility is to contact Replit support about this.
@ReenigneArcher this should be fixed when the changes in pr #30 get published to pypi