Skip to content

Commit

Permalink
Close #20
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanys committed Dec 7, 2017
1 parent cbecaf8 commit 524f699
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'me.ruslanys'
version = '2.1.1'
version = '2.1.2'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
Expand All @@ -30,8 +30,8 @@ dependencies {
compile("com.fasterxml.jackson.core:jackson-core:$jacksonVersion")

// Utils
compile('commons-cli:commons-cli:1.3.1')
compile('commons-codec:commons-codec:1.10')
compile('org.apache.commons:commons-text:1.1')
compile('org.apache.commons:commons-lang3:3.6')
compile('com.google.guava:guava:23.0')
compile('org.jsoup:jsoup:1.10.3')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import me.ruslanys.vkmusic.entity.Audio;
import me.ruslanys.vkmusic.exception.VkException;
import me.ruslanys.vkmusic.util.JsonUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;
Expand All @@ -17,6 +17,7 @@
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -42,8 +43,22 @@ public class ScraperVkClient implements VkClient {
private static final int SLEEP_INTERVAL = 5_000;

private final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
private final String script;

private final Map<String, String> cookies = new HashMap<>();


public ScraperVkClient() throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("decrypt.js")))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
}
script = sb.toString();
}

@Override
public void addCookies(Map<String, String> cookies) {
this.cookies.putAll(cookies);
Expand Down Expand Up @@ -121,12 +136,13 @@ public List<Audio> getAudio(Long ownerId) {

@SneakyThrows
@Override
public void fetchUrls(List<Audio> audioList) {
public void fetchUrls(List<Audio> audioList) { // TODO: refactor
Map<Long, Audio> audioMap = new HashMap<>(audioList.size());
for (Audio audio : audioList) {
audioMap.put(audio.getId(), audio);
}

Long userId = fetchUserId();
int sleepInterval = SLEEP_INTERVAL;
int fromIndex = 0;
int toIndex = Math.min(fromIndex + 10, audioList.size());
Expand Down Expand Up @@ -167,7 +183,7 @@ public void fetchUrls(List<Audio> audioList) {
for (List object : lists) {
Audio audio = audioMap.get(((Number) object.get(0)).longValue());
String url = (String) object.get(2);
url = decrypt(url);
url = decrypt(userId, url);

audio.setUrl(url);
}
Expand All @@ -180,13 +196,12 @@ public void fetchUrls(List<Audio> audioList) {
}

@SneakyThrows
private String decrypt(String url) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("decrypt.js")))) {
scriptEngine.eval(bufferedReader);
private String decrypt(Long vkId, String url) {
String script = this.script.replace("${vkId}", vkId.toString()); // TODO: replace with bindings
scriptEngine.eval(script);

Invocable inv = (Invocable) scriptEngine;
return (String) inv.invokeFunction("decode", url);
}
Invocable inv = (Invocable) scriptEngine;
return (String) inv.invokeFunction("decode", url);
}

@Data
Expand Down
79 changes: 30 additions & 49 deletions src/main/resources/decrypt.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,60 @@
/**
* Обфусцированный код на JS от ВК, который получилось выдрать с фронта.
*
* Дойдут руки, перепишу.
*/

var mask = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN0PQRSTUVWXYZO123456789+/=";

function decode(url) {
if (~url.indexOf("audio_api_unavailable")) {
var e = url.split("?extra=")[1].split("#"), o = "" === e[1] ? "" : a(e[1]);
if (e = a(e[0]),
"string" != typeof o || !e)
return url;
if (e = a(e[0]), "string" != typeof o || !e) return url;
o = o ? o.split(String.fromCharCode(9)) : [];
for (var s, r, n = o.length; n--;) {
if (r = o[n].split(String.fromCharCode(11)),
s = r.splice(0, 1, e)[0],
!l[s])
return url;
if (r = o[n].split(String.fromCharCode(11)), s = r.splice(0, 1, e)[0], !l[s]) return url;
e = l[s].apply(null, r)
}
if (e && "http" === e.substr(0, 4))
return e
if (e && "http" === e.substr(0, 4)) return e
}
return url
}

function a(url) {
if (!url || url.length % 4 == 1)
return !1;
for (var e, i, o = 0, a = 0, s = ""; i = url.charAt(a++);)
i = mask.indexOf(i),
~i && (e = o % 4 ? 64 * e + i : i, o++ % 4) && (s += String.fromCharCode(255 & e >> (-2 * o & 6)));
function a(t) {
if (!t || t.length % 4 == 1) return !1;
for (var e, i, o = 0, a = 0, s = ""; i = t.charAt(a++);) i = r.indexOf(i), ~i && (e = o % 4 ? 64 * e + i : i, o++ % 4) && (s += String.fromCharCode(255 & e >> (-2 * o & 6)));
return s
}

l = {
function s(t, e) {
var i = t.length, o = [];
if (i) {
var a = i;
for (e = Math.abs(e); a--;) e = (i * (a + 1) ^ e + a) % i, o[a] = e
}
return o
}

var e = {}
e.__esModule = !0
e.audioUnmaskSource = decode;
var r = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN0PQRSTUVWXYZO123456789+/=", l = {
v: function (t) {
return t.split("").reverse().join("")
},
r: function (t, e) {
}, r: function (t, e) {
t = t.split("");
for (var i, o = r + r, a = t.length; a--;)
i = o.indexOf(t[a]),
~i && (t[a] = o.substr(i - e, 1));
for (var i, o = r + r, a = t.length; a--;) i = o.indexOf(t[a]), ~i && (t[a] = o.substr(i - e, 1));
return t.join("")
},
s: function (t, e) {
}, s: function (t, e) {
var i = t.length;
if (i) {
var o = s(t, e)
, a = 0;
for (t = t.split(""); ++a < i;)
t[a] = t.splice(o[i - 1 - a], 1, t[a])[0];
var o = s(t, e), a = 0;
for (t = t.split(""); ++a < i;) t[a] = t.splice(o[i - 1 - a], 1, t[a])[0];
t = t.join("")
}
return t
},
x: function (t, e) {
}, i: function (t, e) {
return l.s(t, e ^ ${vkId})
}, x: function (t, e) {
var i = [];
return e = e.charCodeAt(0),
each(t.split(""), function (t, o) {
i.push(String.fromCharCode(o.charCodeAt(0) ^ e))
}),
i.join("")
}
}

function s(t, e) {
var i = t.length
, o = [];
if (i) {
var a = i;
for (e = Math.abs(e); a--;)
o[a] = (e += e * (a + i) / e) % i | 0
return e = e.charCodeAt(0), each(t.split(""), function (t, o) {
i.push(String.fromCharCode(o.charCodeAt(0) ^ e))
}), i.join("")
}
return o
}

0 comments on commit 524f699

Please sign in to comment.