QDomDocumentでtwitterに凸

QDomDocumentのサンプルコードはQDomNode→QDomElementと変換してってコードになっとおけど、QDomElement::firstChild()じゃなくてもQDomElement::firstChildElement()なんて言うそのまんまの名前のものもあったりして。こりゃ使うっきゃない。どうしてもだらだらしがちだしね。

以前のQOAuth周りでゲットできたXMLをざざざーっと適当な構造体配列に突っ込む感じで。

ぶっちゃけOAuth経由でアクセスできて、XMLが取得さえできてしまえばあとは正直どうとでもなる。適当にメモリ上に格納できてしまえばあとは煮るなり焼くなり好きにした上でUIでもつけりゃtwitterクライアントのできあがり。まあ、投稿部分スルーしてるけどね。

以前上げたQOAuthのんと組み合わせれば簡単なコマンドライン型クライアントとかならさくっと作れるんじゃないでしょうか。俺は作りませんけどね。

しかしtwitterの吐くXML、status毎にuser情報持ってたりとかしていやに長い。できるだけ一回のアクセスで情報を流すって工夫だとは思うんだけど、RT元とRT自体の情報を1つのstatusが持ってたりとかするのね。いいのか悪いのかは分からんなあ。まあ取り込んで解析さえしてしまえば同じっちゃあそうだけど。

さて。

例によってかいつまむよー。ちょっとだらだら長いよー。

class UserEntry
{
public:
    QString u_id;
    QString u_name;
    QString u_screen_name;
    QString u_location;
    QString u_description;
    QString u_profile_image_url;
    QString u_url;
    QString u_protected;
    QString u_followers_count;
    QString u_profile_background_color;
    QString u_profile_text_color;
    QString u_profile_link_color;
    QString u_profile_sidebar_fill_color;
    QString u_profile_sidebar_border_color;
    QString u_friends_count;
    QString u_created_at;
    QString u_favourites_count;
    QString u_utc_offset;
    QString u_time_zone;
    QString u_profile_background_image_url;
    QString u_profile_background_tile;
    QString u_profile_use_background_image;
    QString u_notifications;
    QString u_geo_enabled;
    QString u_verified;
    QString u_following;
    QString u_statuses_count;
    QString u_lang;
    QString u_contributors_enabled;
    QString u_follow_request_sent;
    QString u_listed_count;
    QString u_show_all_inline_media;
    QString u_default_profile;
    QString u_default_profile_image;
    QString u_is_translator;

};

class TLEntry
{
public:
    QString s_created_at;
    QString s_id;
    QString s_text;
    QString s_source;
    QString s_truncated;
    QString s_favorited;
    QString s_in_reply_to_status_id;
    QString s_in_reply_to_user_id;
    QString s_in_reply_to_screen_name;
    QString s_retweet_count;
    QString s_retweeted;
    QString s_retweeted_status;
    QString s_user;

};

class TLParser
{
public:
    TLParser();
    bool statusParse(QString res);

protected:
    TLEntry elementToEntry(QDomElement element);
    UserEntry elementToUser(QDomElement element);

protected:
    QMap<QString,TLEntry> status;
    QMap<QString,UserEntry> users;
    QList<QString> timeline;
};
bool TLParser::statusParse(QString res)
{
    QDomDocument xml;
    if (!xml.setContent(res)) return false;

    QDomElement doc = xml.documentElement();
    QDomElement e = doc.firstChildElement();
    while(!e.isNull()) {
        if (e.tagName() == "status") {
            TLEntry entry = elementToEntry(e);
            if (!entry.s_id.isEmpty()) {
                status[entry.s_id] = entry;
                timeline.append(entry.s_id);
            }
        }
        e = e.nextSiblingElement();
    }
    return true;
}

TLEntry TLParser::elementToEntry(QDomElement element)
{
    TLEntry entry;
    QDomElement e = element.firstChildElement();
    while(!e.isNull()) {
        if (e.tagName() == "created_at") entry.s_created_at = e.text();
        else if (e.tagName() == "id") entry.s_id = e.text();
        else if (e.tagName() =="text") entry.s_text = e.text();
        else if (e.tagName() == "source") entry.s_source = e.text();
        else if (e.tagName() == "truncated") entry.s_truncated = e.text();
        else if (e.tagName() == "favorited") entry.s_favorited = e.text();
        else if (e.tagName() == "in_reply_to_status_id") entry.s_in_reply_to_status_id = e.text();
        else if (e.tagName() == "in_reply_to_user_id") entry.s_in_reply_to_user_id = e.text();
        else if (e.tagName() == "in_reply_to_screen_name") entry.s_in_reply_to_screen_name = e.text();
        else if (e.tagName() == "retweeted") entry.s_retweeted = e.text();
        else if (e.tagName() == "retweeted_status") {
            TLEntry r = elementToEntry(e);
            if (!r.s_id.isEmpty()) {
                entry.s_retweeted_status = r.s_id;
                status[r.s_id] = r;
            }
        }
        else if (e.tagName() == "user") {
            UserEntry u = elementToUser(e);
            if (!u.u_id.isEmpty()) {
                entry.s_user = u.u_id;
                users[u.u_id] = u;
            }
        }
        e = e.nextSiblingElement();
    }
    return entry;
}

UserEntry TLParser::elementToUser(QDomElement element)
{
    UserEntry user;
    QDomElement e = element.firstChildElement();
    while(!e.isNull()) {
        if (e.tagName() == "id") user.u_id = e.text();
        else if (e.tagName() == "name") user.u_name = e.text();
        else if (e.tagName() == "screen_name") user.u_screen_name = e.text();
        else if (e.tagName() == "location") user.u_location = e.text();
        else if (e.tagName() == "description") user.u_description = e.text();
        else if (e.tagName() == "profile_image_url") user.u_profile_image_url = e.text();
        else if (e.tagName() == "url") user.u_url = e.text();
        else if (e.tagName() == "protected") user.u_protected = e.text();
        else if (e.tagName() == "followers_count") user.u_followers_count = e.text();
        else if (e.tagName() == "profile_background_color") user.u_profile_background_color = e.text();
        else if (e.tagName() == "profile_text_color") user.u_profile_text_color = e.text();
        else if (e.tagName() == "profile_link_color") user.u_profile_link_color = e.text();
        else if (e.tagName() == "profile_sidebar_fill_color") user.u_profile_sidebar_fill_color = e.text();
        else if (e.tagName() == "profile_sidebar_border_color") user.u_profile_sidebar_border_color = e.text();
        else if (e.tagName() == "friends_count") user.u_friends_count = e.text();
        else if (e.tagName() == "created_at") user.u_created_at = e.text();
        else if (e.tagName() == "favourites_count") user.u_favourites_count = e.text();
        else if (e.tagName() == "utc_offset") user.u_utc_offset = e.text();
        else if (e.tagName() == "time_zone") user.u_time_zone = e.text();
        else if (e.tagName() == "profile_background_image_url") user.u_profile_background_image_url = e.text();
        else if (e.tagName() == "profile_background_tile") user.u_profile_background_tile = e.text();
        else if (e.tagName() == "profile_background_image_image") user.u_profile_use_background_image = e.text();
        else if (e.tagName() == "notifications") user.u_notifications = e.text();
        else if (e.tagName() == "geo_enabled") user.u_geo_enabled = e.text();
        else if (e.tagName() == "verified") user.u_verified = e.text();
        else if (e.tagName() == "following") user.u_following = e.text();
        else if (e.tagName() == "statuses_count") user.u_statuses_count = e.text();
        else if (e.tagName() == "lang") user.u_lang = e.text();
        else if (e.tagName() == "contributors_enabled") user.u_contributors_enabled = e.text();
        else if (e.tagName() == "follow_request_sent") user.u_follow_request_sent = e.text();
        else if (e.tagName() == "listed_count") user.u_listed_count = e.text();
        else if (e.tagName() == "show_all_inline_media") user.u_show_all_inline_media = e.text();
        else if (e.tagName() == "default_profile") user.u_default_profile = e.text();
        else if (e.tagName() == "default_profile_image") user.u_default_profile_image = e.text();
        else if (e.tagName() == "is_translator") user.u_is_translator = e.text();
        e = e.nextSiblingElement();
    }
    return user;
}

コメントを残す