2020-12-11 17:20:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-12-22 17:35:05 +00:00
|
|
|
// "crypto/sha1"
|
2020-12-17 04:23:25 +00:00
|
|
|
"encoding/json"
|
2020-12-22 17:35:05 +00:00
|
|
|
// "fmt"
|
|
|
|
// "html"
|
2020-12-11 17:20:44 +00:00
|
|
|
"log"
|
2020-12-17 04:23:25 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-12-11 17:20:44 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 06:06:32 +00:00
|
|
|
func StreamMastodon(endpoint string, reportPostChan chan ReportPost) {
|
2020-12-11 17:20:44 +00:00
|
|
|
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-17 04:23:25 +00:00
|
|
|
// use_auth = true
|
2020-12-13 07:38:03 +00:00
|
|
|
get_client(endpoint, &http_client)
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-17 04:23:25 +00:00
|
|
|
client_id, client_secret, err = get_client(endpoint, &http_client)
|
2020-12-11 17:20:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
// This needs to updated with the time
|
2020-12-17 04:23:25 +00:00
|
|
|
// last_refresh := time.Now().Unix()
|
2020-12-11 17:20:44 +00:00
|
|
|
_ = time.Now().Unix()
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", oauthData.Access_token)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http_client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error occured for " + api_timeline)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-12-13 08:26:50 +00:00
|
|
|
ri_mutex.Lock()
|
|
|
|
m := runninginstances[endpoint]
|
|
|
|
m.Status = RUNNING
|
|
|
|
m.LastRun = "Streaming"
|
2020-12-17 01:15:17 +00:00
|
|
|
runninginstances[endpoint] = m
|
2020-12-13 08:26:50 +00:00
|
|
|
ri_mutex.Unlock()
|
|
|
|
|
2020-12-11 17:20:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:35:05 +00:00
|
|
|
go check_post(newpost.Uri)
|
2020-12-11 17:20:44 +00:00
|
|
|
|
|
|
|
at_sign := strings.Index(newpost.Account.Acct, "@")
|
2020-12-17 01:15:17 +00:00
|
|
|
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
|
2020-12-18 06:06:32 +00:00
|
|
|
go StartInstance(newinstance, reportPostChan)
|
2020-12-17 04:23:25 +00:00
|
|
|
}
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-17 04:23:25 +00:00
|
|
|
ri_mutex.Unlock()
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-13 08:26:50 +00:00
|
|
|
|
|
|
|
ri_mutex.Lock()
|
2020-12-17 01:15:17 +00:00
|
|
|
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()
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|