Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komiya-atsushi/9ea7e7231a76d5d87124 to your computer and use it in GitHub Desktop.
Save komiya-atsushi/9ea7e7231a76d5d87124 to your computer and use it in GitHub Desktop.
セキココのページをスクレイピングして Twitter スクリーン名を収集し、それをもとに Twitter のリストを生成するやつ。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.UserList;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
/**
* セキココのページをスクレイピングして Twitter リストを生成するアプリです。
*
* <p>
* Twitter4J と Jsoup に依存しています。 <code>build.gradle</code> の内容 (抜粋) は以下のとおり。
* </p>
* <pre>
* dependencies {
* compile group: 'org.twitter4j', name: 'twitter4j-core', version: '4.0.2'
* compile group: 'org.jsoup', name: 'jsoup', version: '1.7.3'
* }
* </pre>
* <p>
* 実行には、Twitter のコンシューマキーとアクセストークンが必要となります。
* </p>
*
* @author KOMIYA Atsushi
*/
public class TokyoWebminingTwitterListFromSekicoco {
public static void main(String[] args) throws TwitterException {
int zasekiId = 356; // 座席ページの ID 的なやつ (URL の末尾)
int no = 38; // Twitter リストに付与する連番
Twitter twitter = TwitterFactory.getSingleton();
UserList twitterList = twitter.createUserList(
String.format("tokyowebmining-%d", no),
false,
String.format("第 %d 回 TokyoWebmining の参加者リスト", no));
twitter.createUserListMembers(
twitterList.getId(),
screenNames(zasekiId));
}
static String[] screenNames(int zasekiId) {
try {
URL url = new URL(String.format("http://sekico.co/zaseki/%d", zasekiId));
Document doc = Jsoup.parse(url, 10 * 1000);
Elements contents = doc.select("#sekicocoListArea div.content div.streamHeader span.screenName a");
List<String> screenNames = contents
.stream()
.map(a -> a.text().substring(1))
.collect(Collectors.toList());
String[] result = new String[screenNames.size()];
return screenNames.toArray(result);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment