How to make a "RequestsCookieJar" object for a GET request from an existing cookie as "dictionary" type in Python - python

I've extracted some cookies in my python code in the form of a list of Dictionaries and each dictionary is a cookie like example below:
[
{
"domain": "x.com",
"httpOnly": True,
"name": "Username_COOKIE",
"path": "/",
"sameSite": "Lax",
"secure": False,
"value": "some value",
},
{
"domain": "x.com",
"expiry": 1676724072,
"httpOnly": False,
"name": "ASP.NET_SessionId",
"path": "/",
"sameSite": "Lax",
"secure": False,
"value": "some value",
},
{
"domain": "x.com",
"expiry": 1676810425,
"httpOnly": False,
"name": "_gid",
"path": "/",
"sameSite": "Lax",
"secure": False,
"value": "some value",
}
Now I want to send a new GET request using these cookies. The request.get() method gets cookies in it's parameter as "RequestsCookieJar" object. My question is how can I make a "RequestsCookieJar" object using my dictionaries to pass it to the request.get() method?

If you first alter the structure a bit with
cookieList = [ {'domain': 'x.com', 'httpOnly': True, 'name': 'Username_COOKIE', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
{'domain': 'x.com', 'expiry': 1676724072, 'httpOnly': False, 'name': 'ASP.NET_SessionId', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
{'domain': 'x.com', 'expiry': 1676810425, 'httpOnly': False, 'name': '_gid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'} ]
for ci, c in enumerate(cookieList):
if 'httpOnly' in c:
cookieList[ci]['rest'] = {'HttpOnly': c['httpOnly']}
del cookieList[ci]['httpOnly']
if 'expiry' in c:
cookieList[ci]['expires'] = c['expiry']
del cookieList[ci]['expiry']
so that cookieList looks like
[{'domain': 'x.com',
'name': 'Username_COOKIE',
'path': '/',
'sameSite': 'Lax',
'secure': False,
'value': 'some value',
'rest': {'HttpOnly': True}},
{'domain': 'x.com',
'name': 'ASP.NET_SessionId',
'path': '/',
'sameSite': 'Lax',
'secure': False,
'value': 'some value',
'rest': {'HttpOnly': False},
'expires': 1676724072},
{'domain': 'x.com',
'name': '_gid',
'path': '/',
'sameSite': 'Lax',
'secure': False,
'value': 'some value',
'rest': {'HttpOnly': False},
'expires': 1676810425}]
then you can turn it into a RequestsCookieJar object with
# import requests
cKeys = ['version', 'name', 'value', 'port', 'domain', 'path', 'secure',
'expires', 'discard', 'comment', 'comment_url', 'rest', 'rfc2109']
jar = requests.cookies.RequestsCookieJar()
for c in cookieList: jar.set(**{k:v for k,v in c.items() if k in cKeys})
You can now use jar like requests.get(url, cookies=jar), or view it with print(repr(jar)) which should output something like
<RequestsCookieJar[
Cookie(version=0, name='ASP.NET_SessionId', value='some value', port=None, port_specified=False, domain='x.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=1676724072, discard=True, comment=None, comment_url=None, rest={'HttpOnly': False}, rfc2109=False),
Cookie(version=0, name='Username_COOKIE', value='some value', port=None, port_specified=False, domain='x.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': True}, rfc2109=False),
Cookie(version=0, name='_gid', value='some value', port=None, port_specified=False, domain='x.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=1676810425, discard=True, comment=None, comment_url=None, rest={'HttpOnly': False}, rfc2109=False)
]>
You might also be able to just get away with
cookieList = [ {'domain': 'x.com', 'httpOnly': True, 'name': 'Username_COOKIE', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
{'domain': 'x.com', 'expiry': 1676724072, 'httpOnly': False, 'name': 'ASP.NET_SessionId', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
{'domain': 'x.com', 'expiry': 1676810425, 'httpOnly': False, 'name': '_gid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'} ]
cookiesDict = {c['name']:c['value'] for c in cookieList if 'name' in c and 'value' in c}
resp = requests.get(url, cookies=cookiesDict)

Related

How load nested Response JSON into a dataframe without saving in a file?

I'm attempting to load a pandas a dataframe with a heavily nesting JSON. This JSON is the result of an API code in Python. Here is the structure of the JSON file:
{
'results': [{
'accountId': 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
'id': '6ed909bd16',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'NewSeptemberFile20220908',
'description': None,
'created': '2022-09-12T22:04:55.799+00:00',
'lastModified': '2022-09-12T22:06:10.838+00:00',
'lastIndexed': '2022-09-12T22:05:04.551+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '6ed909bd16',
'thumbnailId': '5f04af4d-e382-4387-9573-6d9e4bad3b68',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXX',
'id': '34344818e8',
'partition': None,
'externalId': None,
'metadata': None,
'name': '3September',
'description': None,
'created': '2022-09-09T17:55:59.696+00:00',
'lastModified': '2022-09-09T17:57:51.057+00:00',
'lastIndexed': '2022-09-09T17:56:04.544+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '34344818e8',
'thumbnailId': 'baae7ed1-a791-4481-853c-1707b40b5e77',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'id': '82da4b60ef',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'film10',
'description': None,
'created': '2022-08-22T14:24:08.442+00:00',
'lastModified': '2022-09-08T23:13:16.416+00:00',
'lastIndexed': '2022-08-22T14:24:12.605+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '82da4b60ef',
'thumbnailId': '5a5f6a71-0302-46a6-93c8-beb918c00b14',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXX',
'id': '7ea0c5e34a',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'davide_quatela--people_in_frankfurt',
'description': None,
'created': '2022-09-07T21:31:52.818+00:00',
'lastModified': '2022-09-08T22:52:52.833+00:00',
'lastIndexed': '2022-09-07T21:31:57.328+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 131,
'thumbnailVideoId': '7ea0c5e34a',
'thumbnailId': '3aba8f42-a3a7-4d77-92b0-8cabcc275a3b',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-US',
'sourceLanguages': ['en-US'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXXX',
'id': '7c45ae7ffe',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'Untitled project',
'description': None,
'created': '2022-08-17T17:36:23.72+00:00',
'lastModified': '2022-08-17T17:36:49.95+00:00',
'lastIndexed': '2022-08-17T17:36:49.95+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': False,
'hasSourceVideoFile': False,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '',
'durationInSeconds': 0,
'thumbnailVideoId': None,
'thumbnailId': '00000000-0000-0000-0000-000000000000',
'searchMatches': [],
'indexingPreset': None,
'streamingPreset': 'Default',
'sourceLanguage': 'en-US',
'sourceLanguages': ['en-US'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}
],
'nextPage': {
'pageSize': 25,
'skip': 0,
'done': True
}
}
And this is my code in Python 3.x:
url = "https://api.videoindexer.ai/" + Location + "/Accounts/" + AzVIAccountID + "/Videos?pageSize=25&skip=0&accessToken=" + iAccessToken
response = requests.get(url,headers=hdr )
### Response: 200 OK
print("If Response=200 Script run is OK : " , response.status_code)
I want the result to be like this:
Question: Is there a possibility to fetch 'name', 'id', and 'create' of all videos which are showing in the JSON result (Response API) into a dataframe directly?
If data contains the data from the question you can do:
df = pd.DataFrame(data["results"])
columns_i_want = ["id", "name", "created"]
df = df[columns_i_want]
print(df)
Prints:
id name created
0 6ed909bd16 NewSeptemberFile20220908 2022-09-12T22:04:55.799+00:00
1 34344818e8 3September 2022-09-09T17:55:59.696+00:00
2 82da4b60ef film10 2022-08-22T14:24:08.442+00:00
3 7ea0c5e34a davide_quatela--people_in_frankfurt 2022-09-07T21:31:52.818+00:00
4 7c45ae7ffe Untitled project 2022-08-17T17:36:23.72+00:00
EDIT:
url = (
"https://api.videoindexer.ai/"
+ Location
+ "/Accounts/"
+ AzVIAccountID
+ "/Videos?pageSize=25&skip=0&accessToken="
+ iAccessToken
)
response = requests.get(url, headers=hdr)
data = response.json()
# make sure the correct response is sent:
print(data)
# if above command prints correct data, you can do next:
df = pd.DataFrame(data["results"])
columns_i_want = ["id", "name", "created"]
df = df[columns_i_want]
print(df)

How to search for the video id by name in nested JSON Response?

This is my JSON response API, I want to search for the video id by name. I mean Get VideoID using Video Name.
For example: I want to input 'film10' and Python returns '82da4b60ef' as a result. I am using API Response JSON; this is the JSON:
{
'results': [{
'accountId': 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
'id': '6ed909bd16',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'NewSeptemberFile20220908',
'description': None,
'created': '2022-09-12T22:04:55.799+00:00',
'lastModified': '2022-09-12T22:06:10.838+00:00',
'lastIndexed': '2022-09-12T22:05:04.551+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '6ed909bd16',
'thumbnailId': '5f04af4d-e382-4387-9573-6d9e4bad3b68',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXX',
'id': '34344818e8',
'partition': None,
'externalId': None,
'metadata': None,
'name': '3September',
'description': None,
'created': '2022-09-09T17:55:59.696+00:00',
'lastModified': '2022-09-09T17:57:51.057+00:00',
'lastIndexed': '2022-09-09T17:56:04.544+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '34344818e8',
'thumbnailId': 'baae7ed1-a791-4481-853c-1707b40b5e77',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'id': '82da4b60ef',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'film10',
'description': None,
'created': '2022-08-22T14:24:08.442+00:00',
'lastModified': '2022-09-08T23:13:16.416+00:00',
'lastIndexed': '2022-08-22T14:24:12.605+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '82da4b60ef',
'thumbnailId': '5a5f6a71-0302-46a6-93c8-beb918c00b14',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXX',
'id': '7ea0c5e34a',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'davide_quatela--people_in_frankfurt',
'description': None,
'created': '2022-09-07T21:31:52.818+00:00',
'lastModified': '2022-09-08T22:52:52.833+00:00',
'lastIndexed': '2022-09-07T21:31:57.328+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 131,
'thumbnailVideoId': '7ea0c5e34a',
'thumbnailId': '3aba8f42-a3a7-4d77-92b0-8cabcc275a3b',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-US',
'sourceLanguages': ['en-US'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXXX',
'id': '7c45ae7ffe',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'Untitled project',
'description': None,
'created': '2022-08-17T17:36:23.72+00:00',
'lastModified': '2022-08-17T17:36:49.95+00:00',
'lastIndexed': '2022-08-17T17:36:49.95+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': False,
'hasSourceVideoFile': False,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '',
'durationInSeconds': 0,
'thumbnailVideoId': None,
'thumbnailId': '00000000-0000-0000-0000-000000000000',
'searchMatches': [],
'indexingPreset': None,
'streamingPreset': 'Default',
'sourceLanguage': 'en-US',
'sourceLanguages': ['en-US'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}
],
'nextPage': {
'pageSize': 25,
'skip': 0,
'done': True
}
}
This is my code, I get the API data by the requests get method and it returns '200' that it does mean the code is working properly.
url = "https://api.video.ai/trial/Accounts//Videos/7ea0c5e34a/Index?language=af-ZA&reTranslate=false&includeStreamingUrls=true&includeSummarizedInsights=true&accessToken="
response = requests.get(url,headers=hdr )
### Response: 200 OK
print("If Response=200 Script run is OK : " , response.status_code)
So the url does not open on my end. cannot fetch the json.
using the json provided by you -
for item in json_data['results']:
if item['name'] == 'film10':
print(item['id'])
gives
82da4b60ef
You could try the following:
for result in response['results']:
if result['name'] == ('YOUR FILM NAME INPUT'):
return result['id']
As you can see the response has a list with the results called "results", so this code runs through it and check if the name equals the name you want to check.

Accessing specific data from JSON data (Python)

I am new to JSON in Python (forgive me if I word something incorrectly) and I am trying to parse JSON information I get from an API. I get the API information successfully, but when I attempt to extract the information that I need (specifically the 'name' value in the variable info). I can't seem to find anything on how to access it. The code is below:
# Importing JSON handling library for parsing GET request using requests library
import json
import requests
import ast
import collections
# API Endpoint
url = "https://fnbr.co/api/shop"
# API key
key = "" # key is usually here
# Defining Parameters
headers = {"x-api-key":key}
# Sending GET request
req = requests.get(url = url, headers = headers)
info = req.json()
info = info['data']['featured']
print(info)
And the output:
[{'id': '5e8743597576ec53deb18eeb', 'name': 'Deadpool Gear Bundle', 'price': '2,000', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/bundle/5e8743597576ec53deb18eeb/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'bundle', 'slug': 'deadpool-gear-bundle', 'readableType': 'Bundle', 'description': False, 'bundleSet': '5e87444f7576ec029cb18eef', 'bannerText': False, 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8fbb5d3cdac627afdebeb5', 'name': 'Deadpool Mashups', 'price': '2,000', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/bundle/5e8fbb5d3cdac627afdebeb5/icon.png', 'png': False, 'gallery': False, 'featured': False}, 'rarity': 'marvel', 'type': 'bundle', 'slug': 'deadpool-mashups', 'readableType': 'Bundle', 'description': 'Outfit Bundle.', 'bundleSet': '5e8fba7a3cdac6863bdebeaf', 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8742117576ec88f6b18ee0', 'name': 'Chimichanga!', 'price': '300', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/emote/5e8742117576ec88f6b18ee0/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'emote', 'slug': 'chimichanga', 'readableType': 'Emote', 'description': 'Too hot! Too hot!', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8741f37576ecfc79b18edd', 'name': "Scootin'", 'price': '500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/emote/5e8741f37576ecfc79b18edd/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'emote', 'slug': 'scootin', 'readableType': 'Emote', 'description': 'Dangerously dainty.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e87422b7576ec21f5b18ee3', 'name': 'Dragacorn', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/glider/5e87422b7576ec21f5b18ee3/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'glider', 'slug': 'dragacorn', 'readableType': 'Glider', 'description': 'Bask in its mutant rainbow glory.', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8fbafd3cdac6ed02debeb3', 'name': 'Cuddlepool', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5e8fbafd3cdac6ed02debeb3/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5e8fbafd3cdac6ed02debeb3/featured.png', 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'outfit', 'slug': 'cuddlepool', 'readableType': 'Outfit', 'description': 'Ferociously furry.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8fb9f33cdac69b0fdebead', 'name': 'Ravenpool', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5e8fb9f33cdac69b0fdebead/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5e8fb9f33cdac69b0fdebead/featured.png', 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'outfit', 'slug': 'ravenpool', 'readableType': 'Outfit', 'description': 'Maximum darkness.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8741d67576ecad85b18eda', 'name': 'Meaty Mallets', 'price': '800', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/pickaxe/5e8741d67576ecad85b18eda/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'pickaxe', 'slug': 'meaty-mallets', 'readableType': 'Pickaxe', 'description': "Give 'em a thwack.", 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5db7ff1ac02d0515d20b054c', 'name': 'Riley', 'price': '1,200', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5db7ff1ac02d0515d20b054c/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5db7ff1ac02d0515d20b054c/featured.png', 'resizeAvailable': True}, 'rarity': 'rare', 'type': 'outfit', 'slug': 'riley', 'readableType': 'Outfit', 'description': 'The city is her playground.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 5, 'firstSeen': '2019-11-27T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2019-11-27T00:00:00.000Z', '2020-03-09T00:00:00.000Z', '2020-01-01T00:00:00.000Z', '2020-02-06T00:00:00.575Z']}}, {'id': '5db804ebc02d05322e0b059d', 'name': 'Wild X', 'price': '300', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/wrap/5db804ebc02d05322e0b059d/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/wrap/5db804ebc02d05322e0b059d/featured.png'}, 'rarity': 'uncommon', 'type': 'wrap', 'slug': 'wild-x', 'readableType': 'Wrap', 'description': 'Show your style.', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 5, 'firstSeen': '2019-11-27T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2019-11-27T00:00:00.000Z', '2020-03-09T00:00:00.000Z', '2020-01-01T00:00:00.000Z', '2020-02-06T00:00:00.575Z']}}]
I need specifically all of the 'name' value attributes, such as 'Deadpool Gear Bundle', but I'm unsure how. If I try accessing the location info['data']['featured']['name'] location it gives me an error. Also, there could be a different amount of 'name' values every day, so I need a method that returns the attributes of all of them.
So, there's 2 types of objects you need to be aware of.
dict: when you load JSON it's stored as a dictionary object. Dict objects let you access the values through keys, like you're doing here -> info['data']['featured']
list: print(info) is showing that 'info' is a list of things. You can tell by the square brackets [], or just by calling print(type(info)). A list is ordered so to access the name for the first object in your list you would say info[0]['name'].
To get all the objects in your list in a row you can use a for loop:
for x in info:
print(x['name'])
You can name 'x' whatever you want. That loop is just saying " for every object in this list, perform the following action"

Can't get Twitter email when authenticating Twitter

I'm using Oauth1Session for authenticating Twitter and tried to get user's email info. Actually, the application have been successfully fetched user info but it's not included email info. How can I get email info with accouunt/verify_credentials.
My code:
access_token_url = 'https://api.twitter.com/oauth/access_token'
protected_url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
oauth_token = request.args.get('oauth_token')
oauth_verifier = request.args.get('oauth_verifier')
twitter = OAuth1Session(
client_id,
client_secret,
oauth_token,
oauth_verifier
)
response = twitter.post(
access_token_url,
params={'oauth_verifier': oauth_verifier}
)
access_token = dict(parse_qsl(response.content.decode("utf-8")))
oauth = OAuth1Session(
client_id,
client_secret=client_secret,
resource_owner_key=access_token["oauth_token"],
resource_owner_secret=access_token["oauth_token_secret"]
)
params = {"include_email": True}
response = oauth.get(protected_url, params = params)
user_info = response.json()
user_info
{'id': xxxxxxxxx, 'id_str': 'xxxxxxxx', 'name': 'xxxxxxxx', 'screen_name': 'xxxxxxxxxx', 'location': 'xxxxxxxxxxxxx', 'description': 'xxxxxxxxxxxx', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': False, 'followers_count': xxx, 'friends_count': xx, 'listed_count': xx, 'created_at': 'Thu Aug 27 14:16:29 +0000 2015', 'favourites_count': xxxx, 'utc_offset': None, 'time_zone': None, 'geo_enabled': True, 'verified': False, 'statuses_count': xxxxx, 'lang': None, 'status': {'created_at': 'Sun Dec 22 10:11:00 +0000 2019', 'id': xxxxxxxxxxx, 'id_str': 'xxxxxxxxxxx', 'text': 'xxxxxxxxxxxxxxx', 'truncated': False, 'entities': {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': []}, 'source': 'Twitter for iPhone', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 2, 'favorited': False, 'retweeted': False, 'lang': 'ja'}, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': '000000', 'profile_background_image_url': 'xxxxxxxxxxxxxxxxx', 'profile_background_image_url_https': 'xxxxxxxxxxxxxxxx', 'profile_background_tile': False, 'profile_image_url': 'xxxxxxxxxxxxxxxx', 'profile_image_url_https': 'xxxxxxxxxxxxxxxxxxx', 'profile_banner_url': 'xxxxxxxxxxxxxxxxxx', 'profile_link_color': 'DD2E44', 'profile_sidebar_border_color': '000000', 'profile_sidebar_fill_color': '000000', 'profile_text_color': '000000', 'profile_use_background_image': False, 'has_extended_profile': False, 'default_profile': False, 'default_profile_image': False, 'can_media_tag': False, 'followed_by': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none', 'suspended': False, 'needs_phone_verification': False}
But not included email...
I have updated paramas:
params = {"include_email": "true"}

Convert JSON string to a list in order to enumerate

End goal: I want to get 'userid', 'username', and 'email' from a JSON string that I pulled from Slack. When I try converting it to a list, it only gives me the first three keys, it won't pull the nested data.
Here's my code:
from slackclient._client import SlackClient
import json
def main():
token = "XXXXX"
sc = SlackClient(token)
users = get_users(sc)
print(users)
print(list(users))
def get_users(sc):
print("Get Users")
print(80 * "=")
#call the users.list api call and get list of users
users = (sc.api_call("users.list"))
users = json.dumps(users)
users = json.loads(str(users))
return users
main()
My problem is "print(users)" prints me out an entire JSON string and "print(list(users))" only prints the first un-nested keys. Here's an example of what my output is.
"print(list(users))" gives me the following:
['members', 'cache_ts', 'ok']
"print(users)" gives me the following, but I can't pull anything specific from it as a string.
{'members': [{'real_name': 'XXXXXX', 'is_ultra_restricted': False, 'name': 'XXXXX', 'is_primary_owner': False, 'id': 'XXXXXX', 'color': '4bbe2e', 'status': None, 'profile': {'image_32': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-32.png', 'real_name': 'XXXXXX', 'first_name': 'XXXXXX', 'avatar_hash': 'g140a3a97c34', 'email': 'XXXXXXX#XXXXX.COM', 'image_192': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0006-192.png', 'image_24': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-24.png', 'real_name_normalized': 'XXXXXX', 'image_512': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0006-512.png', 'last_name': 'XXXXXX', 'image_48': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-48.png', 'image_72': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-72.png'}, 'deleted': False, 'tz': 'America/Los_Angeles', 'tz_offset': -28800, 'is_restricted': False, 'is_admin': False, 'team_id': 'T3854BB3K', 'has_2fa': False, 'is_bot': False, 'tz_label': 'Pacific Standard Time', 'is_owner': False}, {'real_name': 'XXXXXXX, 'is_ultra_restricted': False, 'name': 'XXXXXXX', 'is_primary_owner': True, 'id': 'XXXXXXX', 'color': '9f69e7', 'status': None, 'profile': {'image_32': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0001-32.png', 'real_name': 'XXXXXX', 'first_name': 'XXXXX', 'avatar_hash': 'g98e886a882b', 'email': 'XXXXXXX#gmail.com', 'image_192': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0001-192.png', 'image_24': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0001-24.png', 'real_name_normalized': 'XXXXXXX', 'image_512': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0001-512.png', 'last_name': 'XXXXXX', 'image_48': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F0180%2Fimg%2Favatars%2Fava_0001-48.png', 'image_72': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F3654%2Fimg%2Favatars%2Fava_0001-72.png'}, 'deleted': False, 'tz': 'America/Chicago', 'tz_offset': -21600, 'is_restricted': False, 'is_admin': True, 'team_id': 'T3854BB3K', 'has_2fa': False, 'is_bot': False, 'tz_label': 'Central Standard Time', 'is_owner': True}, {'real_name': 'Talla', 'is_ultra_restricted': False, 'name': 'talla', 'is_primary_owner': False, 'id': 'XXXXXX', 'color': 'e7392d', 'status': None, 'profile': {'api_app_id': 'A0P3RDP0R', 'image_32': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_32.png', 'image_original': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_original.png', 'real_name': 'Talla', 'always_active': False, 'first_name': 'Talla', 'bot_id': 'B38SQ12EN', 'avatar_hash': '0864de31dc83', 'image_1024': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_512.png', 'image_192': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_192.png', 'image_24': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_24.png', 'real_name_normalized': 'Talla', 'image_512': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_512.png', 'image_48': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_48.png', 'image_72': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_72.png'}, 'deleted': False, 'tz': None, 'tz_offset': -28800, 'is_restricted': False, 'is_admin': False, 'team_id': 'T3854BB3K', 'is_bot': True, 'tz_label': 'Pacific Standard Time', 'is_owner': False}, {'real_name': 'slackbot', 'is_ultra_restricted': False, 'name': 'slackbot', 'is_primary_owner': False, 'id': 'USLACKBOT', 'color': '757575', 'status': None, 'profile': {'image_32': 'https://a.slack-edge.com/2fac/plugins/slackbot/assets/service_32.png', 'real_name': 'slackbot', 'first_name': 'slackbot', 'fields': None, 'last_name': '', 'image_192': 'https://a.slack-edge.com/66f9/img/slackbot_192.png', 'image_24': 'https://a.slack-edge.com/0180/img/slackbot_24.png', 'real_name_normalized': 'slackbot', 'image_512': 'https://a.slack-edge.com/1801/img/slackbot_512.png', 'avatar_hash': 'sv1444671949', 'image_48': 'https://a.slack-edge.com/2fac/plugins/slackbot/assets/service_48.png', 'image_72': 'https://a.slack-edge.com/0180/img/slackbot_72.png'}, 'deleted': False, 'tz': None, 'tz_offset': -28800, 'is_restricted': False, 'is_admin': False, 'team_id': 'T3854BB3K', 'is_bot': False, 'tz_label': 'Pacific Standard Time', 'is_owner': False}], 'ok': True, 'cache_ts': 1480600947}
json.loads() returns a dictionary, and by default iterating a dictionary is actually iterating over the keys.
try iterating over users.items() to get (key,value) pairs.

Categories