-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
90 lines (80 loc) · 2.31 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"log"
"sort"
"strconv"
"time"
twitterscraper "github.com/n0madic/twitter-scraper"
)
type (
Tweet struct {
Hashtags []string
HTML string
ID string
Likes int
PermanentURL string
Photos []string
Replies int
Retweets int
Text string
Timestamp int64
UserID string
Username string
Videos []twitterscraper.Video
}
)
const TWEETS_FILE_PATH = "./web-ui/assets/tweets.json"
// Start date for search for tweets
var START_DATE = time.Date(2022, 9, 16, 0, 0, 0, 0, time.Local)
func main() {
// Load stored tweets ids from file (fetched before)
var err error
var tweetsID []string
if empty, err := isFileEmpty(TWEETS_FILE_PATH); err != nil {
log.Fatalf("Error occurred during check file emptiness - %s", err.Error())
} else if !empty {
tweetsID, err = loadTweetsIDFromFile(TWEETS_FILE_PATH)
if err != nil {
log.Fatalf("Error occurred during load tweets id form file - %s", err.Error())
}
}
fmt.Println("Number of stored tweets is: ", len(tweetsID))
// Fetch needed tweets
totalFetched, tweets, err := fetchTweets(tweetsID)
if err != nil {
// TODO: there is should be a better error handling
log.Fatalf("Error occurred during fetch tweets - %s", err.Error())
}
// Remove duplicate tweets
tweets = removeDuplicateValues(tweets)
fmt.Println("Total records received: " + strconv.Itoa(totalFetched))
fmt.Println("Total records accepted: " + strconv.Itoa(len(tweets)))
// Sort tweets by likes and retweets
sort.Sort(ByLikeAndRetweet(tweets))
// Prune tweets
pruneTweets := []*Tweet{}
for _, tweet := range tweets {
pruneTweets = append(pruneTweets, &Tweet{
Hashtags: tweet.Hashtags,
HTML: tweet.HTML,
ID: tweet.ID,
Likes: tweet.Likes,
PermanentURL: tweet.PermanentURL,
Photos: tweet.Photos,
Replies: tweet.Replies,
Retweets: tweet.Retweets,
Text: tweet.Text,
Timestamp: tweet.Timestamp,
UserID: tweet.UserID,
Username: tweet.Username,
Videos: tweet.Videos,
})
}
// Convert data to Json and write it in file
err = dumpTweetsToFile(TWEETS_FILE_PATH, pruneTweets)
if err != nil {
log.Fatalf("Error occurred during dump/write tweets in file - %s", err.Error())
}
fmt.Println("Finished")
}