<commit_before><?php

$apikey = file_get_contents(__DIR__ . "/apikey.json");
$apikey = json_decode($apikey);

if ($apikey === null) {
    die(json_encode([
        'status' => 'ERROR',
        'error' => 'apikey.json is required!',
    ]));
}

if (isset($apikey->gracenote->user_id) === false) {
    $apikey->gracenote->user_id = null;
}

// iTunes API
define('ITUNES_API_END_POINT', 'https://itunes.apple.com/search?country=JP&entity=song');

require 'vendor/autoload.php';

use Gracenote\Rhythm\RhythmAPI;
use GuzzleHttp\Client;

$app = new \Slim\Slim();
$api = new RhythmAPI($apikey->gracenote->client_id, $apikey->gracenote->user_id);

$app->get('/', function () use ($api) {
    echo json_encode([
        'status'=> 'ok',
        'user_id' => $api->user_id,
    ]);
});

/**
 * iThunes API
 * Get Params
 * term: 曲名
 * artist: アーティスト名で絞り込み、 correct_result に結果を出す
 *
 * Response JSON
 * status: "OK" or "ERROR"
 * correct_result: null or Object (term と artist が一致した曲が入る)
 * results: Array<Object> 検索結果の曲一覧
 */
$app->get('/itunes', function () use ($app) {
    $params = $app->request()->params();

    $client = new Client();
    $res = $client->get(ITUNES_API_END_POINT, [
        'query' => [
            'country' => 'JP',
            'entity' => 'musicTrack',
            'term' => $params['term'],
            'limit' => 10,
        ]
    ]);

    $data = json_decode($res->getBody());

    $correct_result = null;
    if (isset($params['artist'])) {
        $filtered_results = array_filter($data->results, function ($result) use ($params) {
            return mb_strrichr($result->artistName, $params['artist']) !== false;
        });
        if (count($filtered_results) > 0) {
            $correct_result = array_shift($filtered_results);
        }
    }

    echo json_encode([
        'status' => isset($data->results) && count($data->results) > 1 ? 'OK' : 'ERROR',
        'correct_result' => $correct_result,
        'results' => $data->results,
    ]);
});

/**
 * Gracenote Rhythm API - create
 *
 */
$app->get('/create', function () use ($api, $app) {<commit_msg>fix itunes api call<commit_after><?php

$apikey = file_get_contents(__DIR__ . "/apikey.json");
$apikey = json_decode($apikey);

if ($apikey === null) {
    die(json_encode([
        'status' => 'ERROR',
        'error' => 'apikey.json is required!',
    ]));
}

if (isset($apikey->gracenote->user_id) === false) {
    $apikey->gracenote->user_id = null;
}

// iTunes API
define('ITUNES_API_END_POINT', 'https://itunes.apple.com/search');

require 'vendor/autoload.php';

use Gracenote\Rhythm\RhythmAPI;
use GuzzleHttp\Client;

$app = new \Slim\Slim();
$api = new RhythmAPI($apikey->gracenote->client_id, $apikey->gracenote->user_id);

$app->get('/', function () use ($api) {
    echo json_encode([
        'status'=> 'ok',
        'user_id' => $api->user_id,
    ]);
});

/**
 * iThunes API
 * Get Params
 * term: 曲名
 * artist: アーティスト名で絞り込み、 correct_result に結果を出す
 *
 * Response JSON
 * status: "OK" or "ERROR"
 * correct_result: null or Object (term と artist が一致した曲が入る)
 * results: Array<Object> 検索結果の曲一覧
 */
$app->get('/itunes', function () use ($app) {
    $params = $app->request()->params();

    $client = new Client();
    $res = $client->get(ITUNES_API_END_POINT, [
        'query' => [
            'country' => 'JP',
            'entity' => 'musicTrack',
            'attribute' => 'songTerm',
            'term' => $params['term'],
            'limit' => 10,
        ]
    ]);

    $data = json_decode($res->getBody());

    $correct_result = null;
    if (isset($params['artist'])) {
        $filtered_results = array_filter($data->results, function ($result) use ($params) {
            return mb_strrichr($result->artistName, $params['artist']) !== false;
        });
        if (count($filtered_results) > 0) {
            $correct_result = array_shift($filtered_results);
        }
    }

    echo json_encode([
        'status' => isset($data->results) && count($data->results) > 1 ? 'OK' : 'ERROR',
        'correct_result' => $correct_result,
        'results' => $data->results,
    ]);
});

/**
 * Gracenote Rhythm API - create
 *
 */
$app->get('/create', function () use ($api, $app) {
{'fraction_non_alphanumeric': 0.1954653937947494, 'fraction_numerical': 0.0019093078758949881, 'mean_word_length': 2.9575070821529743, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 76, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3694763', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1518, 'n_words': 279}