1 module dud.descriptiongetter.code;
2 
3 import std.array : array;
4 import std.exception : enforce;
5 import std.algorithm.iteration : each, filter, map;
6 import std.algorithm.searching : canFind;
7 import std.json;
8 
9 @safe:
10 
11 JSONValue getCodeDlangDump() @trusted {
12 	import std.exception : assumeUnique;
13 	import std.net.curl;
14 	import std.zlib;
15 
16 	auto data = get("https://code.dlang.org/api/packages/dump");
17 
18 	auto uc = new UnCompress();
19 
20 	const(void[]) un = uc.uncompress(data);
21 	return parseJSON(cast(const(char)[])un);
22 }
23 
24 JSONValue trimCodeDlangDump(ref JSONValue old) @trusted {
25 	if(old.type == JSONType.array) {
26 		return trimArray(old.array());
27 	} else if(old.type == JSONType.object) {
28 		return trimObject(old);
29 	} else {
30 		return old;
31 	}
32 }
33 
34 JSONValue trimArray(ref JSONValue[] old) {
35 	return JSONValue(old
36 			.map!((ref it) { return trimCodeDlangDump(it); })
37 			.array);
38 }
39 
40 JSONValue trimObject(ref JSONValue obj) {
41 	enforce(obj.type == JSONType.object);
42 	JSONValue ret;
43 	foreach(key, ref value; obj.objectNoRef()) {
44 		if(key == "info") {
45 			ret["packageDescription"] = trimPackageDescription(value);
46 		} else if(!canFind(
47 			[ "readme", "readmeMarkdown", "docFolder"
48 			, "packageDescriptionFile", "logo", "errors"
49 			, "categories", "owner", "errors", "stats", "textScore"
50 			, "updateCounter", "_id", "updatedAt", "commitID", "date"
51 			, "documentationURL"
52 			], key))
53 		{
54 			ret[key] = trimCodeDlangDump(value);
55 		}
56 	}
57 	return ret;
58 }
59 
60 JSONValue trimPackageDescription(ref JSONValue pkg) {
61 	enforce(pkg.type == JSONType.object);
62 	JSONValue ret;
63 	foreach(key, ref value; pkg.objectNoRef()) {
64 		if(!canFind([ "packageDescriptionFile" ], key)) {
65 			ret[key] = trimCodeDlangDump(value);
66 		}
67 	}
68 	return ret;
69 }