LoginSignup
8
8

More than 5 years have passed since last update.

D言語でD言語くんbotを書くために調べたことまとめ

Last updated at Posted at 2015-12-02

はじめに

D言語でD言語くんbotを書くために調べたことをまとめます。

D言語くんbotでやること

  • 設定ファイル(YAML)を読み込む
  • Twitter API
    • 単語を指定して検索する
    • 対象のツイートをリツイートをする
    • 対象のユーザーをフォローをする
  • 結果をDBに反映する

正規表現

assert("123-abc-def".match(regex("([a-z]{3})")).captures[0] == "abc");

データ形式

HTML

test.html
<html>
  <body>
    <p>title</p>
    <ul>
      <li>0</li>
      <li>1</li>
    </ul>
  </body>
</html>
import std.stdio,
       std.conv,
       html;

void main(){
  auto html = readText("test.html");
  auto doc = createDocument(html);

  doc.querySelector("body").text;

  assert(doc.querySelector("p").text == "title");

  int i = 0;
  foreach(li; doc.querySelectorAll("li")) {
    assert(li.text == to!string(i));
    i++;
  }
}

JSON

test.json
{
  "int":    123,
  "string": "abc",
  "bool":   true,
  "null":   null,
  "1st": {
    "2nd": "nested"
  },
  "array": [
    {"index": 0},
    {"index": 1}
  ]
}
import std.stdio,
       std.json;

void main(){
  auto json_string = readText("test.json");

  JSONValue json = parseJSON(json_string);

  assert(json["int"].integer == 123);
  assert(json["string"].str == "abc");
  assert(json["bool"].type == JSON_TYPE.TRUE);
  assert(json["null"].isNull);
  assert(json["1st"]["2nd"].str == "nested");
  assert(json["array"].array.length == 2);
  foreach(int i, JSONValue v; json["array"].array){
    assert(v["index"].integer == i);
  }
  assert("2nd" in json["1st"].object);
  assert(("not_found" in json.object) == null);
}

YAML

test.yaml
int:    123
string: abc
bool:   true
null_value: null
1st:
  2nd: nested
array:
- index: 0
- index: 1
import std.stdio,
       std.conv,
       dyaml.all;

void main(){
  auto yaml = Loader("test.yaml").load();

  assert(yaml["int"].as!int == 123);
  assert(yaml["string"].as!string == "abc");
  assert(yaml["bool"] == true);
  assert(yaml["null_value"].isNull);
  assert(yaml["1st"]["2nd"] == "nested");
  int i = 0;
  foreach(Node node; yaml["array"]){
    assert(node["index"].as!int == i);
    i++;
  }
}

データベース

MySQL

DDBC

MySQL, PostgreSQL, SQLiteに対応。
http://code.dlang.org/packages/ddbc

その他

Twitter

REPL

CentOS 7で動作を確認しました。

sudo yum install linenoise-devel
git clone https://github.com/MartinNowak/drepl
git submodule init
git submodule update
dub

下記Warningが出たらallocator.dを修正して再度ビルドします。

libdparse/src/std/allocator.d(4229,15): Warning: instead of C-style syntax, use D-style syntax 'Allocator[(max - (min - 1)) / step] buckets'

writelnを打っても何も反応が無くて一瞬焦りますが、落ち着いてimport std.stdio;して下さい。

./drepl
Welcome to D REPL.
D> 1 + 1
2
D> "Hello, DREPL!"
Hello, DREPL!
D> import std.stdio;
std
D> writeln("Hello, DREPL!");
Hello, DREPL!

D> 

さいごに

まだリツイートとフォローしかできないbotですが、生温かい目で見守って頂けると幸いです。
なお、ソースはGithubで公開しています。生き恥を晒したくない方は是非ご利用下さい。

8
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8