@ -8,79 +8,22 @@
package liveurls
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
)
type Douyin struct {
Shorturl string
Rid string
}
func GetRoomId ( url string ) any {
client := & http . Client {
CheckRedirect : func ( req * http . Request , via [ ] * http . Request ) error {
return http . ErrUseLastResponse
} ,
}
r , _ := http . NewRequest ( "GET" , url , nil )
r . Header . Add ( "user-agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" )
r . Header . Add ( "authority" , "v.douyin.com" )
resp , err := client . Do ( r )
if err != nil {
return err
}
defer resp . Body . Close ( )
reurl := resp . Header . Get ( "Location" )
reg := regexp . MustCompile ( ` \d { 19} ` )
res := reg . FindAllStringSubmatch ( reurl , - 1 )
if res == nil {
return nil
}
return res [ 0 ] [ 0 ]
}
func ( d * Douyin ) GetRealurl ( ) any {
var mediamap map [ string ] map [ string ] map [ string ] map [ string ] any
var roomstatus map [ string ] map [ string ] map [ string ] int
var roomid string
if str , ok := GetRoomId ( d . Shorturl ) . ( string ) ; ok {
roomid = str
} else {
return nil
}
client := & http . Client { }
r , _ := http . NewRequest ( "GET" , "https://webcast.amemv.com/webcast/room/reflow/info/?type_id=0&live_id=1&room_id=" + roomid + "&sec_user_id=&app_id=1128" , nil )
r . Header . Add ( "Accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" )
r . Header . Add ( "user-agent" , "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1" )
resp , _ := client . Do ( r )
defer resp . Body . Close ( )
body , _ := io . ReadAll ( resp . Body )
str , _ := url . QueryUnescape ( string ( body ) )
json . Unmarshal ( [ ] byte ( str ) , & mediamap )
json . Unmarshal ( [ ] byte ( str ) , & roomstatus )
var realurl any
if mediaslice , ok := mediamap [ "data" ] [ "room" ] [ "stream_url" ] [ "hls_pull_url_map" ] . ( map [ string ] any ) ; ok {
for k , v := range mediaslice {
if k == "FULL_HD1" {
realurl = fmt . Sprintf ( "%v" , v )
}
if roomstatus [ "data" ] [ "room" ] [ "status" ] != 2 {
realurl = nil
}
}
}
return realurl
Stream string
Rid string
}
func ( d * Douyin ) GetDouYinUrl ( ) any {
liveurl := "https://live.douyin.com/" + d . Rid
client := & http . Client { }
var mediamap map [ string ] map [ string ] any
re , _ := http . NewRequest ( "GET" , liveurl , nil )
re . Header . Add ( "user-agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" )
re . Header . Add ( "upgrade-insecure-requests" , "1" )
@ -89,26 +32,39 @@ func (d *Douyin) GetDouYinUrl() any {
oreg := regexp . MustCompile ( ` (?i)__ac_nonce=(.*?); ` )
ores := oreg . FindStringSubmatch ( oresp . Header [ "Set-Cookie" ] [ 0 ] )
r , _ := http . NewRequest ( "GET" , liveurl , nil )
cookie1 := & http . Cookie { Name : "__ac_nonce" , Value : ores [ 1 ] }
r . AddCookie ( cookie1 )
cookie := & http . Cookie { Name : "__ac_nonce" , Value : ores [ 1 ] }
r . AddCookie ( cookie )
r . Header . Add ( "user-agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" )
r . Header . Add ( "upgrade-insecure-requests" , "1" )
resp , _ := client . Do ( r )
defer resp . Body . Close ( )
body , _ := io . ReadAll ( resp . Body )
str , _ := url . QueryUnescape ( string ( body ) )
reg := regexp . MustCompile ( ` (?i)\"roomid\"\:\"([0-9]+)\" ` )
res := reg . FindAllStringSubmatch ( str , - 1 )
if res == nil {
return nil
}
nnreg := regexp . MustCompile ( ` (?i)\"id_str\":\" ` + res [ 0 ] [ 1 ] + ` (?i)\"[\s\S]*?\"hls_pull_url\" ` )
nnres := nnreg . FindAllStringSubmatch ( str , - 1 )
nnnreg := regexp . MustCompile ( ` (?i)\"hls_pull_url_map\"[\s\S]*?} ` )
nnnres := nnnreg . FindAllStringSubmatch ( nnres [ 0 ] [ 0 ] , - 1 )
if nnnres == nil {
reg := regexp . MustCompile ( ` (?i)ttwid=.*?; ` )
res := reg . FindStringSubmatch ( fmt . Sprintf ( "%s" , resp . Cookies ( ) ) ) [ 0 ]
url := "https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1728&screen_height=1117&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=116.0.0.0&web_rid=" + d . Rid
req , _ := http . NewRequest ( "GET" , url , nil )
req . Header . Add ( "User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" )
req . Header . Add ( "Cookie" , res )
req . Header . Add ( "Accept" , "*/*" )
req . Header . Add ( "Host" , "live.douyin.com" )
req . Header . Add ( "Connection" , "keep-alive" )
ress , _ := client . Do ( req )
defer ress . Body . Close ( )
body , _ := io . ReadAll ( ress . Body )
var json = string ( body )
status , _ := strconv . Atoi ( fmt . Sprintf ( "%s" , gjson . Get ( json , "data.data.0.status" ) ) )
if status != 2 {
return nil
}
json . Unmarshal ( [ ] byte ( ` { ` + nnnres [ 0 ] [ 0 ] + ` } ` ) , & mediamap )
return mediamap [ "hls_pull_url_map" ] [ "FULL_HD1" ]
var realurl string
value := gjson . Get ( json , "data.data.0.stream_url.live_core_sdk_data.pull_data.stream_data" )
value . ForEach ( func ( key , value gjson . Result ) bool {
switch d . Stream {
case "flv" :
realurl = fmt . Sprintf ( "%s" , gjson . Get ( value . String ( ) , "data.origin.main.flv" ) )
case "hls" :
realurl = fmt . Sprintf ( "%s" , gjson . Get ( value . String ( ) , "data.origin.main.hls" ) )
}
return true
} )
return realurl
}