'Fix function logic to return new messages
My code:
def get_messages(token:str, channel_id:int, last_message_id:int) -> list:
"""Returns a list of dicts containing new messages data"""
headers = {
'authorization': token,
}
dict_with_needed_message = {}
messages_amount = 50
while len(dict_with_needed_message) == 0:
r = requests.get(f"https://website/api/channels/{channel_id}/msgs?limit={messages_amount}", headers=headers)
list_of_dicts = json.loads(r.text)
dict_with_needed_message = list(filter(lambda find_dict: find_dict['id'] == str(last_message_id), list_of_dicts))[0]
if dict_with_needed_message:
dict_index = list_of_dicts.index(dict_with_needed_message)
if dict_index != 0:
new_messages_list = list_of_dicts[dict_index-1::-1]
else:
new_messages_list = []
else:
messages_amount += 50
return new_messages_list
It already looks non-Pythonic, and the condition for finding dict_with_needed_message does not work correctly (the resulting dictionary may have a nested 'id' key, needed to check only the value of the first key of the entire dictionary).
What should the function do:
- Input data remains the same
- Make a request with the default number of messages = 50
- In the received data, find the dictionary number, the value of the first key in which = str(last_message_id)
- If it's not in the list, then the number of messages is increased by 50 and a second request is made. So until the number of the desired message is found
- By the received number, make a slice of the list, getting a list of messages coming after the found one and reflect it in reverse order
- Return this resulting list
Help correct my code or suggest a better option. Thank you.
Example of list_of_data structure:
[
{
'id': '961627935247331329',
'type': 19,
'content': 'Hello world!',
'channel_id': '737700125492772863',
'author': {
'id': '123456789123456789',
'username': 'TestUser',
'avatar': 'd0bd70431e24610fd067109f28654a5b',
'discriminator': '2806',
'public_flags': 64
},
'attachments': [
],
'embeds': [
],
'mentions': [
],
'mention_roles': [
],
'pinned': False,
'mention_everyone': False,
'tts': False,
'timestamp': '2022-04-07T14:06:05.946000+00:00',
'edited_timestamp': None,
'flags': 0,
'components': [
],
'message_reference': {
'channel_id': '737700125492772863',
'guild_id': '724241782833152050',
'message_id': '961550841163157544'
},
'referenced_message': {
'id': '961550841163157544',
'type': 0,
'content': 'test message by testuser',
'channel_id': '737700125492772863',
'author': {
'id': '123456789123456789',
'username': 'TestUser',
'avatar': 'd0bd70431e24610fd067109f28654a5b',
'discriminator': '2806',
'public_flags': 64
},
'attachments': [
],
'embeds': [
],
'mentions': [
],
'mention_roles': [
],
'pinned': False,
'mention_everyone': False,
'tts': False,
'timestamp': '2022-04-07T08:59:45.284000+00:00',
'edited_timestamp': None,
'flags': 0,
'components': [
]
}
},
{
'id': '961550841163157544',
'type': 0,
'content': 'new message from testuser 123',
'channel_id': '737700125492772863',
'author': {
'id': '123456789123456789',
'username': 'TestUser',
'avatar': 'd0bd70431e24610fd067109f28654a5b',
'discriminator': '2806',
'public_flags': 64
},
'attachments': [
],
'embeds': [
],
'mentions': [
],
'mention_roles': [
],
'pinned': False,
'mention_everyone': False,
'tts': False,
'timestamp': '2022-04-07T08:59:45.284000+00:00',
'edited_timestamp': None,
'flags': 0,
'components': [
]
},
{
'id': '961315800499761232',
'type': 0,
'content': 'abcdefgh',
'channel_id': '737700125492772863',
'author': {
'id': '123456789123456789',
'username': 'TestUser',
'avatar': 'd0bd70431e24610fd067109f28654a5b',
'discriminator': '2806',
'public_flags': 64
},
'attachments': [
],
'embeds': [
],
'mentions': [
],
'mention_roles': [
],
'pinned': False,
'mention_everyone': False,
'tts': False,
'timestamp': '2022-04-06T17:25:47.223000+00:00',
'edited_timestamp': None,
'flags': 0,
'components': [
]
},
{
'id': '961299163352559718',
'type': 0,
'content': 'foo bar qwerty 123',
'channel_id': '737700125492772863',
'author': {
'id': '123456789123456789',
'username': 'TestUser',
'avatar': 'd0bd70431e24610fd067109f28654a5b',
'discriminator': '2806',
'public_flags': 64
},
'attachments': [
],
'embeds': [
],
'mentions': [
],
'mention_roles': [
],
'pinned': False,
'mention_everyone': False,
'tts': False,
'timestamp': '2022-04-06T16:19:40.618000+00:00',
'edited_timestamp': None,
'flags': 0,
'components': [
]
}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
