Skip to content

Commit

Permalink
Replace twitter client to dlang-requests version
Browse files Browse the repository at this point in the history
  • Loading branch information
simdnyan committed Dec 24, 2017
1 parent d29edc5 commit b073b6a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "twitter4d"]
path = twitter4d
url = git@github.com:simdnyan/twitter4d.git
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"authors": ["simdnyan"],
"dependencies": {
"mysql-native": "~>1.1.4",
"twitter4d": "~>0.0.70",
"twitter4d": { "path": "./twitter4d" },
"dyaml": "~>0.6.3"
}
}
5 changes: 3 additions & 2 deletions dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"libevent": "2.0.2+2.0.16",
"memutils": "0.4.9",
"mysql-native": "1.1.4",
"requests": "0.3.2",
"taggedalgebraic": "0.10.8",
"tinyendian": "0.1.2",
"twitter4d": "0.0.70",
"twitter4d": {"path":"twitter4d"},
"vibe-core": "1.3.0",
"vibe-d": "0.8.1"
"vibe-d": "0.7.32"
}
}
25 changes: 19 additions & 6 deletions source/dmanbot.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import std.stdio,
std.typecons,
std.variant,
core.stdc.stdlib,
requests,
twitter4d,
mysql,
dyaml;
Expand Down Expand Up @@ -82,14 +83,18 @@ class DmanBot {

if (dry_run) writefln("search: %s", request);

Response ret;
try {
auto ret = twitter.request("GET", "search/tweets.json", request);
return parseJSON(ret)["statuses"].array;
ret = twitter.request("GET", "search/tweets.json", request);
if (ret.code != 200) {
throw new Exception(ret.responseBody.to!string);
}
} catch (Exception e) {
stderr.writefln("[%s] Catch %s. Can't get tweets. { \"q\": \"%s\", \"max_id\": \"%d\" }", currentTime(), e.msg, q, max_id);
exit(1);
assert(0);
}
return parseJSON(ret.responseBody.to!string)["statuses"].array;
}

bool retweet(ulong tweet_id) {
Expand All @@ -98,8 +103,12 @@ class DmanBot {
if (!prepared.queryRow().isNull) return false;
if (!dry_run) {
try {
if (!do_not_post)
twitter.request("POST", format("statuses/retweet/%d.json", tweet_id));
if (!do_not_post) {
Response ret;
ret = twitter.request("POST", format("statuses/retweet/%d.json", tweet_id));
if (ret.code != 200)
throw new Exception(ret.responseBody.to!string);
}
Prepared insert = prepare(mysql, "insert into retweets (id) values (?);");
insert.setArgs(tweet_id);
insert.exec();
Expand All @@ -117,8 +126,12 @@ class DmanBot {
if (!prepared.queryRow().isNull) return false;
if (!dry_run) {
try {
if (!do_not_post)
twitter.request("POST", "friendships/create.json", ["user_id": user_id.to!string]);
if (!do_not_post) {
Response ret;
ret = twitter.request("POST", "friendships/create.json", ["user_id": user_id.to!string]);
if (ret.code != 200)
throw new Exception(ret.responseBody.to!string);
}
Prepared insert = prepare(mysql, "insert into follow_requests (id) values (?);");
insert.setArgs(user_id);
insert.exec();
Expand Down
1 change: 1 addition & 0 deletions twitter4d
Submodule twitter4d added at a363ba

0 comments on commit b073b6a

Please sign in to comment.