Created
June 22, 2018 19:09
-
-
Save GrakovNe/c82169425a7f23e47d01d6c2d913cc28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.grakovne.mds.importer; | |
| import com.google.gson.Gson; | |
| import com.mashape.unirest.http.HttpResponse; | |
| import com.mashape.unirest.http.JsonNode; | |
| import com.mashape.unirest.http.Unirest; | |
| import java.io.File; | |
| import java.nio.file.Files; | |
| import java.util.Arrays; | |
| import java.util.stream.Collectors; | |
| public class Application { | |
| public static void main(String[] args) { | |
| File path = new File("/Users/grakovne/projects/mds/missed-stories/need to re-import"); | |
| Arrays | |
| .stream(path.listFiles()) | |
| .collect(Collectors.toList()) | |
| .parallelStream() | |
| .filter(f -> !f.getName().endsWith(".mp3") && !f.getName().contains("FILE")) | |
| .forEach(file -> { | |
| try { | |
| String content = new String(Files.readAllBytes(file.toPath())); | |
| Story story = new Gson().fromJson(content, Item.class).body; | |
| HttpResponse<JsonNode> assetResult = Unirest.post("http://grakovne.org:5050/api/v1/asset") | |
| .header("Authorization", "Basic Z3Jha292bmU6cmVkaDByc2U=") | |
| .field("file", new File(path.getCanonicalFile(), "story_" + file.getName() + ".mp3")) | |
| .asJson(); | |
| StoryForm form = new StoryForm() | |
| .setAnnotation(story.getAnnotation()) | |
| .setAuthors(story.getAuthors().stream().map(Author::getName).collect(Collectors.toList())) | |
| .setRating(story.getRating()) | |
| .setTitle(story.getTitle()) | |
| .setAssetId((Integer) assetResult.getBody().getObject().getJSONObject("body").get("id")) | |
| .setYear(story.getYear()); | |
| int result = Unirest.post("http://grakovne.org:5050/api/v1/story") | |
| .header("Authorization", "Basic Zha292bmU6cmVkaDByc2U=") | |
| .header("content-type", "application/json") | |
| .body(new Gson().toJson(form)).asJson().getStatus(); | |
| if (result == 200) { | |
| file.delete(); | |
| System.out.println("Story with ID=" + file.getName() + " UPLOADED"); | |
| } else { | |
| System.out.println("Story with ID=" + file.getName() + " ERROR: " + result); | |
| } | |
| } catch (Exception e) { | |
| // fuck you | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment