Last active
November 11, 2017 14:44
-
-
Save 8q/18ce5ca13fc4885cf7ba49b8e6d5cf51 to your computer and use it in GitHub Desktop.
ツイッターのリストに流れてくる画像をひたすら集めるやつ nohup ./start.sh &
This file contains hidden or 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
package fav2slack; | |
import java.io.IOException; | |
import com.ullink.slack.simpleslackapi.SlackChannel; | |
import com.ullink.slack.simpleslackapi.SlackSession; | |
import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory; | |
import twitter4j.Status; | |
import twitter4j.TwitterStream; | |
import twitter4j.TwitterStreamFactory; | |
import twitter4j.User; | |
import twitter4j.UserStreamAdapter; | |
import twitter4j.auth.AccessToken; | |
public class Main | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
TwitterStream twStream = new TwitterStreamFactory().getInstance(); | |
twStream.setOAuthConsumer( | |
"", | |
""); | |
twStream.setOAuthAccessToken(new AccessToken( | |
"", | |
"")); | |
final SlackSession session = SlackSessionFactory.createWebSocketSlackSession("<SlackbotToken>"); | |
session.connect(); | |
final SlackChannel channel = session.findChannelByName("<channel name>"); | |
twStream.addListener(new UserStreamAdapter() | |
{ | |
@Override | |
public void onFavorite(User source, User target, Status favoritedStatus) | |
{ | |
System.out.println(favoritedStatus); | |
session.sendMessage(channel, "https://twitter.com/" + favoritedStatus.getUser().getScreenName() + "/status/" + favoritedStatus.getId()); | |
} | |
}); | |
twStream.user(); | |
} | |
} |
This file contains hidden or 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
#!/bin/bash | |
cd `dirname $0` | |
java -jar gazou_atumeru.jar 1> out.log 2> err.log |
This file contains hidden or 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
package collectimage; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import twitter4j.ExtendedMediaEntity; | |
import twitter4j.Paging; | |
import twitter4j.ResponseList; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.auth.AccessToken; | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
Timer timer = new Timer(); | |
timer.schedule(new MyTimerTask(), 0, 300000); | |
} | |
} | |
class MyTimerTask extends TimerTask | |
{ | |
private Twitter tw; | |
private Paging paging; | |
private long listid; | |
private ResponseList<Status> userListStatuses; | |
public MyTimerTask(){ | |
tw = new TwitterFactory().getInstance(); | |
tw.setOAuthConsumer( | |
"CK", | |
"CS"); | |
tw.setOAuthAccessToken(new AccessToken( | |
"AT", | |
"ATS")); | |
listid = 825312362240499714L; //収集をかけるリストのid | |
paging = new Paging(); | |
paging.setCount(200); | |
} | |
@Override | |
public void run() | |
{ | |
try | |
{ | |
userListStatuses = tw.getUserListStatuses(listid, paging); | |
} | |
catch (TwitterException e) | |
{ | |
e.printStackTrace(); | |
return; | |
} | |
paging.setSinceId(userListStatuses.get(0).getId()); | |
for(Status status : userListStatuses) | |
{ | |
ExtendedMediaEntity[] extendedMediaEntities = status.getExtendedMediaEntities(); | |
for(ExtendedMediaEntity extendedMediaEntity : extendedMediaEntities) | |
{ | |
String url = extendedMediaEntity.getMediaURL(); | |
Pattern p = Pattern.compile("/([a-zA-Z0-9_-]+\\.[a-zA-Z0-9]+)$"); | |
Matcher m = p.matcher(url); | |
if(!m.find()) | |
{ | |
System.err.println("not found filename or file extension: " + url); | |
continue; | |
} | |
String filename = status.getId() + " " + m.group(1); | |
try(InputStream in = new URL(url + ":orig").openStream()) | |
{ | |
String path = System.getProperty("user.dir") + "/image/" + filename; | |
if(!Files.exists(Paths.get(path))) | |
{ | |
Files.copy(in, Paths.get(path)); | |
System.out.println("export " + path); | |
} | |
else | |
{ | |
System.out.println("already exist " + path); | |
} | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
return; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment