Skip to content

Commit

Permalink
Merge pull request #22 from ape-byte/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
ape-byte authored Feb 11, 2025
2 parents 6cae2ab + 6e33755 commit ed3da58
Show file tree
Hide file tree
Showing 20 changed files with 542 additions and 89,587 deletions.
4 changes: 4 additions & 0 deletions BarrageGrab/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
Expand Down
74 changes: 73 additions & 1 deletion BarrageGrab/DyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using BarrageGrab.Modles;
using BarrageGrab.Modles.ProtoEntity;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.Crmf;
Expand Down Expand Up @@ -53,7 +54,78 @@ public static async Task<WebCastGiftPack> GetGifts()
{
Logger.LogError($"礼物信息从服务器请求失败: {ex.Message}");
return null;
}
}
}

/// <summary>
/// 获取房间信息,通过原生接口
/// </summary>
/// <param name="webRoomid">web直播间号</param>
/// <param name="user">账号凭据</param>
/// <param name="proxy">查询代理</param>
/// <returns></returns>
public static async Task<RoomInfo> GetRoomInfoForApi(string webRoomid, string cookie)
{
string url = "https://live.douyin.com/webcast/room/web/enter/";

var client = new HttpClient();


var uri = new UriBuilder(url);
var dict = new Dictionary<string, string>();
dict.Set("aid", "6383");
dict.Set("live_id", "1");
dict.Set("app_name", "douyin_web");
dict.Set("device_platform", "web");
dict.Set("language", "zh-CN");
dict.Set("cookie_enabled", "true");
dict.Set("enter_from", "page_refresh");
dict.Set("web_rid", webRoomid);
//dict.Set("room_id_str", roomid);
dict.Set("enter_source", "");
dict.Set("is_need_double_stream", "false");
dict.Set("insert_task_id", "");
dict.Set("live_reason", "");
dict.Set("browser_language", "zh-CN");
dict.Set("browser_platform", "Win32");
dict.Set("browser_name", "Edge");
dict.Foreach(f => uri.AddQueryParam(f.Key, f.Value));

url = uri.Uri.ToString();

var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Accept", "application/json, text/plain, */*");
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Referer", $"https://live.douyin.com/{webRoomid}");
request.Headers.Add("Host", "live.douyin.com");
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0");
request.Headers.Add("Cookie", cookie);

var rsp = await client.SendAsync(request);
if (rsp == null || rsp.StatusCode != System.Net.HttpStatusCode.OK)
{
return null;
}

var buff = rsp.Content.ReadAsByteArrayAsync();
var result = Encoding.UTF8.GetString(buff.Result);
RoomInfo dto;
var res = RoomInfo.TryParseRoomEnterResponse(result, out dto);
int code = res.Item1;
string msg = res.Item2;
if (code == 0)
{
dto.LiveUrl = $"https://live.douyin.com/{webRoomid}";
dto.WebRoomId = webRoomid;
AppRuntime.RoomCaches.AddRoomInfoCache(dto);
Logger.LogInfo($"{dto.Owner.Nickname ?? dto.WebRoomId ?? dto.RoomId} 的直播间信息已添加到缓存");
}
else
{
return null;
}

return dto;
}
}
}
126 changes: 108 additions & 18 deletions BarrageGrab/LiveCompanHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
using System.Runtime.InteropServices;
using IWshRuntimeLibrary;
using File = System.IO.File;
using Microsoft.Win32;
using System.Diagnostics;
using Newtonsoft.Json;
using System.Runtime.InteropServices.ComTypes;

namespace BarrageGrab
{
Expand All @@ -20,38 +24,124 @@ public static class LiveCompanHelper
/// <returns></returns>
public static string GetExePath()
{
string appName = "直播伴侣";
appName = Path.GetFileNameWithoutExtension(appName);
string exePath = "";
//获取exe所在目录
//1.先在开始菜单找
var shortcutPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\直播伴侣.lnk";
if (File.Exists(shortcutPath))
{
//获取快捷方式的目标路径
exePath = GetInkTargetPath(shortcutPath);
if (!File.Exists(exePath))

//从卸载列表中查找
try
{
// 打开注册表中的卸载信息节点
RegistryKey uninstallNode = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");

if (uninstallNode != null)
{
exePath = "";
Logger.LogWarn($"直播伴侣.lnk 所指向的exe文件位置 {exePath} 无效");
// 遍历所有的子键(每个子键代表一个已安装的程序的卸载信息)
foreach (string subKeyName in uninstallNode.GetSubKeyNames())
{
RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
var displayName = subKey?.GetValue("DisplayName")?.ToString() ?? "";

if (!displayName.Contains(appName)) continue;
string uninstallString = subKey?.GetValue("UninstallString")?.ToString();
if (uninstallString.IsNullOrWhiteSpace()) continue;

//"D:\Program Files (x86)\webcast_mate\Uninstall 直播伴侣.exe" /allusers
//正则取出""中间的内容
var match = Regex.Match(uninstallString, "\"(.+?)\"");
if (match.Success)
{
string uninstallPath = match.Groups[1].Value;
string dir = Path.GetDirectoryName(uninstallPath);
string exe = Path.Combine(dir, $"{appName}.exe");
if (File.Exists(exe))
{
exePath = exe;
}
}
}
}
}
else
catch (Exception ex)
{
// 发生异常时,可能是注册表访问出错,记录错误信息
Logger.LogError(ex, $"Error checking for Live Companion installation path: {ex.Message}");
}

//从 C:\ProgramData\Microsoft\Windows\Start Menu\Programs 中查找
string startMenuPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs");
var findFiles = Directory.GetFiles(startMenuPath, $"{appName}.lnk", SearchOption.AllDirectories);
if (findFiles.Length > 0)
{
Logger.LogInfo("未找到直播伴侣的桌面快捷方式");
exePath = GetShortcutTarget(findFiles[0]);
}

//再从配置文件读取
if (exePath.IsNullOrWhiteSpace())
var fileName = Path.GetFileName(exePath);

//判断是否是 版本选择器
if (fileName.Contains("Launcher"))
{
exePath = AppSetting.Current.LiveCompanPath;
if(!exePath.IsNullOrWhiteSpace() && !File.Exists(exePath))
var dir = Path.GetDirectoryName(exePath);
//读取目录下 launcher_config.json
var launcherConfigPath = Path.Combine(dir, "launcher_config.json");
if (!File.Exists(launcherConfigPath))
{
Logger.LogWarn($"所配置的 {exePath} 不存在");
exePath = "";
throw new Exception("未找到直播伴侣版本选择器的 launcher_config.json 文件");
}
var json = File.ReadAllText(launcherConfigPath, Encoding.UTF8);
var jobj = JsonConvert.DeserializeObject<dynamic>(json);
string curPath = jobj.cur_path;
exePath = Path.Combine(dir, curPath, "直播伴侣.exe");
}

// 如果没有找到相关信息,则返回空字符串
return exePath;
}

/// <summary>
/// 获取 .lnk 文件的目标路径
/// </summary>
/// <param name="shortcutPath">快捷方式文件路径</param>
/// <returns>目标路径</returns>
private static string GetShortcutTarget(string shortcutPath)
{
if (string.IsNullOrEmpty(shortcutPath))
{
throw new ArgumentException("快捷方式路径不能为空", nameof(shortcutPath));
}

var processStartInfo = new ProcessStartInfo
{
FileName = "powershell",
Arguments = $"-command \"$WshShell = New-Object -ComObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut('{shortcutPath}'); $Shortcut.TargetPath\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};

try
{
using (var process = Process.Start(processStartInfo))
{
if (process == null)
{
throw new InvalidOperationException("无法启动 PowerShell 进程");
}

using (var reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
process.WaitForExit();
return result.Trim();
}
}
}
catch (Exception ex)
{
throw new Exception($"powershell 获取快捷方式目标路径失败,错误信息:{ex.Message}", ex);
}
}

/// <summary>
/// 初始化直播伴侣环境
/// </summary>
Expand Down
Loading

0 comments on commit ed3da58

Please sign in to comment.