Skip to content

Commit

Permalink
Merge in Development-Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
frankvHoof93 committed Oct 8, 2023
1 parent e0a3dab commit e6c70b4
Show file tree
Hide file tree
Showing 151 changed files with 7,897 additions and 1,468 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Custom funding urls.
custom: ["https://www.buymeacoffee.com/frankvanhoof"]
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,4 @@ ExportedObj/
sysinfo.txt

# Crashlytics generated file
crashlytics-build.properties
crashlytics-build.properties
32 changes: 32 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] -

Initial full release of TikTokLiveSharp/TikTokLiveUnity

### Note
THIS IS A BREAKING UPDATE FOR USERS OF PREVIOUS VERSIONS OF THE LIBRARY.
PROCEED IN UPDATING WITH CAUTION!

Between the previous version to v1.0.0 a complete re-write was performed of
the schema used when decoding messages from the TikTokLive-Server.
This means that many Events & their underlying paramaters will have changed.
When updating from previous versions to v1.0.0, you will need to re-write your
code that hooks to these events.

### Added

- Added DisplayedGifts, which filters AvailableGifts down to those displayed for the room
- Added the ability to connect by RoomId instead of HostId
- Added the ability to skip checking the RoomInfo whilst connecting
- Added usage of Custom Parameters for clients & Requests

### Fixed

- Proxy is now properly used for both HTTP- and Websocket-Connection
- Lib now works properly on UWP-backend
- Enabled Images in Example

### Changed

- Complete rewrite of TikTokLive-BackendData & Events


## [0.9.5] - 2023-06-11

### Added
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# TikTokLiveSharp / TikTokLiveUnity v0.9.5
# TikTokLiveSharp / TikTokLiveUnity v1.0.0

[![LinkedIn](https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white&style=flat-square)](https://www.linkedin.com/in/frankvhoof93/ )
![Issues](https://img.shields.io/github/issues/frankvHoof93/TikTokLiveSharp)
![Forks](https://img.shields.io/github/forks/frankvHoof93/TikTokLiveSharp)
![Stars](https://img.shields.io/github/stars/frankvHoof93/TikTokLiveSharp)
![Nuget](https://img.shields.io/nuget/dt/TikTokLive_Sharp?logo=nuget)
[![Support Server](https://img.shields.io/discord/977648006063091742.svg?color=7289da&logo=discord&style=flat-square)](https://discord.gg/e2XwPNTBBr)

#### ***This is not an official library nor associated with TikTok or ByteDance in any way***
Expand All @@ -26,6 +27,7 @@ Other available versions include:
- [TikTok-Live-Connector](https://github.com/zerodytrash/TikTok-Live-Connector) - Node.js Library (by zerodytrash)
- [GoTikTokLive](https://github.com/Davincible/gotiktoklive) - Go Library (by David Brouwer)
- [TikTokLive](https://github.com/isaackogan/TikTokLive) - Python Library (by Isaac Kogan)
- [TikTokLiveJava](https://github.com/jwdeveloper/TikTokLiveJava) - Java Library (by JW)

The primary incentive behind this library was to update the existing TikTokLive-Library with anew Messaging-Schema and additional Exception-Handling. It's implemented in .NET Standard and should work universally across all .NET supported platforms. An older version of Protobuf-net was used to ensure Unity compatibility.

Expand All @@ -42,7 +44,8 @@ Setup of the Unity Library can be found [here](Setup_Unity.MD)
* **Sebheron** - *Original Creator of TikTokLiveSharp* - [sebheron](https://github.com/sebheron)
* **Isaac Kogan** - *Creator of TikTokLive & Signing-Server* - [isaackogan](https://github.com/isaackogan)
* **Zerody** - *Initial Reverse-Engineering Protobuf & Support* - [Zerody](https://github.com/zerodytrash/)
* **Davincible** - *Reverse-Engineering Stream Downloads* - [davincible](https://github.com/davincible)
* **David Brouwer** - *Reverse-Engineering Stream Downloads* - [davincible](https://github.com/davincible)
* **JW** - *Creator of TikTokLiveJava* - [JW](https://github.com/jwdeveloper)
* **David Teather** - *TikTokLive Introduction Tutorial* - [davidteather](https://github.com/davidteather)

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion Setup_CSharp.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TikTokLiveSharp v0.9.0
# TikTokLiveSharp v1.0.0

## Downloading the Source Code
Download the TikTokLiveSharp-SourceCode from the [Releases-Page](https://github.com/frankvHoof93/TikTokLiveSharp/releases/)
Expand Down
2 changes: 1 addition & 1 deletion Setup_Unity.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TikTokLiveUnity v0.9.0
# TikTokLiveUnity v1.0.0

## Installing the Project in Unity
- Open the Package Manager Window **(Window -> Package Manager)**
Expand Down
8 changes: 8 additions & 0 deletions TikTokLiveSharp/Client/Config/ClientSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public struct ClientSettings
#endif
public float PollingInterval;

/// <summary>
/// Enable Compression for Http-Responses
/// </summary>
#if UNITY
[UnityEngine.Tooltip("Enable Compression for Http-Responses")]
#endif
public bool EnableCompression;

/// <summary>
/// Proxy for Connections
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions TikTokLiveSharp/Client/Config/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static class Constants
Timeout = DEFAULT_TIMEOUT,
PollingInterval = DEFAULT_POLLTIME,
ClientLanguage = "en-US",
EnableCompression = true,
SkipRoomInfo = false,
HandleExistingMessagesOnConnect = true,
DownloadGiftInfo = true,
Expand Down Expand Up @@ -97,8 +98,10 @@ public static class Constants
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36" },
{ "Referer", "https://www.tiktok.com/" },
{ "Origin", "https://www.tiktok.com" },
{ "Accept-Language", "en-US,en; q=0.8" },
{ "Accept-Encoding", "gzip, deflate, br" }
{ "Accept-Language", "en-US,en; q=0.8" }

});

public static readonly KeyValuePair<string, string> COMPRESSION_HEADER = new KeyValuePair<string, string>( "Accept-Encoding", "gzip, deflate, br" );
}
}
17 changes: 11 additions & 6 deletions TikTokLiveSharp/Client/HTTP/TikTokHTTPClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ internal sealed class TikTokHttpClient
/// Index for this Client
/// </summary>
private readonly int clientNum;
/// <summary>
/// Whether Compression is allowed for Responses
/// </summary>
private readonly bool compression;
#endregion

#region Constructors
Expand All @@ -40,12 +44,13 @@ internal sealed class TikTokHttpClient
/// <param name="timeout">Timeout for Connection</param>
/// <param name="proxyHandler">Proxy for Connection</param>
/// <param name="clientLanguage">Accepted Language for Client (ISO-format)</param>
internal TikTokHttpClient(TimeSpan timeout, IWebProxy proxyHandler = null, string clientLanguage = null)
internal TikTokHttpClient(TimeSpan timeout, bool enableCompression = true, IWebProxy proxyHandler = null, string clientLanguage = null)
{
TikTokHttpRequest.Timeout = timeout;
TikTokHttpRequest.WebProxy = proxyHandler;
if (!string.IsNullOrEmpty(clientLanguage))
TikTokHttpRequest.ClientLanguage = clientLanguage;
compression = enableCompression;
concurrentClients++;
clientNum = concurrentClients;
}
Expand Down Expand Up @@ -133,9 +138,9 @@ internal async Task<JObject> PostJObjectToWebcastApi(string path, IDictionary<st
/// <param name="url">URL for Request</param>
/// <param name="parameters">Additional Parameters for Request</param>
/// <returns>HTTP-Request</returns>
private static ITikTokHttpRequest BuildRequest(string url, IDictionary<string, object> parameters = null)
private static ITikTokHttpRequest BuildRequest(string url, bool enableCompression = true, IDictionary<string, object> parameters = null)
{
return new TikTokHttpRequest(url).SetQueries(parameters);
return new TikTokHttpRequest(url, enableCompression).SetQueries(parameters);
}

/// <summary>
Expand All @@ -147,7 +152,7 @@ private static ITikTokHttpRequest BuildRequest(string url, IDictionary<string, o
/// <returns>Task to await</returns>
private async Task<HttpContent> GetRequest(string url, IDictionary<string, object> parameters = null, bool signUrl = false)
{
ITikTokHttpRequest request = BuildRequest(signUrl ? await GetSignedUrl(url, parameters) : url, signUrl ? null : parameters);
ITikTokHttpRequest request = BuildRequest(signUrl ? await GetSignedUrl(url, parameters) : url, compression, signUrl ? null : parameters);
return await request.Get();
}

Expand All @@ -160,7 +165,7 @@ private async Task<HttpContent> GetRequest(string url, IDictionary<string, objec
/// <returns>Task to await</returns>
private async Task<HttpContent>PostRequest(string url, IDictionary<string, object> parameters = null, bool signUrl = false)
{
ITikTokHttpRequest request = BuildRequest(signUrl ? await GetSignedUrl(url, parameters) : url, signUrl ? null : parameters);
ITikTokHttpRequest request = BuildRequest(signUrl ? await GetSignedUrl(url, parameters) : url, compression, signUrl ? null : parameters);
return await request.Post(null);
}

Expand All @@ -174,7 +179,7 @@ private async Task<HttpContent>PostRequest(string url, IDictionary<string, objec
/// <returns>Task to await</returns>
private async Task<HttpContent> PostRequest(string url, string data, IDictionary<string, object> parameters = null, bool signUrl = false)
{
ITikTokHttpRequest request = BuildRequest(signUrl ? await GetSignedUrl(url, parameters) : url, signUrl ? null : parameters);
ITikTokHttpRequest request = BuildRequest(signUrl ? await GetSignedUrl(url, parameters) : url, compression, signUrl ? null : parameters);
return await request.Post(new StringContent(data, Encoding.UTF8));
}

Expand Down
29 changes: 19 additions & 10 deletions TikTokLiveSharp/Client/HTTP/TikTokHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,24 @@ public static IWebProxy WebProxy
/// </summary>
/// <param name="url">The url to send the request to</param>
/// <param name="useCookies">Whether to allow for Cookies on this HTTP-Request</param>
/// <param name="enableCompression">Enable Compression for Http-Response</param>
/// <exception cref="ArgumentException">Throws exception if URL is invalid</exception>
public TikTokHttpRequest(string url, bool useCookies = true)
public TikTokHttpRequest(string url, bool enableCompression = true, bool useCookies = true)
{
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri result))
throw new ArgumentException("Invalid Url", nameof(url));
CookieJar ??= new TikTokCookieJar();
handler ??= new HttpClientHandler
if (CookieJar == null)
CookieJar = new TikTokCookieJar();
if (handler == null)
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
Proxy = WebProxy,
UseProxy = WebProxy != null,
UseCookies = useCookies
};
handler = new HttpClientHandler
{
AutomaticDecompression = enableCompression ? ~DecompressionMethods.None : DecompressionMethods.None,
Proxy = WebProxy,
UseProxy = WebProxy != null,
UseCookies = useCookies
};
}
if (client == null)
{
client = new HttpClient(handler)
Expand All @@ -156,13 +161,16 @@ public TikTokHttpRequest(string url, bool useCookies = true)
};
foreach (KeyValuePair<string, string> header in Constants.DEFAULT_REQUEST_HEADERS)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
if (enableCompression)
client.DefaultRequestHeaders.Add(Constants.COMPRESSION_HEADER.Key, Constants.COMPRESSION_HEADER.Value);
if (!string.IsNullOrEmpty(clientLanguage))
client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue(clientLanguage, 0.9));
}
request = new HttpRequestMessage
{
RequestUri = result
};
request.Headers.Host = result.Host;
sent = false;
}
#endregion
Expand Down Expand Up @@ -191,7 +199,7 @@ public async Task<HttpContent> Post(HttpContent data)
if (sent)
throw new InvalidOperationException("Requests should not be reused");
request.Method = HttpMethod.Post;
request.Content = data;
request.Content = data;
return await GetContent();
}

Expand All @@ -217,11 +225,12 @@ private async Task<HttpContent> GetContent()
{
if (query != null)
request.RequestUri = new Uri($"{request.RequestUri.AbsoluteUri}?{query}");

HttpResponseMessage response = await client.SendAsync(request);
request.Dispose();
sent = true;
if (response.StatusCode == HttpStatusCode.NotFound)
throw new HttpRequestException($"Request responded with 404 NOT_FOUND");
throw new HttpRequestException("Request responded with 404 NOT_FOUND");
if (!response.IsSuccessStatusCode)
throw new HttpRequestException($"Request was unsuccessful [{(int)response.StatusCode}]");
MediaTypeHeaderValue ct = response.Content.Headers?.ContentType;
Expand Down
9 changes: 3 additions & 6 deletions TikTokLiveSharp/Client/Socket/TikTokWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Text;
Expand All @@ -19,7 +20,7 @@ public sealed class TikTokWebSocket : ITikTokWebSocket
/// <summary>
/// Is the websocket currently connected?
/// </summary>
public bool IsConnected => clientWebSocket is { State: WebSocketState.Open };
public bool IsConnected => clientWebSocket != null && clientWebSocket.State == WebSocketState.Open;

/// <summary>
/// State for WebSocket
Expand Down Expand Up @@ -149,13 +150,9 @@ public async Task<TikTokWebSocketResponse> ReceiveMessage()
ArraySegment<byte> arr = new ArraySegment<byte>(readBuffer);
WebSocketReceiveResult response = await clientWebSocket.ReceiveAsync(arr, token);
if (response.MessageType != WebSocketMessageType.Binary || arr.Array == null)
{
valid = false;
}
else
{
message.AddRange(arr[..response.Count]);
}
message.AddRange(arr.Take(response.Count));
endOfMessage = response.EndOfMessage;
}
token.ThrowIfCancellationRequested();
Expand Down
Loading

0 comments on commit e6c70b4

Please sign in to comment.