kurobyte

Simple Facebook video download

Aug 28th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Net;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace DecodingEntities
  12. {
  13.     class Program
  14.     {
  15.         public static WebClient wc = new WebClient();
  16.         static void Main(string[] args)
  17.         {
  18.     //JSON.net library required to run this code. http://james.newtonking.com/json
  19.             //url = "https://www.facebook.com/video.php?v=1452983714984650";
  20.             Console.WriteLine("Insert video URL:");
  21.             string url = Console.ReadLine();
  22.             string strRegex = @"params\"",\""(.*)\""]";
  23.            Regex myRegex = new Regex(strRegex, RegexOptions.None);
  24.            
  25.            wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
  26.            wc.Headers.Add("Content-Type", "text/html; charset=utf-8");
  27.            string html = "";
  28.            bool error = false;
  29.            try
  30.            {
  31.                html = wc.DownloadString(url);
  32.            }
  33.            catch (WebException wex)
  34.            {
  35.                Console.WriteLine("Invalid URL");
  36.                error = true;
  37.            }
  38.  
  39.            if (!error)
  40.            {
  41.                string title = Regex.Match(html, @"<title id=\""pageTitle\"">(.*?) | .*? </title>").Groups[1].Value;
  42.                string para = myRegex.Match(html).Groups[1].Value;
  43.  
  44.                para = clearEntities(para);
  45.  
  46.                int lindex = 0;
  47.                string[] s = para.Split('}');
  48.                for (int i = 0; i < 2; i++)
  49.                {
  50.                    lindex += s[i].Length;
  51.                }
  52.                para = para.Substring(0, lindex + 2);
  53.  
  54.                Console.WriteLine("Searching video link...");
  55.                try
  56.                {
  57.                    List<Dictionary<string, string>> video_data = JObject.Parse(para).SelectToken("video_data").ToObject<List<Dictionary<string, string>>>();
  58.  
  59.                    if (video_data[0].ContainsKey("hd_src"))
  60.                    {
  61.                        Console.WriteLine("Downloading HD video.\r\nStoring video: "+title+".mp4");
  62.                        DescarregaVideo(video_data[0]["hd_src"], title);
  63.                    }
  64.                    else if (video_data[0].ContainsKey("sd_src"))
  65.                    {
  66.                        Console.WriteLine("Downloading SD video.\r\nStoring video: " + title + ".mp4");
  67.                        DescarregaVideo(video_data[0]["sd_src"], title);
  68.                    }
  69.                    else
  70.                    {
  71.                        Console.WriteLine("No link.");
  72.                    }
  73.                }
  74.                catch (Exception ex)
  75.                {
  76.                    Console.WriteLine("Error searching link.");
  77.                }
  78.            }
  79.  
  80.            Console.ReadKey();
  81.        }
  82.  
  83.        public static void DescarregaVideo(string url, string nom)
  84.        {
  85.            try
  86.            {
  87.                wc.DownloadFile(url, nom + ".mp4");
  88.                Console.WriteLine("Download Complete.");
  89.            }
  90.            catch (WebException wex)
  91.            {
  92.                Console.WriteLine("Error during video download.");
  93.            }
  94.        }
  95.  
  96.        public static string clearEntities(string para)
  97.        {
  98.            para = para.Replace("[", "");
  99.            para = para.Replace("]", "");
  100.  
  101.            para = para.Replace("\\u00257B", "{");
  102.            para = para.Replace("\\u00257D", "}");
  103.            para = para.Replace("\\u002522", "\"");
  104.             para = para.Replace("\\u00253A", ":");
  105.             para = para.Replace("\\u00252C", ",");
  106.  
  107.  
  108.             para = para.Replace("\\u00255C", "\\");
  109.             para = para.Replace("\\u00252F", "/");
  110.             para = para.Replace("\\u00253F", "?");
  111.             para = para.Replace("\\u00253D", "=");
  112.             para = para.Replace("\\u002526", "&");
  113.            
  114.             para = para.Replace("\\u00255B", "[");
  115.             para = para.Replace("\\u00255D", "]");
  116.  
  117.             return Uri.UnescapeDataString(para)+"\"}";
  118.         }
  119.     }
  120. }
Add Comment
Please, Sign In to add comment