add DownloadStream function and isLive condition to main function
This commit is contained in:
parent
2883569ce3
commit
c616c9b5fe
148
main.go
148
main.go
|
@ -60,6 +60,58 @@ func DownloadAweme(filename string, uri string) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
func DownloadStream(filename string, uri string) (bool, error) {
|
||||
var (
|
||||
e error
|
||||
f *os.File
|
||||
rs *http.Response
|
||||
rq *http.Request
|
||||
)
|
||||
/*_, e = os.Stat(filename)
|
||||
if e == nil {
|
||||
return true, nil
|
||||
} else if !os.IsNotExist(e) {
|
||||
return true, e
|
||||
}*/
|
||||
rq, e = http.NewRequest("GET", uri, nil)
|
||||
rq.Header.Set("User-Agent", "okhttp")
|
||||
//rq.Header.Set("Connection", "keep-alive")
|
||||
fmt.Println(rq)
|
||||
rs, e = http.DefaultClient.Do(rq)
|
||||
if e != nil {
|
||||
return false, e
|
||||
//DownloadStream(filename, uri)
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
if rs.StatusCode != http.StatusOK {
|
||||
return false, errors.New(rs.Status)
|
||||
} else if rs.ContentLength == 0 {
|
||||
return false, errors.New("Response body is empty.");
|
||||
}
|
||||
f, e = os.OpenFile(filename, os.O_RDWR | os.O_APPEND, 0700)
|
||||
if e != nil {
|
||||
if os.IsNotExist(e) {
|
||||
f, e = os.Create(filename)
|
||||
if e != nil {
|
||||
return false, e
|
||||
}
|
||||
} else {
|
||||
return false, e
|
||||
}
|
||||
}
|
||||
/*f, e = os.Create(filename)
|
||||
if e != nil {
|
||||
return false, e
|
||||
}*/
|
||||
_, e = io.Copy(f, rs.Body)
|
||||
f.Close()
|
||||
if e != nil {
|
||||
_ = os.Remove(filename)
|
||||
return false, e
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func GenerateBase62(length int) string {
|
||||
var (
|
||||
i int
|
||||
|
@ -574,7 +626,6 @@ func GetUserFavorites(id string, cursor *int64, retry int) (videoids []string, v
|
|||
|
||||
func GetHashtag(keyword string, retry int) (cid string, _error error) {
|
||||
var (
|
||||
//i int
|
||||
e error
|
||||
b []byte
|
||||
d struct {
|
||||
|
@ -593,6 +644,7 @@ func GetHashtag(keyword string, retry int) (cid string, _error error) {
|
|||
"&search_source=challenge" +
|
||||
"&is_pull_refresh=1" +
|
||||
"&hot_search=0")
|
||||
fmt.Println(len(b))
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
|
@ -607,7 +659,7 @@ func GetHashtag(keyword string, retry int) (cid string, _error error) {
|
|||
return d.ChallengeList[0].ChallengeInfo.Id, nil
|
||||
}
|
||||
|
||||
func GetHashtagVideos(top bool, id string, cursor *int, retry int) (videos []string, users []string, usersn []string, more bool, _error error) {
|
||||
func GetHashtagVideos(top bool, id string, cursor *int, retry int) (videos []string, users []string, usersn []string, urls []string, more bool, _error error) {
|
||||
var (
|
||||
i int
|
||||
e error
|
||||
|
@ -616,9 +668,8 @@ func GetHashtagVideos(top bool, id string, cursor *int, retry int) (videos []str
|
|||
HasMore int "json:\"has_more\""
|
||||
Cursor int "json:\"cursor\""
|
||||
AwemeList []struct {
|
||||
//AwemeId string "json:\"aweme_id\""
|
||||
AwemeId string "json:\"aweme_id\""
|
||||
Video struct {
|
||||
AwemeId string "json:\"aweme_id\""
|
||||
PlayAddr struct {
|
||||
UrlList []string "json:\"url_list\""
|
||||
} "json:\"play_addr\""
|
||||
|
@ -646,7 +697,7 @@ func GetHashtagVideos(top bool, id string, cursor *int, retry int) (videos []str
|
|||
"&type=5")
|
||||
}
|
||||
if e != nil {
|
||||
return nil, nil, nil, false, e
|
||||
return nil, nil, nil, nil, false, e
|
||||
}
|
||||
e = json.Unmarshal(b, &d)
|
||||
if e != nil {
|
||||
|
@ -654,20 +705,20 @@ func GetHashtagVideos(top bool, id string, cursor *int, retry int) (videos []str
|
|||
Ip = GeneratePublicIp()
|
||||
return GetHashtagVideos(top, id, cursor, retry + 1)
|
||||
}
|
||||
return nil, nil, nil, false, e
|
||||
return nil, nil, nil, nil, false, e
|
||||
}
|
||||
videos = make([]string, 0)
|
||||
users = make([]string, 0)
|
||||
usersn = make([]string, 0)
|
||||
urls = make([]string, 0)
|
||||
for i = 0; i < len(d.AwemeList); i++ {
|
||||
//if len(d.AwemeList[i].Video.PlayAddr.UrlList) > 0 {
|
||||
videos = append(videos, d.AwemeList[i].Video.AwemeId, d.AwemeList[i].Video.PlayAddr.UrlList[0])
|
||||
//}
|
||||
videos = append(videos, d.AwemeList[i].AwemeId)
|
||||
users = append(users, d.AwemeList[i].UserInfo.Uid)
|
||||
usersn = append(usersn, d.AwemeList[i].UserInfo.UniqueId)
|
||||
urls = append(urls, d.AwemeList[i].Video.PlayAddr.UrlList[0])
|
||||
}
|
||||
*cursor += 20
|
||||
return videos, users, usersn, d.HasMore != 0, nil
|
||||
return videos, users, usersn, urls, d.HasMore != 0, nil
|
||||
}
|
||||
|
||||
func GetMusicVideos(top bool, id string, cursor *int, retry int) (ids []string, videos []string, users []string, usersn []string, more bool, _error error) {
|
||||
|
@ -1065,13 +1116,7 @@ func main() {
|
|||
"music/hashtag [top] - list recent or popular videos using song or hashtag\n" +
|
||||
"examples:\n" +
|
||||
" $ ./" + filepath.Base(os.Args[0]) + " @nyannyancosplay ping\n" +
|
||||
//" name: lorengray\n" +
|
||||
//" id: 934514\n" +
|
||||
//" videos: 2294\n" +
|
||||
//" likes: 2147483647\n" +
|
||||
//" followers: 39339647 20\n" +
|
||||
//" ig: loren\n" +
|
||||
//" yt: UCRYe4G08okte96rmocFNU1g\n $\n" +
|
||||
" $ ./" + filepath.Base(os.Args[0]) + " \"#xyzcba\" top\n" +
|
||||
" $ ./" + filepath.Base(os.Args[0]) + " <( ./" + filepath.Base(os.Args[0]) + " `./" + filepath.Base(os.Args[0]) + " $VIDEO_ID video | grep 'music id' | awk '{print $3}'` music | awk '{print $1}' )\n" +
|
||||
"*usage over TOR is recommended\n" +
|
||||
"*the file's name cannot begin with \"@\" or contain only digits (^(?:0|(:?[1-9][0-9]*))$)\n" +
|
||||
|
@ -1080,15 +1125,15 @@ func main() {
|
|||
"6648394283958349583 4992348 user093092394 http://v16.muscdn.com/...\n" +
|
||||
"(video id) (user id) (user name) (video url)\n" +
|
||||
"*comment lists are output likewise like this:\n" +
|
||||
"6693429093034095344 loomaster2020 open bob\n" +
|
||||
"6693429093034095344 loomaster2020 open bob and vagene\n" +
|
||||
"(user id) (user name) (text)"
|
||||
top, isMusic, isVideo, isHashtag, isLiked, isList, isComments, o, m, ru, isFi, isFo, isp, inr, ss bool
|
||||
top, isMusic, isVideo, isHashtag, isLiked, isList, isComments, isLive, o, m, ru, isFi, isFo, isp, inr, ss bool
|
||||
i, j, y, nfo, nfi, nl, k, com, dl, view, sh int
|
||||
c, u, ctime int64
|
||||
d time.Time
|
||||
id, n, g, yt, x, htid, vid, vauth, vvid, playurl, durl, mid, mtitle, mauth, desc string
|
||||
ig map[string]bool
|
||||
ov, nv, v, fi, fin, fo, fon, htv, htu, htun, mvid, muv, muu, muun, likeid, likeurl, likaid, likan, comauth, comauthn, comt []string
|
||||
ov, nv, v, fi, fin, fo, fon, htv, htu, htun, htuu, mvid, muv, muu, muun, likeid, likeurl, likaid, likan, comauth, comauthn, comt []string
|
||||
e error
|
||||
f *os.File
|
||||
l sync.Mutex
|
||||
|
@ -1124,6 +1169,10 @@ func main() {
|
|||
isList = true
|
||||
} else if os.Args[2] == "comments" {
|
||||
isComments = true
|
||||
} else if os.Args[2] == "live" {
|
||||
isLive = true
|
||||
//} else if os.Args[2] == "top" {
|
||||
// top = true
|
||||
} else {
|
||||
d, e = time.Parse(DATE_FORMAT, os.Args[2])
|
||||
if e != nil {
|
||||
|
@ -1163,9 +1212,18 @@ func main() {
|
|||
m = true
|
||||
k = 0
|
||||
for ru && m {
|
||||
htv, htu, htun, m, e = GetHashtagVideos(false, htid, &k, 0)
|
||||
for i = 0; i < len(htun); i++ {
|
||||
fmt.Println(htv[i] + "\t" + htu[i] + "\t" + htun[i])
|
||||
/*if top == true {
|
||||
htv, htu, htun, htuu, m, e = GetHashtagVideos(true, htid, &k, 0)
|
||||
} else {
|
||||
htv, htu, htun, htuu, m, e = GetHashtagVideos(false, htid, &k, 0)
|
||||
}
|
||||
for i = 0; i < len(htv); i++ {
|
||||
fmt.Println(htv[i] + "\t" + htu[i] + "\t" + htun[i] + "\t" + htuu[i])
|
||||
}
|
||||
}*/
|
||||
htv, htu, htun, htuu, m, e = GetHashtagVideos(false, htid, &k, 0)
|
||||
for i = 0; i < len(htv); i++ {
|
||||
fmt.Println(htv[i] + "\t" + htu[i] + "\t" + htun[i] + "\t" + htuu[i])
|
||||
}
|
||||
}
|
||||
return
|
||||
|
@ -1176,12 +1234,12 @@ func main() {
|
|||
k = 0
|
||||
for ru && m {
|
||||
if top == true {
|
||||
htv, htu, htun, m, e = GetHashtagVideos(true, htid, &k, 0)
|
||||
htv, htu, htun, htuu, m, e = GetHashtagVideos(true, htid, &k, 0)
|
||||
} else {
|
||||
htv, htu, htun, m, e = GetHashtagVideos(false, htid, &k, 0)
|
||||
htv, htu, htun, htuu, m, e = GetHashtagVideos(false, htid, &k, 0)
|
||||
}
|
||||
for i = 0; i < len(htun); i++ {
|
||||
fmt.Println(htv[i] + "\t" + htu[i] + "\t" + htun[i])
|
||||
for i = 0; i < len(htv); i++ {
|
||||
fmt.Println(htv[i] + "\t" + htu[i] + "\t" + htun[i] + "\t" + htuu[i])
|
||||
}
|
||||
}
|
||||
return
|
||||
|
@ -1373,6 +1431,42 @@ func main() {
|
|||
}
|
||||
return
|
||||
}
|
||||
if isLive {
|
||||
if c != 0 {
|
||||
n = RequestStream(c)
|
||||
if len(n) > 0 {
|
||||
n = GetFlvStream(n)
|
||||
if len(n) > 0 {
|
||||
/*go func(id string, uri string) {
|
||||
var (
|
||||
r bool
|
||||
e error
|
||||
)
|
||||
r, e = DownloadStream(id + ".flv", uri)
|
||||
fmt.Println(e)
|
||||
if e != nil {
|
||||
l.Lock()
|
||||
fmt.Println(id, "failed")
|
||||
fmt.Fprintln(os.Stderr, e.Error())
|
||||
l.Unlock()
|
||||
}
|
||||
if !r {
|
||||
fmt.Println("livestream end")
|
||||
}
|
||||
|
||||
}(strconv.FormatInt(c, 10), n)*/
|
||||
fmt.Println("recording livestream to " + strconv.FormatInt(c, 10) + ".flv")
|
||||
fmt.Println("press ctrl-c at any time to stop")
|
||||
DownloadStream(strconv.FormatInt(c, 10) + ".flv", n)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Could not get livestream URL")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("User is not live")
|
||||
}
|
||||
return
|
||||
}
|
||||
fmt.Println("name:\t\t", n)
|
||||
fmt.Println("id:\t\t", id)
|
||||
fmt.Println("videos:\t\t", i)
|
||||
|
|
Loading…
Reference in New Issue