-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (167 loc) · 5.12 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
)
type AuthResponse struct {
Token string `json:"token"`
}
type Episode struct {
UUID string `json:"uuid"`
URL string `json:"url"`
Published string `json:"published"`
Duration int `json:"duration"`
FileType string `json:"fileType"`
Title string `json:"title"`
Size string `json:"size"`
PlayingStatus int `json:"playingStatus"`
PlayedUpTo int `json:"playedUpTo"`
Starred bool `json:"starred"`
PodcastUUID string `json:"podcastUuid"`
PodcastTitle string `json:"podcastTitle"`
EpisodeType string `json:"episodeType"`
EpisodeSeason int `json:"episodeSeason"`
EpisodeNumber int `json:"episodeNumber"`
IsDeleted bool `json:"isDeleted"`
Author string `json:"author"`
Bookmarks []string `json:"bookmarks"`
}
type Data struct {
Total int `json:"total"`
Episodes []Episode `json:"episodes"`
}
func main() {
//Reading username & password
email := os.Getenv("POCKETCASTS_EMAIL")
password := os.Getenv("POCKETCASTS_PASSWORD")
emailRegex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
if email == "" || password == "" {
fmt.Println("Email & Password are mandatory!")
return
}
if !isValidEmail(email, emailRegex) {
fmt.Println("Invalid email format.")
return
}
token, err := authenticate(email, password)
if err != nil {
log.Fatalf("Authentication error: %s", err)
return
}
fmt.Println("Fetching listening history.")
historyData, err := fetchUserHistory(token)
if err != nil {
log.Fatalf("Error fetching listen history: %s", err)
return
}
err = ParseJson(string(historyData))
if err != nil {
fmt.Println(string(historyData))
log.Fatalf("Failed parsing history data: %s", err)
}
}
func isValidEmail(email, regex string) bool {
match, err := regexp.MatchString(regex, email)
if err != nil {
log.Fatal("Failed to match Email regex.")
}
return match
}
func authenticate(email, password string) (string, error) {
authUrl := "https://api.pocketcasts.com/user/login"
payload := map[string]string{
"email": email,
"password": password,
"scope": "webplayer",
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return "Failed creating payload!", err
}
req, err := http.NewRequest("POST", authUrl, bytes.NewReader(payloadBytes))
if err != nil {
return "Faled to make request!", err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "Error sending request:", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("authentication request failed with status code: %s", resp.Status)
}
var authResponse AuthResponse
err = json.NewDecoder(resp.Body).Decode(&authResponse)
if err != nil {
return "Failed decode response: ", err
}
return authResponse.Token, nil
}
func fetchUserHistory(token string) ([]byte, error) {
historyUrl := "https://api.pocketcasts.com/user/history"
historyReq, err := http.NewRequest("POST", historyUrl, nil)
if err != nil {
return nil, err
}
historyReq.Header.Add("Authorization", "Bearer "+token)
client := &http.Client{}
historyResp, err := client.Do(historyReq)
if err != nil {
return nil, err
}
defer historyResp.Body.Close()
if historyResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("user history request failed with status code: %s", historyResp.Status)
}
historyData, err := io.ReadAll(historyResp.Body)
if err != nil {
return nil, err
}
return historyData, nil
}
func ParseJson(historydata string) error {
var data Data
err := json.Unmarshal([]byte(historydata), &data)
if err != nil {
log.Fatal("Unable to marshall json")
}
// Get the top 5 episodes
topEpisodes := data.Episodes[:5]
var top5content string
// Write the top 5 episodes to file
for _, episode := range topEpisodes {
top5content += fmt.Sprintf("%s: [%s](%s)\n\n", episode.PodcastTitle, episode.Title, episode.URL)
}
fmt.Printf("Printing top5 Conent: \n%s\n", top5content)
readmePath := "README.md"
readmeContent, err := os.ReadFile(readmePath)
if err != nil {
log.Fatalf("Error reading README.md: %v", err)
return err
}
// Define the section markers
startMarker := "<!-- PODCAST-LIST:START -->"
endMarker := "<!-- PODCAST-LIST:END -->"
// Create a regular expression pattern that matches the content between the markers
pattern := regexp.MustCompile(startMarker + `[\s\S]*?` + endMarker)
//Replace content
// updatedContent := strings.Replace(string(readmeContent), startMarker+"\n"+"<!-- PODCAST-LIST:END -->", startMarker+"\n"+top5content+"\n"+endMarker, -1)
// Replace the content between markers with the new content
updatedContent := pattern.ReplaceAllString(string(readmeContent), startMarker+"\n"+top5content+"\n"+endMarker)
// Write the updated content back to the README.md file
err = os.WriteFile(readmePath, []byte(updatedContent), 0644)
if err != nil {
fmt.Println("Error writing to README.md:", err)
return err
}
fmt.Println("README.md updated successfully.")
return nil
}