This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshots.groovy
60 lines (51 loc) · 1.73 KB
/
screenshots.groovy
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@Grab(group="org.twitter4j", module="twitter4j-core", version="[3.0,)")
import twitter4j.*
String username = "<twitter-user>"
int numberOfTweetsToSearch = 25
File directory = new File("downloads");
//setup download directory
if(!directory.exists()) {
println "Creating directory ${directory.absolutePath}"
directory.mkdirs()
}
//do the work
filterTweets(readFromTwitter(username, numberOfTweetsToSearch)).each{tweet ->
downloadImage(tweet, directory)
}
//twitter methods
List<Status> readFromTwitter(String username, int count) {
Twitter twitter = TwitterFactory.getSingleton()
Paging paging = new Paging(1, count);
return twitter.getUserTimeline(username, paging);
}
List<SwitchTweet> filterTweets(List<Status> statuses) {
return statuses.findAll{fromNintendo(it)}?.collect { status ->
SwitchTweet tweet = new SwitchTweet()
tweet.tweetDate = status.createdAt
tweet.imageUrl = status.mediaEntities?.first()?.mediaURL
if(tweet.imageUrl) {
return tweet
}
}.findAll{it} //just to make sure there are no null values
}
boolean fromNintendo(Status status) {
return status.source.contains("Nintendo Switch Share")
}
//download method
void downloadImage(SwitchTweet tweet, File directory) {
String filename = tweet.tweetDate.format("yyyy-MM-dd_HH-mm-ss") + ".jpg"
def file = new File(filename, directory)
if(file.exists()){
println "Already downloaded $filename"
} else {
println "Downloading to $filename"
def fileOutputStream = file.newOutputStream()
fileOutputStream << new URL("${tweet.imageUrl}:large").openStream()
fileOutputStream.close()
}
}
//helper class
class SwitchTweet {
String imageUrl
Date tweetDate
}