0

I'm working with the songkick api found here and am very close to finishing a program I'm working on to pull info about a few specific artists upcoming shows. I have inputted an array of metro_areas that I want to track and output ONLY shows within THESE cities and their accompanying ID's. Other info I'm pulling is date of show, artist name, venue name. Basically right now my program is able to pull every show from the list of artist_ids I've inputted iterating through the id's in the request url for the range of dates in my parameters. My current output looks something like this:

['Date', 'Artist Name', 'Venue Name', 'City', 'metroArea ID']

['FEB - 7', 'Rosalia', 'NOTO', 'Philadelphia, PA, US', 5202]
['FEB - 8', 'Rosalia', 'Audio', 'San Francisco, CA, US', 26330]
['FEB - 8', 'Kid Cudi', 'Shady Park', 'Tempe, AZ, US', 23068]
['FEB - 8', 'Kid Cudi', 'Madison Square Garden', 'New York City, NY, US', 7644]

I want the output to be like this:

['FEB - 8', 'Rosalia', 'Audio', 'San Francisco, CA, US', 26330]
['FEB - 8', 'Kid Cudi', 'Madison Square Garden', 'New York City, NY, US', 7644]

Based on this array I've defined at the beginning of my program to match with the metro_areas in the songkick object array.

metro_areas = [
    ('Los Angeles', '17835'),
    ('San Francisco', '26330'),
    ('New York City', '7644'),
    ('Seattle', '2846'),
    ('Nashville', '11104')
]

Here is the json object array that I pull for each artist_id:

{
  "resultsPage": {
    "results": {
      "event": [
        {
          "id":11129128,
          "type":"Concert",
          "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
          "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
          "start": {
            "time":"20:00:00",
            "date":"2012-04-18",
            "datetime":"2012-04-18T20:00:00-0800"
          },
          "performance": [
            {
              "artist": {
                "id":29835,
                "uri":"http://www.songkick.com/artists/29835-wild-flag?utm_source=PARTNER_ID&utm_medium=partner",
                "displayName":"Wild Flag",
                "identifier": []
              },
              "id":21579303,
              "displayName":"Wild Flag",
              "billingIndex":1,
              "billing":"headline"
            }
          ],
          "location": {
            "city":"San Francisco, CA, US",
            "lng":-122.4332937,
            "lat":37.7842398
          },
          "venue": {
            "id":6239,
            "displayName":"The Fillmore",
            "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
            "lng":-122.4332937,
            "lat":37.7842398,
            "metroArea": {
              "id":26330,
              "uri":"http://www.songkick.com/metro-areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
              "displayName":"SF Bay Area",
              "country": { "displayName":"US" },
              "state": { "displayName":"CA" }
            }
          },
          "status":"ok",
          "popularity":0.012763
        }, ....
      ]
    },
    "totalEntries":24,
    "perPage":50,
    "page":1,
    "status":"ok"
  }
}

Some more code to see how I'm getting to my output from the JSON in the songkick requests.

metro_areas = [
                    ('Los Angeles','17835'),
                    ('San Francisco', '26330'),
                    ('New York City','7644'),
                    ('Seattle','2846'),
                    ('Nashville','11104')
               ]



# artists we want to track
artist_ids = [
    ('Rosalia', '4610868'), ('EARTHGANG', '5720759'), ('Kid Cudi', '8630279'), ('Kanye West', '5566863'),
    ('Ludacris', '398291'), ('Hayley Williams', '10087966')
]

# Fetch existing events in each metro area
for artist_id in artist_ids:

params = {
        'apikey': 'API_KEY',
        'min_date': '2020-02-01',
        'max_date': '2020-02-08',
        # 'type': 'Concert'
    }

r = requests.get('https://api.songkick.com/api/3.0/artists/' + artist_id[1] + '/calendar.json', params=params)
    response = r.json()

shows = response['resultsPage']['results']

    for show in shows:
        try:
            shows = shows['event']

            formatted_shows = [{
                'artistID': [perf['artist']['id'] for perf in s['performance']],
                'date': s['start']['date'],
                'name': [perf['artist']['displayName'] for perf in s['performance']],
                'metroArea': s['venue']['metroArea']['id'],
                'city': s['location']['city'],
                'venue': s['venue']['displayName']
                }
                for s in shows if len(s['performance']) > 0
            ]
            for sub in formatted_shows:
                if sub['artistID'] == artist_id[1]:
                    sub['name'] = artist_id[0]
new_show = artist_id[1]
                new_show_name = artist_id[0]
                new_date = sub['date']
                new_date_time = new_date = datetime.strptime(new_date, '%Y-%m-%d')
                date_time_fin = new_date_time.strftime('%b - %-d').upper()

                formatted_show_final = [date_time_fin, new_show_name, sub['venue'], sub['city'], sub['metroArea']
                print(formatted_show_final)


Long story short, I need to find a way to iterate through each of my listed metro_areas id's (LA, SF, NYC, Seattle, Nashville) only and only output the shows that match with 'metroArea': s['venue']['metroArea']['id'] for each request iteration.

14
  • 1
    Why don't you use the Upcoming Events by Metro Area API instead? Commented Jan 23, 2020 at 19:02
  • @SunnyPatel if I do it that way I'll have to do the same thing I'm trying to do here but with a list of artists and their accompanying ID's instead. My list of artists is way bigger than the metro areas list so I thought this way would be easier. Commented Jan 23, 2020 at 19:17
  • You'll be making less requests if you has less metro areas, so that would be better. :) Commented Jan 23, 2020 at 19:30
  • @SunnyPatel I'm not sure if that would be the case since there is a 50 shows per page limit, so I'd have to paginate since I'm looking for a range of dates spanning a full month. Either way I'd either have to match and output only shows with my pre-defined metro_areas id array or from my pre-defined artist_ids array. Commented Jan 23, 2020 at 19:33
  • You don't even put full code here. Plz provide full code Commented Jan 23, 2020 at 21:10

1 Answer 1

1

If I understood well the question, add inside the second for loop: if sub['metroArea'] in [area[1] for area in metro_areas]:

    for show in shows:
        try:
            shows = shows['event']
            formatted_shows = [{
                'artistID': [perf['artist']['id'] for perf in s['performance']],
                'date': s['start']['date'],
                'name': [perf['artist']['displayName'] for perf in s['performance']],
                'metroArea': s['venue']['metroArea']['id'],
                'city': s['location']['city'],
                'venue': s['venue']['displayName']
                }
                for s in shows if len(s['performance']) > 0
            ]
            for sub in formatted_shows:
                #Modified here to apply str() function to transform #sub['metroArea'] to string instead of int value
                if str(sub['metroArea']) in [area[1] for area in metro_areas]:
                    if sub['artistID'] == artist_id[1]:
                        sub['name'] = artist_id[0]
                    new_show = artist_id[1]
                    new_show_name = artist_id[0]
                    new_date = sub['date']
                    new_date_time = new_date = datetime.strptime(new_date, '%Y-%m-%d')
                    date_time_fin = new_date_time.strftime('%b - %-d').upper()
                    formatted_show_final = [date_time_fin, new_show_name, sub['venue'], sub['city'], sub['metroArea']]
                    print(formatted_show_final)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.
Thank you so much! this worked perfectly after converting the metroArea from formatted_shows to a string to compare against! Much appreciated!
Yes I had forgot to convert it to string. I modified the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.