I am trying to get videos using search query with youtube v3 api Search: list.
My query body is something like follows :
request = youtube.search().list(
part="snippet",
maxResults=40,
q="Cat work york"
)
But the problem is, it returns live videos among other regular videos. I dont want live videos to be returned, how can I do that ? please help me here.
Thanks
Try this https://developers.google.com/youtube/v3/docs/search/list with the following filters:
part="snippet",
q="/-Live",
safeSearch="strict",
type="video
I think you should try passing this parameter :
broadcastStatus="completed"
Here is the reference :
https://developers-dot-devsite-v2-prod.appspot.com/youtube/v3/live/docs/liveBroadcasts/list#parameters
I tried with this request and I got no live videos - without need of additional filters:
URL:
https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=10&q=Cat%20work%20york&type=video&key=[YOUR_API_KEY]
And these are the results of the first page:
{
"kind": "youtube#searchListResponse",
"etag": "-KMpLk38ZnJHhWd5fPfmwou7bX4",
"nextPageToken": "CAoQAA",
"regionCode": "CO",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 10
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "fe7kdo2hrlF2OjEpk8Nd1kbStFw",
"id": {
"kind": "youtube#video",
"videoId": "jDoVckC6NhA"
},
"snippet": {
"publishedAt": "2015-07-30T21:25:54Z",
"channelId": "UC9k-yiEpRHMNVOnOi_aQK8w",
"title": "Woman Endures Cat Calls Walking in Street: 'It Makes You Feel Like A Fool'",
"description": "Cosmopolitan magazine is getting a lot of attention for a video they posted to their website of women facing a barrage of cat calls just by walking down the streets ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/jDoVckC6NhA/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/jDoVckC6NhA/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/jDoVckC6NhA/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Inside Edition",
"liveBroadcastContent": "none",
"publishTime": "2015-07-30T21:25:54Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "k0tqw-tDAsnJZ5HJjBmHXZyiaiY",
"id": {
"kind": "youtube#video",
"videoId": "aAxx1EZwDIY"
},
"snippet": {
"publishedAt": "2016-02-13T05:35:16Z",
"channelId": "UC-2ePlhDlfJyzU7vypHFgBQ",
"title": "2 night races in traffic (cat & mouse)!!",
"description": "what i normally encounter on my way home !!",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/aAxx1EZwDIY/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/aAxx1EZwDIY/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/aAxx1EZwDIY/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Daily Street Racing",
"liveBroadcastContent": "none",
"publishTime": "2016-02-13T05:35:16Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "grZqq0QA-Z1xxcNDM4Iukhqx3GI",
"id": {
"kind": "youtube#video",
"videoId": "nMlJJKNWQZY"
},
"snippet": {
"publishedAt": "2018-06-24T13:00:04Z",
"channelId": "UCINb0wqPz-A0dV9nARjJlOQ",
"title": "Cat Runs A Real Estate Office In Brooklyn | The Dodo City Pets",
"description": "This Cat Runs A Real Estate Office In Brooklyn | Valentino's dad found him as a stray and brought him to work at his real estate office in Carroll Gardens, ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/nMlJJKNWQZY/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/nMlJJKNWQZY/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/nMlJJKNWQZY/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "The Dodo",
"liveBroadcastContent": "none",
"publishTime": "2018-06-24T13:00:04Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "SzycZTHAXxzRnxJH7pl1H-3AHhc",
"id": {
"kind": "youtube#video",
"videoId": "ZUO9ijDjXTg"
},
"snippet": {
"publishedAt": "2020-08-14T03:04:14Z",
"channelId": "UCcl8P4rYjdbxbFTXxkNXuZQ",
"title": "Mega Hits 2020 ๐ Best Of Vocal Deep House Summer Music Mix ๐ Deep Feeling By ChilledVibes Live #7",
"description": "Mega Hits 2020 Best Of Vocal Deep House Summer Music Mix Deep Feeling By ChilledVibes Live #7 #SummerMusicMix #BestOfDeepHouse ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/ZUO9ijDjXTg/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/ZUO9ijDjXTg/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/ZUO9ijDjXTg/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "ChilledVibes",
"liveBroadcastContent": "none",
"publishTime": "2020-08-14T03:04:14Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "nNSPJNKlYZWYA5FDN96Jv9nFygk",
"id": {
"kind": "youtube#video",
"videoId": "-JOy1nM6UoA"
},
"snippet": {
"publishedAt": "2008-04-21T03:37:32Z",
"channelId": "UCmSOMw225KDZwqOsLFe9GSw",
"title": "CAT POWER - New York",
"description": "Snail is sad but determined to make things work in NYC, with music by Cat Power.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/-JOy1nM6UoA/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/-JOy1nM6UoA/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/-JOy1nM6UoA/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "puppetbully",
"liveBroadcastContent": "none",
"publishTime": "2008-04-21T03:37:32Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "HUmDdgYk1jz3lv6cNhiZLyqek8c",
"id": {
"kind": "youtube#video",
"videoId": "XcAH4OMPlDs"
},
"snippet": {
"publishedAt": "2020-04-17T15:53:42Z",
"channelId": "UCUw3KEvEX49JlnYhlneD-CA",
"title": "Running the York Cat Trail During Lockdown April 2020",
"description": "For anyone who would like to experience the York Cat Trail but who can't get to York. Come with me on a 12 minute whistle stop tour of the cats on buildings in ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/XcAH4OMPlDs/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/XcAH4OMPlDs/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/XcAH4OMPlDs/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "The Cat Gallery",
"liveBroadcastContent": "none",
"publishTime": "2020-04-17T15:53:42Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "B6Mx6RpMg8UXlABqaFPgJd_EOqk",
"id": {
"kind": "youtube#video",
"videoId": "df7g19GzFkg"
},
"snippet": {
"publishedAt": "2019-11-05T02:24:12Z",
"channelId": "UCDVYQ4Zhbm3S2dlz7P1GBDg",
"title": "Cat on the Field Scores 50-Yd TD Run! ๐พ",
"description": "A fluffy feline makes its way onto the field and somehow gets to the end zone for a touchdown! The Dallas Cowboys take on the New York Giants during Week 9 ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/df7g19GzFkg/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/df7g19GzFkg/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/df7g19GzFkg/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "NFL",
"liveBroadcastContent": "none",
"publishTime": "2019-11-05T02:24:12Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "k5CXKp3kDIqnyqT03Vxj9_tMdKg",
"id": {
"kind": "youtube#video",
"videoId": "j0RgF_AL5vk"
},
"snippet": {
"publishedAt": "2018-08-31T02:11:05Z",
"channelId": "UC4L0b4TTnV_dK67pWxPxPXA",
"title": "FAT CAT PUT ALL THAT WORK IN AND TURNED ON PAPPY MASON HE IS NOW IN ADX SUFFERING ๐คฆ๐ฝโโ๏ธ๐ข",
"description": "Fat cat TRIPLE DOUBLE CROSSER.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/j0RgF_AL5vk/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/j0RgF_AL5vk/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/j0RgF_AL5vk/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "KING EARNER",
"liveBroadcastContent": "none",
"publishTime": "2018-08-31T02:11:05Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "QM6IfM0553SCG8Lb21omcpVmZ2k",
"id": {
"kind": "youtube#video",
"videoId": "4cb1YIsyty4"
},
"snippet": {
"publishedAt": "2016-09-12T14:18:43Z",
"channelId": "UCEcDA5zAfZTVr93HoUzYf_w",
"title": "Catยฎ Power Box Rake Attachment Operating Tips",
"description": "http://www.cat.com/en_US/products/new/attachments/rakes.html | Learn operating tips for the Power Box Rake work tool attachment for Skid Steer Loaders, ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/4cb1YIsyty4/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/4cb1YIsyty4/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/4cb1YIsyty4/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Cat Landscaping and Construction",
"liveBroadcastContent": "none",
"publishTime": "2016-09-12T14:18:43Z"
}
},
{
"kind": "youtube#searchResult",
"etag": "gXii2GJAAanIFjYjqeIZvsctnHI",
"id": {
"kind": "youtube#video",
"videoId": "4pq9N8qrTT0"
},
"snippet": {
"publishedAt": "2020-07-14T14:23:32Z",
"channelId": "UChLtXXpo4Ge1ReTEboVvTDg",
"title": "''Rocco, put your tail down": Cat interrupts virtual UK parliamentary meeting",
"description": "A member of the UK parliament's cat interrupted a live virtual committee meeting on Tuesday, prompting laughter from others on the call. John Nicolson was ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/4pq9N8qrTT0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/4pq9N8qrTT0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/4pq9N8qrTT0/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Global News",
"liveBroadcastContent": "none",
"publishTime": "2020-07-14T14:23:32Z"
}
}
]
}
If you still got "live videos", add the videoDuration=medium filter in the URL request.
Related
I just downloaded some json from spotify and took a look into the pd.normalize_json().
But if I normalise the data i still have dictionaries within my dataframe. Also setting the level doesnt help.
DATA I want to have in my dataframe:
{
"collaborative": false,
"description": "",
"external_urls": {
"spotify": "https://open.spotify.com/playlist/5"
},
"followers": {
"href": null,
"total": 0
},
"href": "https://api.spotify.com/v1/playlists/5?additional_types=track",
"id": "5",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/a",
"width": 640
}
],
"name": "Another",
"owner": {
"display_name": "user",
"external_urls": {
"spotify": "https://open.spotify.com/user/user"
},
"href": "https://api.spotify.com/v1/users/user",
"id": "user",
"type": "user",
"uri": "spotify:user:user"
},
"primary_color": null,
"public": true,
"snapshot_id": "M2QxNTcyYTkMDc2",
"tracks": {
"href": "https://api.spotify.com/v1/playlists/100&additional_types=track",
"items": [
{
"added_at": "2020-12-13T18:34:09Z",
"added_by": {
"external_urls": {
"spotify": "https://open.spotify.com/user/user"
},
"href": "https://api.spotify.com/v1/users/user",
"id": "user",
"type": "user",
"uri": "spotify:user:user"
},
"is_local": false,
"primary_color": null,
"track": {
"album": {
"album_type": "album",
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/1dfeR4Had"
},
"href": "https://api.spotify.com/v1/artists/1dfDbWqFHLkxsg1d",
"id": "1dfeR4HaWDbWqFHLkxsg1d",
"name": "Q",
"type": "artist",
"uri": "spotify:artist:1dfeRqFHLkxsg1d"
}
],
"available_markets": [
"CA",
"US"
],
"external_urls": {
"spotify": "https://open.spotify.com/album/6wPXmlLzZ5cCa"
},
"href": "https://api.spotify.com/v1/albums/6wPXUJ9LzZ5cCa",
"id": "6wPXUmYJ9zZ5cCa",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab676620a47",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/ab67616d0620a47",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/ab603e6620a47",
"width": 64
}
],
"name": "The (Deluxe ",
"release_date": "1920-07-17",
"release_date_precision": "day",
"total_tracks": 15,
"type": "album",
"uri": "spotify:album:6m5cCa"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/1dg1d"
},
"href": "https://api.spotify.com/v1/artists/1dsg1d",
"id": "1dfeR4HaWDbWqFHLkxsg1d",
"name": "Q",
"type": "artist",
"uri": "spotify:artist:1dxsg1d"
}
],
"available_markets": [
"CA",
"US"
],
"disc_number": 1,
"duration_ms": 21453,
"episode": false,
"explicit": false,
"external_ids": {
"isrc": "GBU6015"
},
"external_urls": {
"spotify": "https://open.spotify.com/track/5716J"
},
"href": "https://api.spotify.com/v1/tracks/5716J",
"id": "5716J",
"is_local": false,
"name": "Another",
"popularity": 73,
"preview_url": null,
"track": true,
"track_number": 3,
"type": "track",
"uri": "spotify:track:516J"
},
"video_thumbnail": {
"url": null
}
}
],
"limit": 100,
"next": null,
"offset": 0,
"previous": null,
"total": 1
},
"type": "playlist",
"uri": "spotify:playlist:fek"
}
So what are best practices to read nested data like this into one dataframe in pandas?
I'm glad for any advice.
EDIT:
so basically I want all keys as columns in my dataframe. But with normalise it stops at "tracks.items" and if I normalise this again i have the recursive problem again.
It depends on the information you are looking for. Take a look at pandas.read_json() to see if that can work. Also you can select data as such
json_output = {"collaborative": 'false',"description": "", "external_urls": {"spotify": "https://open.spotify.com/playlist/5"}}
df['collaborative'] = json_output['collaborative'] #set value of your df to value of returned json values
I'm trying to get and store the videos from a youtube playlist into an list in python. I followed the instructions in the docs https://developers.google.com/youtube/v3/docs/playlistItems/list but it cant seem to retrieve the video ID.
def list_playlist_localizations(youtube, playlist_id):
results = youtube.playlists().list(
part='snippet, contentDetails', id=playlist_id, maxResults=5
).execute()
#print results
for playlist_item in results['items']:
title = playlist_item['snippet']['title']
video_id = playlist_item['snippet']['resourceId']['videoId']
print video_id
EDIT:
from hyper import HTTPConnection
import random
import requests
playlist_id = "PLx0sYbCqOb8TBPRdmBHs5Iftvv9TPboYG"
links = []
token = '' # to get ID to next page CDIQAA
# https://stackoverflow.com/questions/14173428/how-to-change-page-results-with-youtube-data-api-v3 - reference
s = requests.Session()
r = s.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlist_id + '&key={MY_KEY}')
data = r.json()
while 'nextPageToken' in data and len(data) < 200:
token = data['nextPageToken']
r = s.get('https://www.googleapis.com/youtube/v3/playlistItems?pageToken='
+ token +
'&part=snippet&maxResults=50&playlistId='
+ playlist_id +
'&key=AIzaSyAglPb2-eVaoy1muXclkUrfPFtdYEaUcCw')
data = r.json()
for items in data['items']:
links.append('https://www.youtube.com/watch?v=' + items['snippet']['resourceId']['videoId'])
r = s.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlist_id + '&key={MY_KEY}')
data = r.json()
# Add the remaining links
for items in data['items']:
links.append('https://www.youtube.com/watch?v=' + items['snippet']['resourceId']['videoId'])
'''
for items in links:
print items
'''
print "num items: " + str(len(links))
print "Random link: "
print links[random.randint(0, len(links) - 1)]
This is my implementation to generate random youtube links from a playlist
You can get the data use html requests
And you will get the playlist as JSON data
For example
import requests,json
key = 'YOUR API KEY'
playlistid = 'PLBCF2DAC6FFB574DE'
data = json.loads(requests.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={}&key={}'.format(playlistid,key)).text)
print(data)
the resoult will be
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/_HwI6Wa-Ku-4k5VAHT1GRPH2his\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 13,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/4MX9K88_OdrfvVVaQ2ys5BdQtI8\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkE2OTA3QzIwNEI3RjYxMDE=",
"snippet": {
"publishedAt": "2013-10-18T14:55:24.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Andrew Willis, Skatepark Engineer",
"description": "Andrew Willis built a skatepark in East London using reclaimed materials left over from the Olympic Games, creating a lasting legacy for the local community of Hackney Wick.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/GvgqDSnpRQM/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "GvgqDSnpRQM"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/PBY3vMCXQ8JLWA2_X9BSSpasHUo\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLjU5NzE2QkNERURDRTE5NDc=",
"snippet": {
"publishedAt": "2011-09-23T04:52:44.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "fIc_VEQ7Vo0"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/y4jkKn0lC3ggyxUi8Xr1NXcGPjA\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkZGQjNGMEFDQ0U0MzBCMzc=",
"snippet": {
"publishedAt": "2011-11-22T15:29:40.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Mark Lesek: A New/Old Prosthetic",
"description": "Follow Mark on Google+: https://plus.google.com/u/0/114778778979884307299/about\r\n\r\nMark lost his arm several years ago. Now his search for a better prosthetic could improve the lives of amputees everywhere.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/V4DDt30Aat4/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 2,
"resourceId": {
"kind": "youtube#video",
"videoId": "V4DDt30Aat4"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/9uUzwGG9HqDu7g3ZMZw3TYWlBOs\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkFBRDU2ODRCNTgwMzA2RTQ=",
"snippet": {
"publishedAt": "2011-10-20T01:02:11.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 3,
"resourceId": {
"kind": "youtube#video",
"videoId": "07718Vcwcyc"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"Wu2llbfqCdxIVjGbVPm2DslKPCA/x8utLR6SK07clGh9DQzv80Mczyw\"",
"id": "UExCQ0YyREFDNkZGQjU3NERFLkMyQjUzQkM1OTFFRTNFMEQ=",
"snippet": {
"publishedAt": "2011-11-22T15:29:40.000Z",
"channelId": "UCvceBgMIpKb4zK1ss-Sh90w",
"title": "Mark Kempton: Neighbors In Need",
"description": "Follow Mark on Google+: https://profiles.google.com/u/0/105705606437451864842\r\n\r\nWhen floodwaters hit northeast Australia, Mark's innovative search became the difference between life and death for many of his neighbors.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/XDgC4FMftpg/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Google Search Stories",
"playlistId": "PLBCF2DAC6FFB574DE",
"position": 4,
"resourceId": {
"kind": "youtube#video",
"videoId": "XDgC4FMftpg"
}
}
}
]
}
I downloaded the Spotify App on my Windows 10 and installed. So I had the ideia of make a software that need to get the music that is running at the moment. Is it possible to get It? It os not necessary the code. I just want to know where to start, because I did not find anything about It. I would rather do It using python.
You can use the Get the User's Currently Playing Track endpoint, documented here: https://developer.spotify.com/web-api/get-the-users-currently-playing-track/
The endpoint will return JSON that includes information about the track, for example:
{
"context": {
"external_urls" : {
"spotify" : "http://open.spotify.com/user/spotify/playlist/49znshcYJROspEqBoHg3Sv"
},
"href" : "https://api.spotify.com/v1/users/spotify/playlists/49znshcYJROspEqBoHg3Sv",
"type" : "playlist",
"uri" : "spotify:user:spotify:playlist:49znshcYJROspEqBoHg3Sv"
},
"timestamp": 1490252122574,
"progress_ms": 44272,
"is_playing": true,
"item": {
"album": {
"album_type": "album",
"external_urls": {
"spotify": "https://open.spotify.com/album/6TJmQnO44YE5BtTxH8pop1"
},
"href": "https://api.spotify.com/v1/albums/6TJmQnO44YE5BtTxH8pop1",
"id": "6TJmQnO44YE5BtTxH8pop1",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/8e13218039f81b000553e25522a7f0d7a0600f2e",
"width": 629
},
{
"height": 300,
"url": "https://i.scdn.co/image/8c1e066b5d1045038437d92815d49987f519e44f",
"width": 295
},
{
"height": 64,
"url": "https://i.scdn.co/image/d49268a8fc0768084f4750cf1647709e89a27172",
"width": 63
}
],
"name": "Hot Fuss",
"type": "album",
"uri": "spotify:album:6TJmQnO44YE5BtTxH8pop1"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/0C0XlULifJtAgn6ZNCW2eu"
},
"href": "https://api.spotify.com/v1/artists/0C0XlULifJtAgn6ZNCW2eu",
"id": "0C0XlULifJtAgn6ZNCW2eu",
"name": "The Killers",
"type": "artist",
"uri": "spotify:artist:0C0XlULifJtAgn6ZNCW2eu"
}
],
"available_markets": [
"AD",
"AR",
...
"TW",
"UY"
],
"disc_number": 1,
"duration_ms": 222075,
"explicit": false,
"external_ids": {
"isrc": "USIR20400274"
},
"external_urls": {
"spotify": "https://open.spotify.com/track/0eGsygTp906u18L0Oimnem"
},
"href": "https://api.spotify.com/v1/tracks/0eGsygTp906u18L0Oimnem",
"id": "0eGsygTp906u18L0Oimnem",
"name": "Mr. Brightside",
"popularity": 0,
"preview_url": "http://d318706lgtcm8e.cloudfront.net/mp3-preview/f454c8224828e21fa146af84916fd22cb89cedc6",
"track_number": 2,
"type": "track",
"uri": "spotify:track:0eGsygTp906u18L0Oimnem"
}
}
If you want even more info about the track, you can use the id you get from this endpoint to call any of the Track endpoints here: https://developer.spotify.com/web-api/track-endpoints/. There are lots of fun things you can do, like audio features or analysis.
I'm working on a Python script that can download images from Flickr, among other sites. I use the Flickr API to pull the various sizes of the image I'm trying to download and identify the URL for the original size. Well, that's what I'm trying to do. Here's my code so far...
URL = {a Flickr link}
flickr = re.match(r".*flickr\.com\/photos\/([^\/]+)\/([0-9^\/]+)\/", URL)
URL = "https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=6002c84e96ff95c1a861eafafa4284ba&photo_id=" + flickr.group(2) + "&format=json&nojsoncallback=1"
request = requests.get(URL)
result = request.text
parsed = re.match(r".\"Original\".*\"source\"\: \"([^\"]+)", result)
URL = parsed.group(1)
Using print() statements throughout my code, I know that the first regular expression (to parse the original Flickr URL to identify the photo ID) works properly, and that the API request works properly, returning the following result (using the example URL https://www.flickr.com/photos/matbellphotography/33413612735/sizes/h/)...
{ "sizes": { "canblog": 0, "canprint": 0, "candownload": 1,
"size": [
{ "label": "Square", "width": 75, "height": 75, "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_s.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/sq\/", "media": "photo" },
{ "label": "Large Square", "width": "150", "height": "150", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_q.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/q\/", "media": "photo" },
{ "label": "Thumbnail", "width": 100, "height": 67, "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_t.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/t\/", "media": "photo" },
{ "label": "Small", "width": "240", "height": "160", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_m.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/s\/", "media": "photo" },
{ "label": "Small 320", "width": "320", "height": "213", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_n.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/n\/", "media": "photo" },
{ "label": "Medium", "width": "500", "height": "333", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/m\/", "media": "photo" },
{ "label": "Medium 640", "width": "640", "height": "427", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_z.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/z\/", "media": "photo" },
{ "label": "Medium 800", "width": "800", "height": "534", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_c.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/c\/", "media": "photo" },
{ "label": "Large", "width": "1024", "height": "683", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_b.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/l\/", "media": "photo" },
{ "label": "Large 1600", "width": "1600", "height": "1067", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_4d92e2f70d_h.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/h\/", "media": "photo" },
{ "label": "Large 2048", "width": "2048", "height": "1365", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_81441ed1da_k.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/k\/", "media": "photo" },
{ "label": "Original", "width": "5760", "height": "3840", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_34cbc172c1_o.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/o\/", "media": "photo" }
] }, "stat": "ok" }
My code apparently breaks down after that. The second regular expression, intended to identify the download URL of the image at its original filesize, apparently doesn't find any matches. According to yet another print() statement...
parsed.group(1) = none
I setup the expression using RegExr, which identified exactly what I needed from the JSON result. What have I done wrong?
Maybe your requests.Response object has a json attribute that you can access directly. If not, simply import json, parse your request.content and work with the returned dictionary. Example:
>>> import json
>>> json_response = """
... { "sizes": { "canblog": 0, "canprint": 0, "candownload": 1,
... "size": [
... { "label": "Square", "width": 75, "height": 75, "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_s.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/sq\/", "media": "photo" },
... { "label": "Large Square", "width": "150", "height": "150", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_q.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/q\/", "media": "photo" },
... { "label": "Thumbnail", "width": 100, "height": 67, "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_t.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/t\/", "media": "photo" },
... { "label": "Small", "width": "240", "height": "160", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_m.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/s\/", "media": "photo" },
... { "label": "Small 320", "width": "320", "height": "213", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_n.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/n\/", "media": "photo" },
... { "label": "Medium", "width": "500", "height": "333", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/m\/", "media": "photo" },
... { "label": "Medium 640", "width": "640", "height": "427", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_z.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/z\/", "media": "photo" },
... { "label": "Medium 800", "width": "800", "height": "534", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_c.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/c\/", "media": "photo" },
... { "label": "Large", "width": "1024", "height": "683", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_645397d6a5_b.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/l\/", "media": "photo" },
... { "label": "Large 1600", "width": "1600", "height": "1067", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_4d92e2f70d_h.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/h\/", "media": "photo" },
... { "label": "Large 2048", "width": "2048", "height": "1365", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_81441ed1da_k.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/k\/", "media": "photo" },
... { "label": "Original", "width": "5760", "height": "3840", "source": "https:\/\/farm3.staticflickr.com\/2855\/33413612735_34cbc172c1_o.jpg", "url": "https:\/\/www.flickr.com\/photos\/matbellphotography\/33413612735\/sizes\/o\/", "media": "photo" }
... ] }, "stat": "ok" }"""
>>>
>>> json_parsed = json.loads(json_response)
>>> for img in json_parsed["sizes"]["size"]:
... print img.get("source")
...
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_s.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_q.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_t.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_m.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_n.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_z.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_c.jpg
https://farm3.staticflickr.com/2855/33413612735_645397d6a5_b.jpg
https://farm3.staticflickr.com/2855/33413612735_4d92e2f70d_h.jpg
https://farm3.staticflickr.com/2855/33413612735_81441ed1da_k.jpg
https://farm3.staticflickr.com/2855/33413612735_34cbc172c1_o.jpg
>>>
I have the following JSON object that I get from the Instagram API, it can have n number of posts (depending upon the count parameter provided).
{
"pagination": {
"next_url": "https:\/\/api.instagram.com\/v1\/users\/3\/media\/recent?access_token=184046392.f59def8.c5726b469ad2462f85c7cea5f72083c0&max_id=205140190233104928_3",
"next_max_id": "205140190233104928_3"
},
"meta": {
"code": 200
},
"data": [{
"attribution": null,
"tags": [],
"type": "image",
"location": {
"latitude": 37.798594362,
"name": "Presidio Bowling Center",
"longitude": -122.459878922,
"id": 27052
},
"comments": {
"count": 132,
"data": [{
"created_time": "1342734265",
"text": "Distinguishing!",
"from": {
"username": "naiicamilos",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_53690312_75sq_1336573463.jpg",
"id": "53690312",
"full_name": "Naii Camilos"
},
"id": "239194812924826175"
}, {
"created_time": "1342737428",
"text": "#kevin in Spanish Presidio means Jail",
"from": {
"username": "jm0426",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_25881992_75sq_1342156673.jpg",
"id": "25881992",
"full_name": "Juan Mayen"
},
"id": "239221343768285211"
}, {
"created_time": "1342768120",
"text": "Good imagination",
"from": {
"username": "kidloca",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_193133903_75sq_1342032241.jpg",
"id": "193133903",
"full_name": "Khaleda Noon"
},
"id": "239478811731694145"
}, {
"created_time": "1342775967",
"text": "Cwl!",
"from": {
"username": "awesomeath",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_179252164_75sq_1339745821.jpg",
"id": "179252164",
"full_name": "awesomeath"
},
"id": "239544638740894674"
}, {
"created_time": "1342796153",
"text": "\u597d\u7f8e\u263a",
"from": {
"username": "hidelau",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_47295330_75sq_1342763977.jpg",
"id": "47295330",
"full_name": "Hide Lau"
},
"id": "239713963951001995"
}, {
"created_time": "1343018007",
"text": "#mindfreak",
"from": {
"username": "info2021",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg",
"id": "27664191",
"full_name": "info2021"
},
"id": "241575017119224582"
}, {
"created_time": "1343068374",
"text": "#kevin please share and promote my last pic. This will be the new hype as instagram",
"from": {
"username": "thansy_mansy",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_189019343_75sq_1342951587.jpg",
"id": "189019343",
"full_name": "thansy_mansy"
},
"id": "241997523093295303"
}, {
"created_time": "1343068382",
"text": "#kevin :P",
"from": {
"username": "thansy_mansy",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_189019343_75sq_1342951587.jpg",
"id": "189019343",
"full_name": "thansy_mansy"
},
"id": "241997589589790922"
}]
},
"filter": "Rise",
"created_time": "1342676212",
"link": "http:\/\/instagr.am\/p\/NQD4KAABKF\/",
"likes": {
"count": 4810,
"data": [{
"username": "caitlyn_hammonds",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg",
"id": "198322184",
"full_name": "caitlyn_hammonds"
}, {
"username": "sophiafrancis",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_43092892_75sq_1340548333.jpg",
"id": "43092892",
"full_name": "Sophiaaa."
}, {
"username": "amna7861",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_175807260_75sq_1343135903.jpg",
"id": "175807260",
"full_name": "Amna Haroon"
}, {
"username": "yaya0318",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_74056_75sq_1287001004.jpg",
"id": "74056",
"full_name": "Mao Yaya"
}, {
"username": "jay_damage",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_197465040_75sq_1342932411.jpg",
"id": "197465040",
"full_name": "jay_damage"
}, {
"username": "reves",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_671833_75sq_1335966794.jpg",
"id": "671833",
"full_name": "Fernando D. Ramirez"
}, {
"username": "lizray1",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198450407_75sq_1343144120.jpg",
"id": "198450407",
"full_name": "lizray1"
}, {
"username": "alivewtheglory",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_37907416_75sq_1341561441.jpg",
"id": "37907416",
"full_name": "Marilynn C"
}, {
"username": "mnforever55",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_29255977_75sq_1334833008.jpg",
"id": "29255977",
"full_name": "mnforever55"
}]
},
"images": {
"low_resolution": {
"url": "http:\/\/distilleryimage9.s3.amazonaws.com\/beb7f896d16311e19fe21231380f3636_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http:\/\/distilleryimage9.s3.amazonaws.com\/beb7f896d16311e19fe21231380f3636_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http:\/\/distilleryimage9.s3.amazonaws.com\/beb7f896d16311e19fe21231380f3636_7.jpg",
"width": 612,
"height": 612
}
},
"caption": {
"created_time": "1342676255",
"text": "Happy birthday #amy !",
"from": {
"username": "kevin",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg",
"id": "3",
"full_name": "Kevin Systrom"
},
"id": "238708186813567655"
},
"user_has_liked": false,
"id": "238707833418289797_3",
"user": {
"username": "kevin",
"website": "",
"bio": "CEO & Co-founder of Instagram",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg",
"full_name": "Kevin Systrom",
"id": "3"
}
}, {
"attribution": null,
"tags": [],
"type": "image",
"location": {
"latitude": 38.503100608,
"name": "Goose & Gander",
"longitude": -122.468387538,
"id": 12059278
},
"comments": {
"count": 85,
"data": [{
"created_time": "1342555499",
"text": "Cheers !!! \ud83d\ude18\ud83d\ude18",
"from": {
"username": "kattiab",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_1345073_75sq_1340495505.jpg",
"id": "1345073",
"full_name": "kattia b"
},
"id": "237695212468572732"
}, {
"created_time": "1342558279",
"text": "happy birthday instagram!",
"from": {
"username": "alanasayshi",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_4235095_75sq_1341960681.jpg",
"id": "4235095",
"full_name": "Alana Boy\u00e9r"
},
"id": "237718535382504383"
}, {
"created_time": "1342567977",
"text": "Happy Natal Day Instagram!",
"from": {
"username": "cynrtst",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_12493918_75sq_1341538462.jpg",
"id": "12493918",
"full_name": "Cynthia L"
},
"id": "237799888639758668"
}, {
"created_time": "1342568896",
"text": "Happy Birthday \ud83c\udf89\ud83c\udf89\ud83c\udf89 was it a long labour \ud83d\ude02\ud83d\ude02\ud83d\ude02",
"from": {
"username": "relzie",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_15718874_75sq_1332290975.jpg",
"id": "15718874",
"full_name": "Relz"
},
"id": "237807595966960050"
}, {
"created_time": "1342579289",
"text": "Cheers #kevin and Happy Birthday #instagram thank you so much Kevin for creating instagram it's truly got me back out there taking more photos and falling in love with photography all over again...",
"from": {
"username": "bpphotographs",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_12149171_75sq_1339457436.jpg",
"id": "12149171",
"full_name": "bpphotographs"
},
"id": "237894779172557723"
}, {
"created_time": "1342652660",
"text": "#suz_h",
"from": {
"username": "ianyorke",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_1041088_75sq_1339844137.jpg",
"id": "1041088",
"full_name": "Ian Yorke"
},
"id": "238510264889118973"
}, {
"created_time": "1342667574",
"text": "Love your app\ud83d\udc97\ud83d\udc97\ud83d\udc97\ud83d\udc97\ud83d\udc97",
"from": {
"username": "gothangel1997",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_186799125_75sq_1342668825.jpg",
"id": "186799125",
"full_name": "Angel Mercado"
},
"id": "238635368570687940"
}, {
"created_time": "1342843274",
"text": "\ud83d\ude09\u2764",
"from": {
"username": "andescu",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_145554839_75sq_1337641309.jpg",
"id": "145554839",
"full_name": "andescu"
},
"id": "240109245637333685"
}]
},
"filter": "Sierra",
"created_time": "1342332400",
"link": "http:\/\/instagr.am\/p\/NF0G6bABA2\/",
"likes": {
"count": 3282,
"data": [{
"username": "caysondesigns",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_23464609_75sq_1329177054.jpg",
"id": "23464609",
"full_name": "Jasmine at Cayson Designs"
}, {
"username": "m_azooz16",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198179404_75sq_1343096096.jpg",
"id": "198179404",
"full_name": "m_azooz16"
}, {
"username": "shulinghuang",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_144887495_75sq_1342460246.jpg",
"id": "144887495",
"full_name": "H\u3002"
}, {
"username": "caitlyn_hammonds",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg",
"id": "198322184",
"full_name": "caitlyn_hammonds"
}, {
"username": "sophiafrancis",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_43092892_75sq_1340548333.jpg",
"id": "43092892",
"full_name": "Sophiaaa."
}, {
"username": "beatle1234",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg",
"id": "197988834",
"full_name": "beatle1234"
}, {
"username": "yaya0318",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_74056_75sq_1287001004.jpg",
"id": "74056",
"full_name": "Mao Yaya"
}, {
"username": "lizray1",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198450407_75sq_1343144120.jpg",
"id": "198450407",
"full_name": "lizray1"
}, {
"username": "rawr1234321",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_198492630_75sq_1343151765.jpg",
"id": "198492630",
"full_name": "rawr1234321"
}]
},
"images": {
"low_resolution": {
"url": "http:\/\/distilleryimage6.s3.amazonaws.com\/3ec59e18ce4311e1b8031231380702ee_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http:\/\/distilleryimage6.s3.amazonaws.com\/3ec59e18ce4311e1b8031231380702ee_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http:\/\/distilleryimage6.s3.amazonaws.com\/3ec59e18ce4311e1b8031231380702ee_7.jpg",
"width": 612,
"height": 612
}
},
"caption": {
"created_time": "1342332465",
"text": "Mellivora capensis - eagle rare, peat, honey, lemon, pineapple, black cardamom, chili, coconut foam",
"from": {
"username": "kevin",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg",
"id": "3",
"full_name": "Kevin Systrom"
},
"id": "235824269324456712"
},
"user_has_liked": false,
"id": "235823728972271670_3",
"user": {
"username": "kevin",
"website": "",
"bio": "CEO & Co-founder of Instagram",
"profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg",
"full_name": "Kevin Systrom",
"id": "3"
}
}, .....
So I m looking to iterate over each post individually and extract the tags, id, and the image urls. I m having some trouble, (since I m a PHP developer and finding it really hard to work with Python as a beginner).
Here's the code that I m using to iterate over each post and process the attributes provided. I dont want to store them in a list or dict. Just want to search through the tags.
(this is just a attempted code since I couldnt find which loop should I use)
info= simplejson.load(info)
print type(info['data']) # I get it as a list
for k, v in info['data']:
print v
I could have done this easily using php with a foreach :
foreach($info->data as $i) {
$tags = $i->tags();
$id = $i->id();
}
If info['data'] is a list, you should be able to iterate over it like so:
for post in info['data']:
tags = post['tags']
id = post['id']
image_urls = [] # An empty list -- we'll fill it below
for img_type in ['low_resolution', 'thumbnail', 'standard_resolution']:
image_urls.append(post['images'][img_type]['url'])
# Now image_urls has all the image urls in it
I think the part that's rather different from PHP is that where the key is "tags" in the JSON structure, you have to use the string "tags" in Python, whereas you would use the literal tags() in PHP.