-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterBot.java
44 lines (37 loc) · 1.32 KB
/
TwitterBot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.olgamelnichenko;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.olgamelnichenko.model.ArticleNotFoundException;
import com.olgamelnichenko.model.News;
import com.olgamelnichenko.property.reader.PropertyReader;
import com.olgamelnichenko.service.NewsService;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
@Component
public class TwitterBot{
private final static String URL = "https://newsapi.org/";
private final NewsService NEWS_SERVICE = new NewsService(URL, new PropertyReader());
@Scheduled(cron = "0 0 7 * * *")
private void tweetArticle() throws ArticleNotFoundException {
try {
News todayNews = NEWS_SERVICE.getNews();
String title = todayNews.getArticles()[0].getTitle();
String url = todayNews.getArticles()[0].getUrl();
sendTweet(title + "\n" + url);
} catch (Exception e) {
throw new ArticleNotFoundException();
}
}
private void sendTweet(String line) {
Twitter twitter = TwitterFactory.getSingleton();
Status status;
try {
status = twitter.updateStatus(line);
System.out.println(status);
} catch (TwitterException e) {;
e.printStackTrace();
}
}
}