-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversus.go
More file actions
111 lines (101 loc) · 2.36 KB
/
Copy pathversus.go
File metadata and controls
111 lines (101 loc) · 2.36 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
)
type Actor struct {
ID int
Login string
GravatarID string
URL string
AvatarURL string
}
type Repo struct {
ID int
Name string
URL string `json:"url"`
}
type Payload struct {
PushID int
Size int
DistinctSize int
Ref string
Head string
Before string
Commits []Commits
}
type Commits struct {
SHA string
Message string
Distinct string
URL string
}
type Data struct {
ID string `json:"id"`
Type string `json:"type"`
Actor Actor `json:"actor"`
Repo Repo `json:"repo"`
PayLoad Payload `json:"payload"`
Public string `json:"public"`
CreatedAt string `json:"created_at"`
}
func main() {
fmt.Println("ユーザー名を入力してください")
fmt.Print("YourName: ")
player1 := commitCount()
fmt.Print("Player: ")
player2 := commitCount()
if player1 > player2 {
fmt.Println("Win : +", player1-player2)
fmt.Printf("MyCommits: %d\nPlayer Commits: %d", player1, player2)
}
if player1 < player2 {
fmt.Println("Lose : -", player2-player1)
fmt.Printf("MyCommits: %d\nPlayer Commits: %d", player1, player2)
}
if player1 == player2 {
fmt.Println("DROW")
fmt.Printf("MyCommits: %d\nPlayer Commits: %d", player1, player2)
}
}
// commitCount is counts the number of commits .
func commitCount() (commitTotal int) {
player := getUserName() + "/events"
url := "https://api.github.com/users/" + player
req, _ := http.NewRequest("GET", url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var d []Data
dec.Decode(&d)
var count = 0
for _, json := range d {
// 日付文字列パース
var ts = strings.Replace(json.CreatedAt, "T", " ", 1)
ts = strings.Replace(ts, "Z", " UTC", 1)
// JST置換
t, _ := time.Parse("2006-01-02 15:04:05 MST", ts)
jst := time.FixedZone("Asia/Tokyo", 9*60*60)
tJst := t.In(jst)
// 日付比較
nowJst := time.Now()
if tJst.Format("2006-01-02") == nowJst.Format("2006-01-02") &&
json.Type == "PushEvent" {
count += len(json.PayLoad.Commits)
}
}
return count
}
// getWord is type text UserName .
func getUserName() (stringReturned string) {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
stringReturned = scanner.Text()
return
}