Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text.RegularExpressions;
- namespace CNCDNPARSER {
- internal class Program {
- // Globs
- private static WebClient WC = new WebClient();
- private static List<string> Errors = new List<string>();
- private static List<string> Timeouts = new List<string>();
- private static List<string> NotFounds = new List<string>();
- private static List<string> Forbiddens = new List<string>();
- private enum PARTNERS {
- CN_LATAM = 618292,
- CN_EUROPE = 1745101
- }
- private enum FLAVORIDS {
- Source = 0,
- HD_HIGH = 1
- }
- private static PARTNERS SelectedPartner = PARTNERS.CN_EUROPE;
- private static FLAVORIDS SelectedFlavorId = FLAVORIDS.HD_HIGH;
- private static string SelectedOutputDir = "K:/CN";
- #region Kaltura API Templated URLs
- private static readonly string BASE_URL = "https://{subdomain}.kaltura.com/p/{partner}/sp/{partner}00/{action}/{eid}/protocol/https/flavorParamIds/{fid}{extraparams}";
- private static readonly string VIDEO_URL = BASE_URL.Template(new Dictionary<string, object> {
- { "subdomain", "cdnapisec" },
- { "action", "playManifest/entryId" },
- { "format", "/format/download" },
- { "extraparams", string.Empty }
- });
- private static readonly string THUMB_URL = BASE_URL.Template(new Dictionary<string, object> {
- { "subdomain", "cfvod" },
- { "action", "thumbnail/entry_id" },
- { "format", "" },
- { "extraparams", "/quality/100/version/0/height/width/" }
- });
- #endregion
- private static void Main() {
- IEnumerable<(string, string)> VIDS = Regex.Matches(
- File.ReadAllText("K:/CN/" + SelectedPartner + ".html"),
- @"<p>Name: ([^<]*)<\/p><p>Kaltura ID: ([^<]*)"
- ).Cast<Match>().Select(VID => new ValueTuple<string, string>(VID.Groups[1].Value, VID.Groups[2].Value));
- int Total = VIDS.Count();
- int loop = 0;
- string[] CNFID0Files = Directory.GetDirectories($"{SelectedOutputDir}/{SelectedPartner}/{(int)FLAVORIDS.Source}").Select(x => Path.GetFileName(x).Trim().Trim(new[] { '\\', '/' })).ToArray();
- foreach((string NAME, string EID) in VIDS.Skip(0)) {
- string VIDEOFILE = VIDEO_URL.Template(new Dictionary<string, object> {
- { "partner", (int)SelectedPartner },
- { "eid", EID },
- { "fid", (int)SelectedFlavorId }
- });
- string THUMB = THUMB_URL.Template(new Dictionary<string, object> {
- { "partner", (int)SelectedPartner },
- { "eid", EID },
- { "fid", (int)SelectedFlavorId }
- });
- // Update the user on what's being downloaded
- Console.WriteLine($"[{++loop}/{Total}] \"{NAME}\"");
- // Make sure this isnt one we got in fid 0 run
- if(SelectedFlavorId != FLAVORIDS.Source && CNFID0Files.Any(F => F.EndsWith($" [{EID}]"))) {
- Console.WriteLine(" - Skipped, we have it from fid 0");
- continue;// we have it from the 0 run, we dont need it again in lower quality, skip
- }
- // Check if the file exists and able to be accessed, and get its hosted filename.
- string Filename = string.Empty;
- string Filesize = string.Empty;
- try {
- WebRequest WR = WebRequest.Create(VIDEOFILE);
- WR.Method = "HEAD";
- WebHeaderCollection WHC = WR.GetResponse().Headers;
- Filename = Regex.Match(WHC.ToString(), "filename=\"([^\"]*)").Groups[1].Value;
- Filesize = WHC[HttpResponseHeader.ContentLength];
- } catch(Exception ex) {
- if(ex.Message.Contains("(404)")) {
- Console.WriteLine(" - 404");
- NotFounds.Add(VIDEOFILE);
- } else {
- Console.WriteLine(" - ERROR! " + ex.Message);
- Timeouts.Add(VIDEOFILE);
- }
- continue;
- }
- // Create an output folder
- string VIDEOFILE_OUT = $"{SelectedOutputDir}/{SelectedPartner}/{(int)SelectedFlavorId}/{NAME.Legalize()} [{EID}]/{Filename.Legalize()}";
- Directory.CreateDirectory(Path.GetDirectoryName(VIDEOFILE_OUT));
- // Skip if already downloaded
- if(File.Exists(VIDEOFILE_OUT) && new FileInfo(VIDEOFILE_OUT).Length.ToString() == Filesize) {
- Console.WriteLine(" - Already downloaded, skipping");
- continue;
- }
- // Try download
- while(true) {
- try {
- WC.DownloadFile(VIDEOFILE, VIDEOFILE_OUT);
- Console.WriteLine(" - Downloaded!");
- } catch(Exception ex) {
- // If it timed out, retry
- if(ex.Message.Contains("timed out")) {
- continue;
- }
- // Delete folder, as anything in it is invalid data
- Directory.Delete(Path.GetDirectoryName(VIDEOFILE_OUT), true);
- // Log result
- string Result = ex.Message.Contains("(403)") ? "403" : "Unknown Error when downloading file :(";
- Console.WriteLine($" - {Result}");
- (Result == "403" ? Forbiddens : Errors).Add(VIDEOFILE);
- break;
- }
- }
- }
- // Done, tell the user
- File.WriteAllLines("errors.txt", Errors);
- File.WriteAllLines("timeouts.txt", Timeouts);
- File.WriteAllLines("notfounds.txt", NotFounds);
- File.WriteAllLines("forbiddens.txt", Forbiddens);
- Console.WriteLine($"\nFinished with {Errors.Count + Timeouts.Count + NotFounds.Count + Forbiddens.Count} Failed Entries");
- Console.ReadLine();
- }
- }
- internal static class Extensions {
- public static string Legalize(this string S) => S.Replace(":", ";").Replace("|", "¦").Replace("?", "?").Replace("\"", "'").Replace("/", "-");
- public static string Template(this string S, Dictionary<string, object> Parameters) => Parameters.Aggregate(S, (c, p) => c.Replace("{" + p.Key + "}", p.Value.ToString()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement