How to get users activity and status using discord api in python

Viewed 63

I want to get someone's activity and status using their id and discord api in python and I have written some code to do that but I'm really confused about the output.

The code:


import requests
token = 'REDACTED'
headers = {'Authorization': token}
base_URL = "https://discord.com/api/v9"
base_URL_attachment = base_URL + "/activities"
f = requests.get(base_URL_attachment, headers=headers).json()
print(f)

The output:

[{'user_id': '455082161050222595', 'application_id': '356876176465199104', 'updated_at': '2022-09-03T17:50:26.457000+00:00', 'duration': 11062}, {'user_id': '455082161050222595', 'application_id': '614454657753939987', 'updated_at': '2022-09-03T19:21:05.516000+00:00', 'duration': 74}, {'user_id': '911382588587921439', 'application_id': '356869127241072640', 'updated_at': '2022-09-03T17:08:27.673000+00:00', 'duration': 20095}, {'user_id': '911382588587921439', 'application_id': '363412841862266880', 'updated_at': '2022-09-03T21:14:12.069000+00:00', 'duration': 5399}, {'user_id': '324477408717701121', 'application_id': '363445589247131668', 'updated_at': '2022-09-02T15:28:11.245000+00:00', 'duration': 1510}, {'user_id': '237598665122775040', 'application_id': '356869127241072640', 'updated_at': '2022-09-02T08:34:11.834000+00:00', 'duration': 2818}, {'user_id': '720754227848151070', 'application_id': '356877880938070016', 'updated_at': '2022-09-03T23:01:28.132000+00:00', 'duration': 0}, {'user_id': '720754227848151070', 'application_id': '357607478105604096', 'updated_at': '2022-09-04T01:38:28.368000+00:00', 'duration': 3600}, {'user_id': '995433254595330090', 'application_id': '432980957394370572', 'updated_at': '2022-09-03T21:34:18.138000+00:00', 'duration': 594}, {'user_id': '821376098104377424', 'application_id': '363445589247131668', 'updated_at': '2022-09-03T22:02:44.429000+00:00', 'duration': 10293}]

Maybe my code isn't even doing what I actually want it to do, Maybe I'm just doing this completely incorrect and the output is something way different, Any help would be appreciated.

I did see another post with something similar and the answer was this link https://discord.com/developers/docs/topics/gateway#activity-object-activity-structure now I don't understand how to like implement this since I tried and it failed supposedly the data is supposed to look like this if I implemented it: { "details": "24H RL Stream for Charity", "state": "Rocket League", "name": "Twitch", "type": 1, "url": "twitch.tv/discord" }

1 Answers

The response is JSON. See: https://www.w3schools.com/js/js_json_intro.asp.

The response looks valid so I'm not sure what help you need. Please review.

[
     {
          'user_id': '455082161050222595', 
          'application_id': '356876176465199104', 
          'updated_at': '2022-09-03T17:50:26.457000+00:00', 
          'duration': 11062},
...]

One of the things that I often do is load JSON data into a Pandas DataFrame which then makes it easy to do further analysis.

import pandas as pd
import json

response_string = {'user_id': '455082161050222595', 'application_id': '356876176465199104', 'updated_at': '2022-09-03T17:50:26.457000+00:00', 'duration': 11062}, {'user_id': '455082161050222595', 'application_id': '614454657753939987', 'updated_at': '2022-09-03T19:21:05.516000+00:00', 'duration': 74}, {'user_id': '911382588587921439', 'application_id': '356869127241072640', 'updated_at': '2022-09-03T17:08:27.673000+00:00', 'duration': 20095}, {'user_id': '911382588587921439', 'application_id': '363412841862266880', 'updated_at': '2022-09-03T21:14:12.069000+00:00', 'duration': 5399}

x = json.dumps(response_string)
y = json.loads(x)
df = pd.DataFrame.from_dict(y)
Related