-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
119 lines (106 loc) · 3.74 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/ChimeraCoder/anaconda"
)
// TwitterCreds : struct for Twitter credentials
type TwitterCreds struct {
CustomerKey string
CustomerSecret string
AccessToken string
AccessTokenSecret string
}
// Periscope : Struct of json, meant for extracting the "LhlsURL" variable
type Periscope struct {
LhlsURL string `json:"lhls_url"`
ReplayURL string `json:"replay_url"`
}
func main() {
twitterCreds := TwitterCreds{ // Twitter crendentials
CustomerKey: os.Getenv("TWITTER_CUSTOMERKEY"),
CustomerSecret: os.Getenv("TWITTER_CUSTOMERSECRET"),
AccessToken: os.Getenv("TWITTER_ACCESSTOKEN"),
AccessTokenSecret: os.Getenv("TWITTER_ACCESSTOKENSECRET"),
}
anaconda.SetConsumerKey(twitterCreds.CustomerKey) // Apply Twitter Consumer Key
anaconda.SetConsumerSecret(twitterCreds.CustomerSecret) // Apply Twitter Consumer Secret
api := anaconda.NewTwitterApi(twitterCreds.AccessToken, twitterCreds.AccessTokenSecret) // Twitter API
v := url.Values{}
v.Set("count", "500") // Set the "count" key with a value
searchResult, err := api.GetSearch("periscope", v) // Search Twitter for 250 "periscope" results
if err != nil {
panic(err)
}
var collectedURLs []string
for _, status := range searchResult.Statuses {
for _, entities := range status.Entities.Urls {
switch strings.Contains(entities.Expanded_url, "https://www.pscp.tv/w/") {
case true:
broadcastID := strings.SplitAfter(entities.Expanded_url, "https://www.pscp.tv/w/")
apiURL := "https://api.periscope.tv/api/v2/accessVideoPublic?broadcast_id=" + broadcastID[1]
switch strings.Contains(apiURL, "?t") {
case true:
removeTimeRef := strings.SplitAfter(apiURL, "?t")
requestURLs := removeTimeRef[0][:len(removeTimeRef[0])-2]
getStreamURL := acquireStreamURLs(requestURLs)
collectedURLs = append(collectedURLs, getStreamURL)
case false:
getStreamURL := acquireStreamURLs(apiURL)
collectedURLs = append(collectedURLs, getStreamURL)
}
}
}
}
encounteredFiles := map[string]bool{} // incoming files
nodupsCollectedURLs := []string{} // duplicates removed
for v := range collectedURLs {
if encounteredFiles[collectedURLs[v]] == true {
} else {
encounteredFiles[collectedURLs[v]] = true
nodupsCollectedURLs = append(nodupsCollectedURLs, collectedURLs[v])
}
}
dir := os.Args[1] // User-defined directory to search
currentTime := time.Now() // Get current time
formattedTime := currentTime.Format("2006-01-02-15:04:05") // Format time with y-m-d-h-m-s
for num := 0; num < len(nodupsCollectedURLs); num++ {
newWavFile := dir + "/" + formattedTime + "-" + strconv.Itoa(num+1) + ".wav"
cmd := exec.Command("ffmpeg", "-ss", "0", "-t", "10", "-i", nodupsCollectedURLs[num], "-strict", "-2", "-ac", "1", newWavFile, "-nostdin", "-nostats")
cmd.Run()
}
}
func acquireStreamURLs(url string) string {
var outputURL string // Variable for return string
resp, err := http.Get(url) // GET request
if err != nil {
fmt.Println("FAIL")
}
defer resp.Body.Close()
body, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
fmt.Println("FAIL")
}
var record Periscope
json.Unmarshal(body, &record)
switch strings.Contains(record.ReplayURL, "?type=replay") { // Switch between replay and live streams
case true:
if record.ReplayURL != "" {
replayURL := record.ReplayURL[:len(record.ReplayURL)-12]
outputURL = replayURL
}
case false:
if record.LhlsURL != "" {
outputURL = record.LhlsURL
}
}
return outputURL
}