<?php

$myfile = fopen("exercicis.sql", "w");

$next = "https://wodcat.com/api/w/v3/exercises/?limit=10&offset=0&ordering=-popular";
while($next != null) {
    echo "Fetching $next...\n";
    $content = json_decode(file_get_contents($next));
    $next = $content->next;
    foreach($content->results as $e) {
        //level
        if($e->level=="Lung") $level = 1;
        elseif($e->level=="Average") $level = 2;
        elseif($e->level=="Technically Complex") $level = 3;
        else $level = -1;

        //description
        $description = trim(preg_replace('/\s\s+/', ' ', strip_tags($e->teaser)));

        //fwrite
        fwrite($myfile, "INSERT INTO exercises (name, slug, description, modality, type, equipment_type, level, url_video_predefined) VALUES ('$e->name', '$e->slug', '$description', '$e->modality', '$e->class_exercise', '$e->equipment_type', $level, '$e->video');\n\n");
    }
}

fclose($myfile);
