1 module moeimgd.moeimg;
2 
3 import std.stdio, std.regex, std.conv, std.string, std.traits, std.net.curl, std.algorithm, std.range;
4 
5 struct Article {
6 	string index;
7 	string name;
8 
9 	this(string index, string name) {
10 		this.index = index;
11 		this.name = name;
12 	}
13 	this(char[] index, char[] name) {
14 		this.index = cast(string)index;
15 		this.name = cast(string)name;
16 	}
17 
18 	string getURL() {
19 		return format("http://moeimg.net/%s.html", index);
20 	}
21 }
22 
23 struct Image {
24 	string archive_num;
25 	string filename;
26 	Article article;
27 
28 	this(string archive_num, string filename, Article article) {
29 		this.archive_num = archive_num;
30 		this.filename = filename;
31 		this.article = article;
32 	}
33 	this(char[] archive_num, char[] filename, Article article) {
34 		this.archive_num = cast(string)archive_num;
35 		this.filename = cast(string)filename;
36 		this.article = article;
37 	}
38 	string getURL() {
39 		return format("http://img.moeimg.net/wp-content/uploads/archives%s/%s/%s", archive_num, article.index, filename);
40 	}
41 }
42 
43 Article[] getArticles(uint page) {
44 	Article[] result;
45 	try {
46 		char[] content = get(format("http://moeimg.net/page/%d", page));
47 		auto r = ctRegex!("<a href=\"http://moeimg.net/([0-9]+?).html\" title=\"(.+?)\">");
48 		foreach(c; matchAll(content, r)) {
49 			result ~= Article(c[1], c[2]);
50 		}
51 
52 	} catch(Exception e) {
53 		stderr.writeln("An error occured");
54 		throw e;
55 	}
56 	return result.uniq.array;
57 }
58 
59 Image[] getImages(Article article) {
60 	Image[] result;
61 	try {
62 		char[] content = get(article.getURL());
63 		auto r = regex(format("<a href=\"http://img.moeimg.net/wp-content/uploads/archives([0-9].*?)/%s/(.+?)\" target=\"_blank\">", article.index));
64 		foreach(c; matchAll(content, r)) {
65 			result ~= Image(c[1], c[2], article);
66 		}
67 	} catch(Exception e) {
68 		stderr.writeln("An error occured");
69 	}
70 	return result;
71 }