mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-20 22:14:34 -04:00
29 lines
896 B
C#
29 lines
896 B
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace NzbDrone.Common.Http
|
|
{
|
|
public static class UserAgentParser
|
|
{
|
|
private static readonly Regex AppSourceRegex = new(@"^(?<agent>[a-z0-9]+)(?:\/.+(?:\(.*\))?|$)",
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
public static string SimplifyUserAgent(string userAgent)
|
|
{
|
|
if (userAgent == null || userAgent.StartsWith("Mozilla/5.0", StringComparison.Ordinal))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return userAgent;
|
|
}
|
|
|
|
public static string ParseSource(string userAgent)
|
|
{
|
|
var match = AppSourceRegex.Match(SimplifyUserAgent(userAgent?.Trim()) ?? string.Empty);
|
|
|
|
return match.Groups["agent"].Success ? match.Groups["agent"].Value : "Other";
|
|
}
|
|
}
|
|
}
|