Beispiel #1
0
def recommend(event, context=None):
    body = event["body"]

    if not body:
        error = "post body is null or empty"
        logger.error(error)
        return {"statusCode": 500, "body": json.dumps({"error": error})}

    segment = json.loads(body)

    logger.info(segment)

    recommender = Recommender()

    # @todo, pass influencer seeds as argument
    influencer = os.path.join(os.path.join(local_dir, 'seeds'), 'mike.json')

    with open(influencer) as json_data:
        influences = json.load(json_data)

    logger.info(influences)

    recommender.artists = [a['id'] for a in influences['artists']]

    recommender.tracks = [t['id'] for t in influences['tracks']]

    recommender.genres = influences['genres']

    segment = Segment(
        segment['start_time'], segment['end_time'], segment['segment_type'],
        Power(segment['power']['min_intensity'],
              segment['power']['max_intensity']), segment['cadence'])

    try:
        results = recommender.get_tracks_for_segment(segment)

        print('got results from recommender.get_tracks_for_segment')

        logger.info(json.dumps(results, indent=4, sort_keys=True))

        return {
            "statusCode": 200,
            "headers": _get_headers(),
            "body": json.dumps(results, indent=4, sort_keys=True)
        }
    except Exception as e:
        logger.error(e.__doc__)
        logger.error(e.message)
        logger.error(traceback.format_exc())

        return {
            "statusCode": 500,
            "headers": _get_headers(),
            "body": json.dumps({"error": error})
        }
def main():
    recommender = Recommender()

    #influencer = '..\seeds\mike.json'

    influencer = '..\\seeds\\tribby.json'

    # influencer = '..\seeds\emily.json'

    with open(influencer) as json_data:
        influences = json.load(json_data)
        # print(influences)

    recommender.artists = [a['id'] for a in influences['artists']]

    recommender.tracks = [t['id'] for t in influences['tracks']]

    recommender.genres = influences['genres']

    recommender.allow_explicit_lyrics = True

    print 'Warmup, 10 mins, power 20% - 45%, rpm 90\n'
    segment = Segment(0, 600, "Warmup", Power(0.20, 0.45), 90)
    results = recommender.get_tracks_for_segment(segment)
    print_results(results)

    print 'SteadyState, 15 mins, power 45% - 75%, rpm 90\n'
    segment = Segment(0, 900, "SteadyState", Power(0.45, 0.75), 90)
    results = recommender.get_tracks_for_segment(segment)
    print_results(results)

    print 'IntervalsT, 20 mins, power 75% - 150%, rpm 120\n'
    segment = Segment(0, 1200, "IntervalsT", Power(0.75, 1.5), 90)
    results = recommender.get_tracks_for_segment(segment)
    print_results(results)

    print 'CoolDown, 10 mins, power 25% - 45%, rpm 90\n'
    segment = Segment(0, 600, "CoolDown", Power(0.25, 0.45), 90)
    results = recommender.get_tracks_for_segment(segment)
    print_results(results)