Created
September 21, 2017 08:45
-
-
Save MysticDoll/83b544e7ec70fad859267c44ec37518e to your computer and use it in GitHub Desktop.
ツイートの会話スレッドに現れてるユーザーの列挙
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ThreadedUsers = require("./threadedUsers"); | |
let threaded = new ThreadedUsers("https://twitter.com/sekimiya/status/666901649798000640"); | |
threaded.fetch().then(users => console.log(users)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {JSDOM} from "jsdom"; | |
import request from "request"; | |
export default class ThreadedUsers { | |
constructor(url) { | |
this.target = url | |
} | |
fetch() { | |
return (new Promise((resolve, reject) => | |
request.get({url: this.target}, (err, res) => err ? reject(err) : resolve(res)))) | |
.then(res => { | |
this.rawDOM = new JSDOM(res.body); | |
return this.rawDOM; | |
}) | |
.then(dom => { | |
this.users = this.listUser(dom); | |
return this.users; | |
}) | |
} | |
listUser(dom) { | |
return [...dom.window.document.querySelectorAll(".username.u-dir")] | |
.map(u => u.textContent) | |
.filter(u => u !== "@") | |
.filter((u, i, arr) => | |
arr.slice(0,i) | |
.filter(_u => _u === u).length === 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment