So I'm trying to parse the following response (JSON Response), here's a sample:
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/fywkWrox-IkW0v2IWY27RMiWvvA\"",
"nextPageToken": "CBQQAA",
"regionCode": "IQ",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 20
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/j0uEstXCXOhrDqDegEBmEeHqsBM\"",
"id": {
"kind": "youtube#video",
"videoId": "YQHsXMglC9A"
},
"snippet": {
"publishedAt": "2015-10-23T06:54:18.000Z",
"channelId": "UComP_epzeKzvBX156r6pm1Q",
"title": "Adele - Hello",
"description": "'Hello' is taken from the new album, 25, out November 20. http://adele.com Available now from iTunes http://smarturl.it/itunes25 Available now from Amazon ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/YQHsXMglC9A/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/YQHsXMglC9A/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/YQHsXMglC9A/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "AdeleVEVO",
"liveBroadcastContent": "none"
}
}
And this is my parsing function:
def parse(self):
items = self['items']
i = 0
for item in items:
Data = {str(i): {
"id": item['id']['videoId'],
"title": item['snippet']['title'],
"description": item['snippet']['description'],
"thumbnail": item['snippet']['thumbnails']['medium']['url'],
"publishedAt": item['snippet']['publishedAt'],
"FullURL": "https://www.youtube.com/watch?v=" + item['id']['videoId']
}}
i = i +1
return Data
The main problem is that the dictionary is only inserting the last bit of the response, for example, I'm fetching 10 results, and it's only returning the last response. What's the problem?
Just take the definition of Data out of the for loop, initialise it as an empty dictionary and then add key/value pairs to it on each iteration. Currently, you keep redefining the entire dictionary on each loop, containing a single entry. You then end up returning the final version.
def parse(self):
items = self['items']
Data = {} # Initialise it here
for i, item in enumerate(items): # Now you don't need to increment i
# Insert your key/value pair
Data[str(i)] = {
"id": item['id']['videoId'],
"title": item['snippet']['title'],
"description": item['snippet']['description'],
"thumbnail": item['snippet']['thumbnails']['medium']['url'],
"publishedAt": item['snippet']['publishedAt'],
"FullURL": "https://www.youtube.com/watch?v=" + item['id']['videoId']
}
return Data
Related
I'm using the YouTube v3 API to fetch the user's videos uploaded on the channel.
I need the video statistics (mainly the view count) for specific time-frames in a video. For example: from 10 second to 15 second in the video. Or maybe at the 10th second.
How can this be achieved? I have gone through the API docs but don't see any parameter or section that can provide that data.
Code snippet to fetch the data of completed livestreams / liveBroadcasts:
request = youtube.liveBroadcasts().list(
part="snippet, status",
broadcastStatus="completed",
broadcastType="all"
)
response = request.execute()
Then for each video from the list data returned, I'm running the following API to fetch the video related data:
request = youtube.videos().list(
part="snippet, contentDetails, statistics, liveStreamingDetails, status",
id=videoID
)
response = request.execute()
return response
Sample Response
{
"kind": "youtube#videoListResponse",
"etag": "JI93kvK9Gsz3h2R_OvkmgPkUrqs",
"items": [
{
"kind": "youtube#video",
"etag": "NfG5gw0fBnjykGf8db9FiaBXw3M",
"id": "FNS_JCM-H3U",
"snippet": {
"publishedAt": "2022-02-27T19:20:15Z",
"channelId": "UCVxNxW5_GhdS_o8m4PyHsmg",
"title": "Time Check Test",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/FNS_JCM-H3U/default_live.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/FNS_JCM-H3U/mqdefault_live.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/FNS_JCM-H3U/hqdefault_live.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/FNS_JCM-H3U/sddefault_live.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/FNS_JCM-H3U/maxresdefault_live.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Streampala",
"categoryId": "20",
"liveBroadcastContent": "none",
"localized": {
"title": "Time Check Test",
"description": ""
},
"defaultAudioLanguage": "en-US"
},
"contentDetails": {
"duration": "PT1M20S",
"dimension": "2d",
"definition": "sd",
"caption": "false",
"licensedContent": False,
"contentRating": {},
"projection": "rectangular",
"hasCustomThumbnail": False
},
"status": {
"uploadStatus": "uploaded",
"privacyStatus": "public",
"license": "youtube",
"embeddable": False,
"publicStatsViewable": True,
"madeForKids": False,
"selfDeclaredMadeForKids": False
},
"statistics": {
"viewCount": "4",
"likeCount": "0",
"dislikeCount": "0",
"favoriteCount": "0",
"commentCount": "0"
},
"liveStreamingDetails": {
"actualStartTime": "2022-02-27T19:20:33Z",
"actualEndTime": "2022-02-27T19:21:53Z",
"scheduledStartTime": "2022-02-27T19:20:12Z"
}
}
],
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
}
}
I'm pretty new to Python so forgive me here.
I'm trying to call/print/log JSON from a GET request. I need to get the id's following this tree: items > track > artists > id.
Basically im trying to just get the ID's from each track on this JSON file/list. I keep getting errors no matter how I format them. Again im new so im not sure how to call it let also run it in a loop to get all the ID's as an array. I have done it with other JSON files in the past but this one is farther in and I get the error : TypeError: list indices must be integers or slices, not str
I figured out it works when you do it by adding the [0] like this:
artists = response["items"][0]["track"]["artists"] BUT I want to loop it and get all of the Id's for each one and that option picks just one.
here is the beginning of the json so you can see the layout.
{
"href": "https://api.spotify.com/v1/me/tracks?offset=0&limit=15&market=US",
"items": [
{
"added_at": "2021-12-15T22:26:25Z",
"track": {
"album": {
"album_type": "single",
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/6MlPT0WxdWnrYcpXT8GZF8"
},
"href": "https://api.spotify.com/v1/artists/6MlPT0WxdWnrYcpXT8GZF8",
"id": "6MlPT0WxdWnrYcpXT8GZF8",
"name": "PARKFORD",
"type": "artist",
"uri": "spotify:artist:6MlPT0WxdWnrYcpXT8GZF8"
}
],
"external_urls": {
"spotify": "https://open.spotify.com/album/4o2d8uBTyMfJeaJqSXn9tP"
},
"href": "https://api.spotify.com/v1/albums/4o2d8uBTyMfJeaJqSXn9tP",
"id": "4o2d8uBTyMfJeaJqSXn9tP",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b27332bcd9e1b2234c6cd6b2b2ec",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/ab67616d00001e0232bcd9e1b2234c6cd6b2b2ec",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/ab67616d0000485132bcd9e1b2234c6cd6b2b2ec",
"width": 64
}
],
"name": "There's Nothing in the Rain",
"release_date": "2021-11-25",
"release_date_precision": "day",
"total_tracks": 1,
"type": "album",
"uri": "spotify:album:4o2d8uBTyMfJeaJqSXn9tP"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/6MlPT0WxdWnrYcpXT8GZF8"
},
"href": "https://api.spotify.com/v1/artists/6MlPT0WxdWnrYcpXT8GZF8",
"id": "6MlPT0WxdWnrYcpXT8GZF8",
"name": "PARKFORD",
"type": "artist",
"uri": "spotify:artist:6MlPT0WxdWnrYcpXT8GZF8"
here is the code I have written out
uri = MY_LIKED_SONGS
headers = {
"Authorization": f'Bearer {tokens["access_token"]}',
"Content-Type": "application/json",
}
r = requests.get(uri, headers=headers)
response = r.json()
# return response
artist_ids = {}
data = json.dumps(response)
for artist_ids in data["items"]["track"]:
logger.info(artist_ids["items"]["track"]["artists"]["id"])
print(artist_ids["_source"]["items"][0]["track"]["id"])
tracks = response["items"]["track"][0]["artists"]["id"]
Here when you do json.dumps(), it converts a Python object into a json string. In order to iterate through json object you need to convert it into json object. For that you have to use json.loads() then you can get artist id from that.
data = json.loads(json.dumps(response))
for item in data["items"]:
for artist in item['track']['album']['artists']:
print(artist['id'])
This might be helpful.
I'm not advanced with Python Json. I have these Json result:
{
"href": "https://api.spotify.com/v1/users/wizzler/playlists",
"items": [
{
"collaborative": false,
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c"
},
"href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c",
"id": "53Y8wT46QIMz5H4WQ8O22c",
"images": [],
"name": "Wizzlers Big Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzler"
},
"href": "https://api.spotify.com/v1/users/wizzler",
"id": "wizzler",
"type": "user",
"uri": "spotify:user:wizzler"
},
"public": true,
"snapshot_id": "bNLWdmhh+HDsbHzhckXeDC0uyKyg4FjPI/KEsKjAE526usnz2LxwgyBoMShVL+z+",
"tracks": {
"href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c/tracks",
"total": 30
},
"type": "playlist",
"uri": "spotify:user:wizzler:playlist:53Y8wT46QIMz5H4WQ8O22c"
},
{
"collaborative": false,
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju"
},
"href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju",
"id": "1AVZz0mBuGbCEoNRQdYQju",
"images": [],
"name": "Another Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzlersmate"
},
"href": "https://api.spotify.com/v1/users/wizzlersmate",
"id": "wizzlersmate",
"type": "user",
"uri": "spotify:user:wizzlersmate"
},
"public": true,
"snapshot_id": "Y0qg/IT5T02DKpw4uQKc/9RUrqQJ07hbTKyEeDRPOo9LU0g0icBrIXwVkHfQZ/aD",
"tracks": {
"href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju/tracks",
"total": 58
},
"type": "playlist",
"uri": "spotify:user:wizzlersmate:playlist:1AVZz0mBuGbCEoNRQdYQju"
}
],
"limit": 9,
"next": null,
"offset": 0,
"previous": null,
"total": 9
}
Now I need to extract only the Playlist ids. How to do that?
Edit:
I get the Json Data from doing:
r = requests.get(BASE_URL + 'users/' + user_id + '/playlists', headers=headers)
r = r.json()
print(r) returning me the Json Data. When I try to data = json.load(r)
I get these error! AttributeError: 'dict' object has no attribute 'read'
First, load the JSON file using the built in json library.
import json
with open('path/to/json/file.json') as f:
data = json.load(f)
Then, use a list comprehension to get only the IDs.
playlist_ids = [item['id'] for item in data['items']]
Edit: Or, if you've got your JSON parsed already, just use the list comprehension. Don't do r = r.json(), that will reset the request object to the data. Set it to some other variable, data is OK - data = r.json()
playlist_ids = [item['id'] for item in data['items']]
Edit 2: If you only want it where the owner ID is "wizzler", then add a if clause to the list comprehension.
playlist_ids = [item['id'] for item in data['items'] if item['owner']['id'] == 'wizzler']
I am trying to combine multiple lists in selected format. Simply, trying to create
elapsed + "' " + player + ' (A: ' + assist + ') - ' + detail (for example: 51' H. Onyekuru (A: R. Babel) - Normal Goal ). I also added the json file i took the data. Maybe it can be created directly without creating lists.
Code:
elapsed = []
player = []
assist = []
detail = []
for item in data['response']:
player.append(item['player']['name'])
for item in data['response']:
elapsed.append(item['time']['elapsed'])
for item in data['response']:
assist.append(item['assist']['name'])
for item in data['response']:
detail.append(item['detail'])
JSON file:
{
"get": "fixtures/events",
"parameters": { "fixture": "599120", "type": "goal" },
"errors": [],
"results": 3,
"paging": { "current": 1, "total": 1 },
"response": [
{
"time": { "elapsed": 51, "extra": null },
"team": {
"id": 645,
"name": "Galatasaray",
"logo": "https://media.api-sports.io/football/teams/645.png"
},
"player": { "id": 456, "name": "H. Onyekuru" },
"assist": { "id": 19034, "name": "R. Babel" },
"type": "Goal",
"detail": "Normal Goal",
"comments": null
},
{
"time": { "elapsed": 79, "extra": null },
"team": {
"id": 645,
"name": "Galatasaray",
"logo": "https://media.api-sports.io/football/teams/645.png"
},
"player": { "id": 456, "name": "H. Onyekuru" },
"assist": { "id": 142959, "name": "K. Akturkoglu" },
"type": "Goal",
"detail": "Normal Goal",
"comments": null
},
{
"time": { "elapsed": 90, "extra": 7 },
"team": {
"id": 3573,
"name": "Gazi\u015fehir Gaziantep",
"logo": "https://media.api-sports.io/football/teams/3573.png"
},
"player": { "id": 25921, "name": "A. Maxim" },
"assist": { "id": null, "name": null },
"type": "Goal",
"detail": "Penalty",
"comments": null
}
]
}
Output:
['H. Onyekuru', 'H. Onyekuru', 'A. Maxim']
[51, 79, 90]
['R. Babel', 'K. Akturkoglu', None]
['Normal Goal', 'Normal Goal', 'Penalty']
Sure you can – just iterate over the events and print out those lines (or gather them into a list if you like, for example). The f-string syntax below requires Python 3.6 or newer.
data = {
# ... elided for brevity, see OP's post
}
for event in data["response"]:
print(f"{event['time']['elapsed']}' {event['player']['name']} (A: {event['assist']['name']}) {event['detail']}")
This prints out
51' H. Onyekuru (A: R. Babel) Normal Goal
79' H. Onyekuru (A: K. Akturkoglu) Normal Goal
90' A. Maxim (A: None) Penalty
This creates a list of strings in the format that you want. As a bonus, python is quite nice for iterables so it can be done in one line.
list_of_all = [f"{item['time']['elapsed']}' {item['player']['name']} ({item['assist']['name']}) {item['detail']}" for item in data['response']]
Hi im am trying to parse json data and gets this error every time the element
if ['fields']['assignee'] in each:
TypeError: list indices must be integers or slices, not str
>>>
My json is this
{
"expand": "schema,names",
"startAt": 1,
"maxResults": 50,
"total": 7363,
"issues": [
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "591838",
"self": "https://jira.mynet.com/rest/api/2/issue/591838",
"key": "TEST-8564",
"fields": {
"summary": "delete tables 31-03-2020 ",
"customfield_10006": 2.0,
"created": "2020-02-27T10:29:12.000+0100",
"description": "A LOT OF TEXT",
"assignee": null,
"labels": [
"DATA",
"Refined"
],
"status": {
"self": "https://jira.mynet.com/rest/api/2/status/10000",
"description": "",
"iconUrl": "https://jira.mynet.com/",
"name": "To Do",
"id": "10000",
"statusCategory": {
"self": "https://jira.mynet.com/rest/api/2/statuscategory/2",
"id": 2,
"key": "new",
"colorName": "blue-gray",
"name": "To Do"
}
}
}
}
]
}
The element in ['fields']['assignee'] is NULL in this example
sometimes it is like this
"assignee": : {
"self": "https://mynet.com/rest/api/2/user?username=xxxxxx",
"name": "sij",
"key": "x",
"emailAddress": xx#mynet.com",
"avatarUrls": {
"48x48": "https://mynet.com/secure/useravatar?ownerId=bdysdh&avatarId=16743",
"24x24": "https://mynet.com/secure/useravatar?size=small&ownerId=bdysdh&avatarId=16743",
"16x16": "https://mynet.com/secure/useravatar?size=xsmall&ownerId=bdysdh&avatarId=16743",
"32x32": "https://mynet.com/secure/useravatar?size=medium&ownerId=bdysdh&avatarId=16743"
},
"displayName": "Bruce Springsteen",
"active": true,
"timeZone": "Arctic/Longyearbyen"
},
I am trying to check of assignee is null and if so print null
my code looks like this
with open('C:\\TEMP\\testdata.json') as json_file:
data = json.load(json_file)
for each in data['issues']:
if ['fields']['assignee'] in each:
print (['fields']['assignee']['name'])
else:
print ('null')
I have tried to put in [0] between ['fields']['assignee']['name'] but nothing seems to help.
Try with
if 'fields' in each and 'assignee' in each['fields']:
Note that you need the name of the key, not surrounded by square brackets.
Perhaps better:
for each in data['issues']:
print(each.get('fields', {}).get('assignee', {}).get('name', 'null'))
and if you can't guarantee that 'issues' exists in data either:
for each in data.get('issues', []):
<as before>
data.get('issues', []) returns an empty list if data['issuess'] doesn't exist.