Last active
May 1, 2019 14:18
-
-
Save thebitbrine/5f3802d721a58cb11dd40fc7594998d1 to your computer and use it in GitHub Desktop.
Parses magnet URI into an object.
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
public TorrentMetadata ParseMagnet(string Magnet) | |
{ | |
var MD = new TorrentMetadata(); | |
MD.URI = Magnet; | |
Magnet = Magnet.Replace("magnet:?", ""); | |
var Parts = Magnet.Split('&'); | |
foreach (var RawPart in Parts) | |
{ | |
var Part = System.Net.WebUtility.UrlDecode(RawPart); | |
if (Part.StartsWith("xt=")) MD.InfoHash = Part.Split(':').Last().ToUpper(); | |
if (Part.StartsWith("dn=")) MD.DisplayName = Part.Replace("dn=", ""); | |
if (Part.StartsWith("xl=")) MD.ExactLength = long.Parse(Part.Replace("xl=", "")); | |
if (Part.StartsWith("xt=")) AddToArray(ref MD.ExactTopics, Part); | |
if (Part.StartsWith("as=")) AddToArray(ref MD.AcceptableSources, Part); | |
if (Part.StartsWith("xs=")) AddToArray(ref MD.ExactSources, Part); | |
if (Part.StartsWith("kt=")) AddToArray(ref MD.KeywordTopics, Part); | |
if (Part.StartsWith("mt=")) AddToArray(ref MD.ManifestTopics, Part); | |
if (Part.StartsWith("tr=")) AddToArray(ref MD.Trackers, Part); | |
} | |
return MD; | |
} | |
public void AddToArray(ref string[] Array, string Value) | |
{ | |
System.Array.Resize(ref Array, Array.Length + 1); | |
Array[Array.Length - 1] = Value.Remove(0, 3); | |
} | |
public class TorrentMetadata | |
{ | |
public string URI; | |
public string InfoHash; | |
public string DisplayName; | |
public long ExactLength; | |
public string[] ExactTopics = new string[0]; | |
public string[] AcceptableSources = new string[0]; | |
public string[] ExactSources = new string[0]; | |
public string[] KeywordTopics = new string[0]; | |
public string[] ManifestTopics = new string[0]; | |
public string[] Trackers = new string[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment