forked from kkdai/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a nearly verbatim copy of [1], for now the code is pretty rough and I'm not even sure that this data should be passed for all requests but it does seem to unblock the iOS client for now. [1] TeamNewPipe/NewPipeExtractor#1262
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package youtube | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"net/url" | ||
) | ||
|
||
type ProtoBuilder struct { | ||
byteBuffer bytes.Buffer | ||
} | ||
|
||
func (pb *ProtoBuilder) ToBytes() []byte { | ||
return pb.byteBuffer.Bytes() | ||
} | ||
|
||
func (pb *ProtoBuilder) ToUrlEncodedBase64() string { | ||
b64 := base64.URLEncoding.EncodeToString(pb.ToBytes()) | ||
return url.QueryEscape(b64) | ||
} | ||
|
||
func (pb *ProtoBuilder) writeVarint(val int64) error { | ||
if val == 0 { | ||
_, err := pb.byteBuffer.Write([]byte{0}) | ||
return err | ||
} | ||
for { | ||
b := byte(val & 0x7F) | ||
val >>= 7 | ||
if val != 0 { | ||
b |= 0x80 | ||
} | ||
_, err := pb.byteBuffer.Write([]byte{b}) | ||
if err != nil { | ||
return err | ||
} | ||
if val == 0 { | ||
break | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (pb *ProtoBuilder) field(field int, wireType byte) error { | ||
val := int64(field<<3) | int64(wireType&0x07) | ||
return pb.writeVarint(val) | ||
} | ||
|
||
func (pb *ProtoBuilder) Varint(field int, val int64) error { | ||
err := pb.field(field, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return pb.writeVarint(val) | ||
} | ||
|
||
func (pb *ProtoBuilder) String(field int, stringVal string) error { | ||
strBts := []byte(stringVal) | ||
return pb.Bytes(field, strBts) | ||
} | ||
|
||
func (pb *ProtoBuilder) Bytes(field int, bytesVal []byte) error { | ||
if err := pb.field(field, 2); err != nil { | ||
return err | ||
} | ||
|
||
if err := pb.writeVarint(int64(len(bytesVal))); err != nil { | ||
return err | ||
} | ||
|
||
_, err := pb.byteBuffer.Write(bytesVal) | ||
return err | ||
} |