Skip to content

Instantly share code, notes, and snippets.

@vladholubiev
Created June 27, 2014 15:21
Show Gist options
  • Save vladholubiev/cbc0903db0e8ee7f617d to your computer and use it in GitHub Desktop.
Save vladholubiev/cbc0903db0e8ee7f617d to your computer and use it in GitHub Desktop.
Alphabetically sorts vk audio. Time = audios / 3
package vkapi.Audio;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class Sorting {
public static String permission = "audio";
private String ACCESS_TOKEN = "";
private String USER_ID = "";
private int OFFSET;
public Sorting(String ACCESS_TOKEN, String USER_ID) {
this.ACCESS_TOKEN = ACCESS_TOKEN;
this.USER_ID = USER_ID;
}
private int audioCount;
private int presentAudios;
private int sleepCounter;
private String firstAudioAfterID;
private LinkedHashMap<String, String> audiosUnsorted = new LinkedHashMap<>();
private TreeMap<String, String> audios = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
public void setOFFSET(int OFFSET) {
this.OFFSET = OFFSET;
}
private void parse(String resp) throws IOException, ParseException, URISyntaxException, InterruptedException {
JSONParser parser = new JSONParser();
Object obj = parser.parse(resp);
JSONObject jo = (JSONObject) obj;
JSONArray ja = (JSONArray) jo.get("response");
try {
for (int i = 1; i < ja.size(); i++) {
Object aJa = ja.get(i);
Object songObj = parser.parse(String.valueOf(aJa));
JSONObject song = (JSONObject) songObj;
String audioName = ((String) song.get("artist")).trim() + " - "
+ ((String) song.get("title")).trim();
String audioID = String.valueOf(song.get("aid"));
audiosUnsorted.put(audioName, audioID);
}
} catch (NullPointerException npe) {
System.out.println("Too many requests per second. Sleeping for 1000ms");
Thread.sleep(1000);
} //Checking if new audio added
if (presentAudios != audiosUnsorted.size() && ja.size() > 1) {
presentAudios = audiosUnsorted.size();
setOFFSET(audiosUnsorted.size());
audioGet();
} else {
audios.putAll(audiosUnsorted);
System.out.println();
audios.keySet().forEach(System.out::println);
firstAudioAfterID = audios.firstEntry().getValue();
}
}
public void sort() throws ParseException, IOException, URISyntaxException, NullPointerException, InterruptedException {
getAudioCount();
audioGet();
setFirstAudio();
System.out.println();
Map<String, String> audiosDesc = new TreeMap(Collections.reverseOrder());
audiosDesc.putAll(audios);
for (Map.Entry<String, String> entry : audiosDesc.entrySet()) {
Thread.sleep(300);
move(entry.getValue(), "0", firstAudioAfterID, sleepCounter);
sleepCounter++;
}
}
private int move(String audioID, String beforeID, String afterID, int sleepCounter)
throws URISyntaxException, IOException, ParseException {
int success = 0;
URIBuilder builder = new URIBuilder();
builder.setScheme("https").setHost("api.vk.com").setPath("/method/audio.reorder")
.setParameter("audio_id", audioID)
.setParameter("owner_id", USER_ID)
.setParameter("before", beforeID)
.setParameter("after", afterID)
.setParameter("access_token", ACCESS_TOKEN);
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
HttpClient httpclient = HttpClientBuilder.create().build();
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) try (InputStream inputStream = entity.getContent()) {
String responseAsString = IOUtils.toString(inputStream);
JSONParser parser = new JSONParser();
Object obj = parser.parse(responseAsString);
JSONObject jo = (JSONObject) obj;
try {
success = ((Long) jo.get("response")).intValue();
System.out.println(sleepCounter + "/" + audioCount + " Moving: " + audioID);
} catch (NullPointerException npe) {
System.out.println(jo);
}
}
return success;
}
public void audioGet() throws URISyntaxException, IOException, ParseException, InterruptedException {
URIBuilder builder = new URIBuilder();
builder.setScheme("https").setHost("api.vk.com").setPath("/method/audio.get")
.setParameter("owner_id", USER_ID)
.setParameter("need_user", "0")
.setParameter("count", "1000")
.setParameter("offset", String.valueOf(OFFSET))
.setParameter("access_token", ACCESS_TOKEN);
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
HttpClient httpclient = HttpClientBuilder.create().build();
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) try (InputStream inputStream = entity.getContent()) {
String responseAsString = IOUtils.toString(inputStream);
System.out.print("\r" + audiosUnsorted.size() + "\\" + audioCount); //Progress counter
parse(responseAsString);
}
}
public void setFirstAudio() throws ParseException, IOException, URISyntaxException {
String firstAudioAfterID = audios.firstEntry().getValue();
move(firstAudioAfterID, audiosUnsorted.entrySet().iterator().next().getValue(), "0", sleepCounter);
}
public void getAudioCount() throws URISyntaxException, IOException, ParseException {
URIBuilder builder = new URIBuilder();
builder.setScheme("https").setHost("api.vk.com").setPath("/method/audio.getCount")
.setParameter("owner_id", USER_ID)
.setParameter("access_token", ACCESS_TOKEN);
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
HttpClient httpclient = HttpClientBuilder.create().build();
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) try (InputStream inputStream = entity.getContent()) {
String responseAsString = IOUtils.toString(inputStream);
JSONParser parser = new JSONParser();
Object obj = parser.parse(responseAsString);
JSONObject jo = (JSONObject) obj;
audioCount = ((Long) jo.get("response")).intValue();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment