fedilogue/stream.go

114 lines
2.4 KiB
Go
Raw Normal View History

package main
import (
"bufio"
2020-12-17 04:23:25 +00:00
"encoding/json"
"log"
2020-12-17 04:23:25 +00:00
"net/http"
"strings"
"time"
)
func StreamMastodon(endpoint string) {
http_client := http.Client{}
var client_id string
var client_secret string
var oauthData OAuth
var err error
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 {
2020-12-13 07:38:03 +00:00
get_client(endpoint, &http_client)
2020-12-17 04:23:25 +00:00
client_id, client_secret, err = get_client(endpoint, &http_client)
if err != nil {
log.Fatal("Unable to register client: ", err)
}
oauthData, err = oauth_login(endpoint, extaccount.Username, extaccount.Password, client_id, client_secret)
if err != nil {
log.Print("Unable to login: ", err)
return
}
req.Header.Add("Authorization", oauthData.Access_token)
}
}
resp, err := http_client.Do(req)
if err != nil {
2020-12-23 08:20:03 +00:00
log.Print("Unable to stream " + api_timeline + ": ", err)
return
}
defer resp.Body.Close()
2020-12-13 08:26:50 +00:00
ri_mutex.Lock()
m := runninginstances[endpoint]
m.Status = RUNNING
m.LastRun = "Streaming"
runninginstances[endpoint] = m
2020-12-13 08:26:50 +00:00
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":
jsoner := token[1][1:]
err := json.Unmarshal([]byte(jsoner), &newpost)
if err != nil {
log.Fatal("Unable to unmarshal with error: ", err)
}
default:
continue
}
default:
continue
}
go check_post(newpost.Uri)
at_sign := strings.Index(newpost.Account.Acct, "@")
newinstance := newpost.Account.Acct[at_sign+1:]
2020-12-17 04:23:25 +00:00
if settings.Crawl == true && stringexists(endpoint, settings.Banned) == false {
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
runninginstances[newinstance] = m
go StartInstance(newinstance)
2020-12-17 04:23:25 +00:00
}
2020-12-17 04:23:25 +00:00
ri_mutex.Unlock()
}
}
2020-12-13 08:26:50 +00:00
ri_mutex.Lock()
m = runninginstances[endpoint]
m.LastRun = time.Now().Format(time.RFC3339)
2020-12-13 08:26:50 +00:00
m.Status = STREAM_ENDED
runninginstances[endpoint] = m
ri_mutex.Unlock()
}