Skip to content

Instantly share code, notes, and snippets.

@h4t0n
Last active May 4, 2017 21:29
Show Gist options
  • Save h4t0n/abfd4d2bf7f46b92c44f375d1702fca2 to your computer and use it in GitHub Desktop.
Save h4t0n/abfd4d2bf7f46b92c44f375d1702fca2 to your computer and use it in GitHub Desktop.
Retweet from Influencer with Spring Boot and Twitter4j
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import twitter4j.*;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Stream;
@Service
@ConditionalOnProperty("retweetInfluencer")
public class RetweetInfluencer {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RetweetInfluencer.class);
private Twitter twitter;
@Value("${retweetInfluencer.file:retweetInfluencer.txt}")
private String INFLUENCER_FILE;
@Value("${retweetInfluencer.tweetLanguage:it}")
private String TWEET_LANGUAGE;
private Map<String, Double> probabilityByInfluences = new HashMap<>();
private Date lastCheckedTime;
ConcurrentLinkedQueue<Long> retweetsQueue = new ConcurrentLinkedQueue<>();
@PostConstruct
private void init() throws TwitterException, IOException {
twitter = TwitterFactory.getSingleton();
User user = twitter.verifyCredentials();
log.info(String.format("Twitter user: %s", user.getScreenName()));
try (Stream<String> stream = Files.lines(Paths.get(INFLUENCER_FILE))) {
stream.forEach(line -> {
String[] split = line.split(",");
String name = split[0];
double probability = 1.;
if (split.length > 1) {
probability = Double.parseDouble(split[1]);
}
probabilityByInfluences.put(name, probability);
});
} catch (IOException e) {
log.error(e.getMessage());
}
log.info(String.format("Influencer: %s", probabilityByInfluences));
lastCheckedTime = new Date();
}
@Scheduled(fixedRateString = "${retweetInfluencer.checkRate:120000}")
public void getRetweets() {
log.info("RETWEET INFLUENCER - START");
Date checkedTime = new Date();
for (String influencer : probabilityByInfluences.keySet()) {
try {
ResponseList<Status> userTimeline = twitter.getUserTimeline(influencer);
for (Status status : userTimeline) {
Date createdAt = status.getCreatedAt();
if (createdAt.after(lastCheckedTime)
&& status.getInReplyToUserId() < 0
&& status.getInReplyToStatusId() < 0
&& !status.isRetweet()
&& status.getLang().equals(TWEET_LANGUAGE)
&& Math.random() <= probabilityByInfluences.get(influencer)) {
log.info(String.format("Add retweet from: %s - %s", influencer, status.getText()));
retweetsQueue.add(status.getId());
}
}
} catch (TwitterException e) {
log.error(e.getMessage());
}
}
lastCheckedTime = checkedTime;
log.info("RETWEET INFLUENCER - STOP");
}
@Scheduled(fixedRateString = "${retweetInfluencer.retweetRate:30000}")
public void retweet() {
Long toRetweetId = retweetsQueue.poll();
if (toRetweetId != null) {
try {
twitter.retweetStatus(toRetweetId);
} catch (TwitterException e) {
log.error(e.getMessage());
}
}
log.info(String.format("Queue size: %d", retweetsQueue.size()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment