-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (54 loc) · 1.44 KB
/
main.go
File metadata and controls
75 lines (54 loc) · 1.44 KB
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
package main
import (
"bufio"
"fmt"
"os"
"time"
"encoding/json"
"math/rand"
"github.com/smmd/giveaway-2021/repository"
)
func main() {
var filePath string
userNames := make([]string, 0)
fmt.Print("Enter the file name: ")
_, err := fmt.Scanf("%s", &filePath)
if err != nil {
panic(fmt.Errorf("could not read Path: %w", err))
}
file, err := os.Open(filePath)
if err != nil {
panic(fmt.Errorf("failed to open file error: %v", err))
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var text []string
for scanner.Scan() {
text = append(text, scanner.Text())
}
file.Close()
for _, line := range text {
userData := &repository.Response{}
byt := []byte(line)
err = json.Unmarshal(byt, userData)
if err != nil {
panic(fmt.Errorf("could not process line file error: %v", err))
} else {
instructions := userData.Data.FavoritersTimeline.Timeline.Instructions
for _, instruction := range instructions {
for _, entry := range instruction.Entries {
user := entry.Content.ItemContent.UserResults.Result.Legacy
if len(user.ScreenName) > 0 && user.FollowedBy {
userNames = append(userNames, user.ScreenName)
}
}
}
}
}
followers := len(userNames)
rand.Seed(time.Now().Unix())
n := rand.Intn(followers)
fmt.Printf("\nNumber of users that meet the rule: %v\n\n", len(userNames))
fmt.Printf("Users: %#v\n\n", userNames)
fmt.Printf("Winner: %v\n\n", userNames[n])
}