Skip to content

Instantly share code, notes, and snippets.

@Muffindrake
Last active June 23, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muffindrake/b4809aec67e9e3020cbc7c6b63215f13 to your computer and use it in GitHub Desktop.
Save Muffindrake/b4809aec67e9e3020cbc7c6b63215f13 to your computer and use it in GitHub Desktop.
A small program that prints summoner profiles on opgg to stdout. Requires you to start it after you opened the league client. (currently assumes a Linux machine under Wine)
#!/usr/bin/env -S rdmd
import core.thread;
import std.algorithm.sorting;
import std.array : appender;
import std.datetime.systime;
import std.file;
import std.format;
import std.json;
import std.process;
import std.range.primitives;
import std.range;
import std.regex;
import std.stdio;
import std.uri;
string lol_region;
string lol_user;
string[] outbuf;
string
lol_lastlog(string dir)
{
auto entry = dirEntries(dir, "*LeagueClientUx.log", SpanMode.shallow).array;
entry.sort!("a.timeLastAccessed < b.timeLastAccessed");
return entry.back.name;
}
string
lol_match_and_retrieve_json(char[] buf)
{
size_t offset;
size_t backset;
while (buf.length - 1 > offset && buf[offset] != '{')
offset++;
if (offset == buf.length - 1)
return "";
backset = buf.length - 1;
while (backset > 0 && buf[backset] != '}')
backset--;
if (backset == 0)
return "";
return buf[offset .. backset + 1].idup;
}
string
lol_display_name(string json)
{
JSONValue j;
try
j = parseJSON(json);
catch (JSONException e)
return "";
if ("displayName" in j)
return j["displayName"].str;
return "";
}
void
lol_pretty_print_with_url(string[4] names)
{
string users = "users: %s, %s, %s, %s"
.format(names[0], names[1], names[2], names[3]);
string url = "https://porofessor.gg/pregame/%s/%s,%s,%s,%s"
.format(lol_region,
encode(cast (const char[]) names[0]),
encode(cast (const char[]) names[1]),
encode(cast (const char[]) names[2]),
encode(cast (const char[]) names[3]));
users.write;
" ".write;
url.writeln;
url.browse;
}
int
main(string[] args)
{
char[] buf;
size_t cread;
ulong offset;
string json;
string name;
string path = environment["WINEPREFIX"]
~ "/drive_c/Riot Games/League of Legends/Logs/LeagueClient Logs";
lol_region = environment.get("LOL_REGION", "euw");
lol_user = environment.get("LOL_USER", "Muffindrake");
string log = lol_lastlog(path);
auto f = File(log);
f.seek(0, SEEK_END);
outbuf.reserve(4);
auto name_outbuf = appender(&outbuf);
again:
try {
cread = f.readln(buf);
if (cread == 0)
goto skip;
json = buf.lol_match_and_retrieve_json;
if (json == "")
goto skip;
name = json.lol_display_name;
if (name == "" || name == lol_user)
goto skip;
name_outbuf.put(name);
if (outbuf.length == 4) {
outbuf[0 .. 4].lol_pretty_print_with_url;
outbuf.length = 0;
name_outbuf = appender(&outbuf);
}
} catch (StdioException) {
}
skip:
if (f.eof) {
offset = f.tell;
f.reopen(log);
f.seek(offset);
Thread.sleep(dur!("seconds")(5));
}
goto again;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment