package main import ( "bufio" "encoding/json" "log" "net/http" "strings" "time" ) func StreamMastodon(endpoint string, o *RunningInstance) { stream_client := http.Client{} var oauthData OAuth var retry bool for { api_timeline := "https://" + endpoint + "/api/v1/streaming/public" req, err := http.NewRequest("GET", api_timeline, nil) if err != nil { log.Print("Unable to create new request") return } for _, extaccount := range settings.Externalaccounts { if extaccount.Endpoint == endpoint { get_client(endpoint, o) err = get_client(endpoint, o) if err != nil { log.Fatal("Unable to register client: ", err) } oauthData, err = oauth_login(endpoint, o, extaccount.Username, extaccount.Password) if err != nil { log.Print("Unable to login: ", err) return } req.Header.Add("Authorization", oauthData.Access_token) } } resp, err := stream_client.Do(req) if err != nil { log.Print("Unable to stream "+api_timeline+": ", err) return } defer resp.Body.Close() ri_mutex.Lock() m := runninginstances[endpoint] m.Status = RUNNING m.LastRun = "Streaming" runninginstances[endpoint] = m ri_mutex.Unlock() s := bufio.NewScanner(resp.Body) var name string for s.Scan() { line := s.Text() token := strings.SplitN(line, ":", 2) var newpost ReportPost if len(token) != 2 { continue } switch strings.TrimSpace(token[0]) { case "event": name = strings.TrimSpace(token[1]) continue case "data": switch name { case "update": jsondata := token[1][1:] err := json.Unmarshal([]byte(jsondata), &newpost) if err != nil { continue log.Fatal("Unable to unmarshal with error: ", err) } retry = true default: continue } default: continue } go check_post(newpost.Uri) matchset := re.FindStringSubmatch(newpost.Uri) if matchset != nil { newinstance := matchset[1] go CheckInstance(newinstance, endpoint) } } if retry == true { time.Sleep(time.Minute * 30) } else { break } } }