Add dynamic account name and link to account
This commit is contained in:
parent
d77383be08
commit
643c644e33
57
server.go
57
server.go
|
@ -30,6 +30,12 @@ type Config struct {
|
|||
BaseURL string `json:"base_url"`
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"display_name"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
config := getConfig()
|
||||
|
||||
|
@ -153,16 +159,26 @@ func handleConn(conn *tls.Conn, baseURL string) {
|
|||
return
|
||||
}
|
||||
|
||||
account, err := getAccount(baseURL, path)
|
||||
blogs := getBlog(baseURL, path)
|
||||
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Picasoft account toots\n")
|
||||
if err != nil || blogs == nil {
|
||||
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Toots for " + account.Name + " account\n")
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, blog := range blogs {
|
||||
date := "```\n* Posted at " + blog.Date + "\n```\n"
|
||||
date := "\n```\n* Posted at " + blog.Date + " *\n```\n"
|
||||
|
||||
text := blog.Content + "\n"
|
||||
text = strings.ReplaceAll(text, "<p>", "")
|
||||
|
@ -192,6 +208,12 @@ func handleConn(conn *tls.Conn, baseURL string) {
|
|||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(conn, "=> " + account.Url + " Go to " + account.Name + " account")
|
||||
if err != nil {
|
||||
log.Println("add link: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getBlog(baseURL, account string) []Blog {
|
||||
|
@ -223,3 +245,34 @@ func getBlog(baseURL, account string) []Blog {
|
|||
|
||||
return blogs
|
||||
}
|
||||
|
||||
func getAccount(baseURL, accountId string) (Account, error) {
|
||||
if baseURL == "" || accountId == "" {
|
||||
log.Println("baseURL or account is empty")
|
||||
return Account{}, fmt.Errorf("baseURL or account is empty")
|
||||
}
|
||||
|
||||
resp, err := http.Get(baseURL + "/api/v1/accounts/" + accountId)
|
||||
if err != nil {
|
||||
log.Println("Mastodon API request: %s", err)
|
||||
return Account{}, fmt.Errorf("API request failed")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
log.Println("Mastodon API response: %s", resp.Status)
|
||||
return Account{}, fmt.Errorf("API response is not 200")
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Println("Mastodon response body: %s", err)
|
||||
return Account{}, fmt.Errorf("Failed to read response")
|
||||
}
|
||||
|
||||
var account Account
|
||||
json.Unmarshal(body, &account)
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue