Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace CNCDNPARSER {
  9. internal class Program {
  10.  
  11. // Globs
  12. private static WebClient WC = new WebClient();
  13. private static List<string> Errors = new List<string>();
  14. private static List<string> Timeouts = new List<string>();
  15. private static List<string> NotFounds = new List<string>();
  16. private static List<string> Forbiddens = new List<string>();
  17.  
  18. private enum PARTNERS {
  19. CN_LATAM = 618292,
  20. CN_EUROPE = 1745101
  21. }
  22. private enum FLAVORIDS {
  23. Source = 0,
  24. HD_HIGH = 1
  25. }
  26. private static PARTNERS SelectedPartner = PARTNERS.CN_EUROPE;
  27. private static FLAVORIDS SelectedFlavorId = FLAVORIDS.HD_HIGH;
  28. private static string SelectedOutputDir = "K:/CN";
  29.  
  30. #region Kaltura API Templated URLs
  31. private static readonly string BASE_URL = "https://{subdomain}.kaltura.com/p/{partner}/sp/{partner}00/{action}/{eid}/protocol/https/flavorParamIds/{fid}{extraparams}";
  32. private static readonly string VIDEO_URL = BASE_URL.Template(new Dictionary<string, object> {
  33. { "subdomain", "cdnapisec" },
  34. { "action", "playManifest/entryId" },
  35. { "format", "/format/download" },
  36. { "extraparams", string.Empty }
  37. });
  38. private static readonly string THUMB_URL = BASE_URL.Template(new Dictionary<string, object> {
  39. { "subdomain", "cfvod" },
  40. { "action", "thumbnail/entry_id" },
  41. { "format", "" },
  42. { "extraparams", "/quality/100/version/0/height/width/" }
  43. });
  44. #endregion
  45.  
  46. private static void Main() {
  47.  
  48. IEnumerable<(string, string)> VIDS = Regex.Matches(
  49. File.ReadAllText("K:/CN/" + SelectedPartner + ".html"),
  50. @"<p>Name: ([^<]*)<\/p><p>Kaltura ID: ([^<]*)"
  51. ).Cast<Match>().Select(VID => new ValueTuple<string, string>(VID.Groups[1].Value, VID.Groups[2].Value));
  52. int Total = VIDS.Count();
  53. int loop = 0;
  54. string[] CNFID0Files = Directory.GetDirectories($"{SelectedOutputDir}/{SelectedPartner}/{(int)FLAVORIDS.Source}").Select(x => Path.GetFileName(x).Trim().Trim(new[] { '\\', '/' })).ToArray();
  55. foreach((string NAME, string EID) in VIDS.Skip(0)) {
  56. string VIDEOFILE = VIDEO_URL.Template(new Dictionary<string, object> {
  57. { "partner", (int)SelectedPartner },
  58. { "eid", EID },
  59. { "fid", (int)SelectedFlavorId }
  60. });
  61. string THUMB = THUMB_URL.Template(new Dictionary<string, object> {
  62. { "partner", (int)SelectedPartner },
  63. { "eid", EID },
  64. { "fid", (int)SelectedFlavorId }
  65. });
  66. // Update the user on what's being downloaded
  67. Console.WriteLine($"[{++loop}/{Total}] \"{NAME}\"");
  68. // Make sure this isnt one we got in fid 0 run
  69. if(SelectedFlavorId != FLAVORIDS.Source && CNFID0Files.Any(F => F.EndsWith($" [{EID}]"))) {
  70. Console.WriteLine(" - Skipped, we have it from fid 0");
  71. continue;// we have it from the 0 run, we dont need it again in lower quality, skip
  72. }
  73. // Check if the file exists and able to be accessed, and get its hosted filename.
  74. string Filename = string.Empty;
  75. string Filesize = string.Empty;
  76. try {
  77. WebRequest WR = WebRequest.Create(VIDEOFILE);
  78. WR.Method = "HEAD";
  79. WebHeaderCollection WHC = WR.GetResponse().Headers;
  80. Filename = Regex.Match(WHC.ToString(), "filename=\"([^\"]*)").Groups[1].Value;
  81. Filesize = WHC[HttpResponseHeader.ContentLength];
  82. } catch(Exception ex) {
  83. if(ex.Message.Contains("(404)")) {
  84. Console.WriteLine(" - 404");
  85. NotFounds.Add(VIDEOFILE);
  86. } else {
  87. Console.WriteLine(" - ERROR! " + ex.Message);
  88. Timeouts.Add(VIDEOFILE);
  89. }
  90. continue;
  91. }
  92. // Create an output folder
  93. string VIDEOFILE_OUT = $"{SelectedOutputDir}/{SelectedPartner}/{(int)SelectedFlavorId}/{NAME.Legalize()} [{EID}]/{Filename.Legalize()}";
  94. Directory.CreateDirectory(Path.GetDirectoryName(VIDEOFILE_OUT));
  95. // Skip if already downloaded
  96. if(File.Exists(VIDEOFILE_OUT) && new FileInfo(VIDEOFILE_OUT).Length.ToString() == Filesize) {
  97. Console.WriteLine(" - Already downloaded, skipping");
  98. continue;
  99. }
  100. // Try download
  101. while(true) {
  102. try {
  103. WC.DownloadFile(VIDEOFILE, VIDEOFILE_OUT);
  104. Console.WriteLine(" - Downloaded!");
  105. } catch(Exception ex) {
  106. // If it timed out, retry
  107. if(ex.Message.Contains("timed out")) {
  108. continue;
  109. }
  110. // Delete folder, as anything in it is invalid data
  111. Directory.Delete(Path.GetDirectoryName(VIDEOFILE_OUT), true);
  112. // Log result
  113. string Result = ex.Message.Contains("(403)") ? "403" : "Unknown Error when downloading file :(";
  114. Console.WriteLine($" - {Result}");
  115. (Result == "403" ? Forbiddens : Errors).Add(VIDEOFILE);
  116. break;
  117. }
  118. }
  119. }
  120. // Done, tell the user
  121. File.WriteAllLines("errors.txt", Errors);
  122. File.WriteAllLines("timeouts.txt", Timeouts);
  123. File.WriteAllLines("notfounds.txt", NotFounds);
  124. File.WriteAllLines("forbiddens.txt", Forbiddens);
  125. Console.WriteLine($"\nFinished with {Errors.Count + Timeouts.Count + NotFounds.Count + Forbiddens.Count} Failed Entries");
  126. Console.ReadLine();
  127. }
  128. }
  129. internal static class Extensions {
  130. public static string Legalize(this string S) => S.Replace(":", ";").Replace("|", "¦").Replace("?", "?").Replace("\"", "'").Replace("/", "-");
  131. public static string Template(this string S, Dictionary<string, object> Parameters) => Parameters.Aggregate(S, (c, p) => c.Replace("{" + p.Key + "}", p.Value.ToString()));
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement