Trvp

[Async] Offliberty Wrapper

Dec 12th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace OfflibertyWrapper
  9. {
  10.     /// <summary>
  11.     /// Enum the programmar provides that states whether to convert the given media to MP3 or MP4.
  12.     /// </summary>
  13.     public enum MediaType { Mp3, Mp4 }
  14.  
  15.     /// <summary>
  16.     /// Wrapper used to convert media to MP3 or MP4.
  17.     /// </summary>
  18.     public static class OfflibertyConverter
  19.     {
  20.         /// <summary>
  21.         /// The Uri of the server that will convert the media.
  22.         /// </summary>
  23.         private static readonly Uri OfflibertyServerUri = new Uri("http://offliberty.com");
  24.  
  25.         /// <summary>
  26.         /// The Uri of the page on the server that will be checked to see if the server is ready.
  27.         /// </summary>
  28.         private static readonly Uri WaitUri = new Uri(OfflibertyServerUri, "wait.php");
  29.  
  30.         /// <summary>
  31.         /// The Uri of the page on the server that we will be using to send the media arguments to.
  32.         /// </summary>
  33.         private static readonly Uri Off54Uri = new Uri(OfflibertyServerUri, "off54.php");
  34.  
  35.         /// <summary>
  36.         /// The Regex instance that will be used to scrape the converted media data url.
  37.         /// </summary>
  38.         private static readonly Regex UrlRegex = new Regex("\"([^\"]*)\" class=\"download\">Right-click", RegexOptions.Compiled);
  39.  
  40.  
  41.         /// <summary>
  42.         /// The CookieContainer instance used to store the cookies between the server.
  43.         /// </summary>
  44.         private static CookieContainer _cookieContainer;
  45.  
  46.         /// <summary>
  47.         /// The HttpClient instance used to convert the videos using the offlibery website.
  48.         /// </summary>
  49.         private static readonly HttpClient Client;
  50.  
  51.         /// <summary>
  52.         /// OfflibertyConverter initializer.
  53.         /// </summary>
  54.         static OfflibertyConverter()
  55.         {
  56.             _cookieContainer = new CookieContainer();
  57.  
  58.             Client = new HttpClient(new HttpClientHandler
  59.                         { CookieContainer = _cookieContainer });
  60.         }
  61.  
  62.  
  63.         /// <summary>
  64.         /// Returns the converted version of the content in mediaUrl into Mp3 or Mp4 form.
  65.         /// </summary>
  66.         /// <param name="mediaUri">The media that will get converted ino the specificed media type.</param>
  67.         /// <param name="mediaType">The media type that the media will get converted into.</param>
  68.         /// <returns></returns>
  69.         public static async Task<OfflibertyResponse> Convert(Uri mediaUri, MediaType mediaType)
  70.         {
  71.             try
  72.             {
  73.                 // Send the initial response to check if the server is available.
  74.                 var waitingOneResponse = await Client.GetAsync(WaitUri);
  75.                 var waitingOneResponseContent = await waitingOneResponse.Content.ReadAsStringAsync();
  76.  
  77.                 // Check if the waiting response starts with "Wait". This verifies if the server is available to convert the file.
  78.                 if (!waitingOneResponseContent.StartsWith("Wait"))
  79.                     return new OfflibertyResponse(false, "Server is not available");
  80.  
  81.                 // Declare data without the "video_file" key
  82.                 var data = new Dictionary<string, string> { {"track", mediaUri.ToString()} };
  83.  
  84.                 // If the specificed media type is an MP4 add the "video_file" key to the data.
  85.                 if (mediaType == MediaType.Mp4)
  86.                     data.Add("video_file", "1");
  87.  
  88.                 // Declare postData with data as the first parameter to the FormUrlEncodedContent constructor.
  89.                 var postData = new FormUrlEncodedContent(data);
  90.  
  91.                 // Send the post data to the server.
  92.                 var off54Response = await Client.PostAsync(Off54Uri, postData);
  93.                 var off54ResponseContent = await off54Response.Content.ReadAsStringAsync();
  94.  
  95.                 // Checks the source if it contains the media file with the UrlRegex instance.
  96.                 // Send the "OfflibertyResponse" with the status "false" if the url is not matched.
  97.                 if (!UrlRegex.IsMatch(off54ResponseContent))
  98.                     return new OfflibertyResponse(false, "Converted media url is not found.");
  99.  
  100.                 var url = UrlRegex.Match(off54ResponseContent).Groups[1].Value;
  101.                 var convertedMediaUrl = string.Join(".", url.Substring(0, url.LastIndexOf('.')),
  102.                                             mediaType.ToString().ToLower());
  103.  
  104.                 // Send the "OfflibertyResponse" along with the converted media Url.
  105.                 return new OfflibertyResponse(true, convertedMediaUrl);
  106.             }
  107.             catch (Exception ex)
  108.             { return new OfflibertyResponse(false, "Exception Occured", ex); }
  109.             finally
  110.             { _cookieContainer = new CookieContainer(); }
  111.         }
  112.     }
  113.  
  114.     /// <summary>
  115.     /// Class that returns whether a exception occured or if the convertion was successful.
  116.     /// </summary>
  117.     public class OfflibertyResponse
  118.     {
  119.         /// <summary>
  120.         /// The boolean that states wheter the convertion was successful or not.
  121.         /// </summary>
  122.         public bool Status { get; private set; }
  123.  
  124.         /// <summary>
  125.         /// The string that stores an input given. (This may be an error message or the converted media url)
  126.         /// </summary>
  127.         public string Message { get; private set; }
  128.  
  129.         /// <summary>
  130.         /// The exception that has been thrown when trying to convert media.
  131.         /// </summary>
  132.         public Exception Exception { get; private set; }
  133.  
  134.         /// <summary>
  135.         /// The constructor that initializes the OfflibertyResponse class.
  136.         /// </summary>
  137.         /// <param name="status"></param>
  138.         /// <param name="message"></param>
  139.         /// <param name="exception"></param>
  140.         public OfflibertyResponse(bool status, string message = null, Exception exception = null)
  141.         {
  142.             Status = status;
  143.             Message = string.IsNullOrWhiteSpace(message) ? string.Empty : message;
  144.             Exception = exception;
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment