Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace OfflibertyWrapper
- {
- /// <summary>
- /// Enum the programmar provides that states whether to convert the given media to MP3 or MP4.
- /// </summary>
- public enum MediaType { Mp3, Mp4 }
- /// <summary>
- /// Wrapper used to convert media to MP3 or MP4.
- /// </summary>
- public static class OfflibertyConverter
- {
- /// <summary>
- /// The Uri of the server that will convert the media.
- /// </summary>
- private static readonly Uri OfflibertyServerUri = new Uri("http://offliberty.com");
- /// <summary>
- /// The Uri of the page on the server that will be checked to see if the server is ready.
- /// </summary>
- private static readonly Uri WaitUri = new Uri(OfflibertyServerUri, "wait.php");
- /// <summary>
- /// The Uri of the page on the server that we will be using to send the media arguments to.
- /// </summary>
- private static readonly Uri Off54Uri = new Uri(OfflibertyServerUri, "off54.php");
- /// <summary>
- /// The Regex instance that will be used to scrape the converted media data url.
- /// </summary>
- private static readonly Regex UrlRegex = new Regex("\"([^\"]*)\" class=\"download\">Right-click", RegexOptions.Compiled);
- /// <summary>
- /// The CookieContainer instance used to store the cookies between the server.
- /// </summary>
- private static CookieContainer _cookieContainer;
- /// <summary>
- /// The HttpClient instance used to convert the videos using the offlibery website.
- /// </summary>
- private static readonly HttpClient Client;
- /// <summary>
- /// OfflibertyConverter initializer.
- /// </summary>
- static OfflibertyConverter()
- {
- _cookieContainer = new CookieContainer();
- Client = new HttpClient(new HttpClientHandler
- { CookieContainer = _cookieContainer });
- }
- /// <summary>
- /// Returns the converted version of the content in mediaUrl into Mp3 or Mp4 form.
- /// </summary>
- /// <param name="mediaUri">The media that will get converted ino the specificed media type.</param>
- /// <param name="mediaType">The media type that the media will get converted into.</param>
- /// <returns></returns>
- public static async Task<OfflibertyResponse> Convert(Uri mediaUri, MediaType mediaType)
- {
- try
- {
- // Send the initial response to check if the server is available.
- var waitingOneResponse = await Client.GetAsync(WaitUri);
- var waitingOneResponseContent = await waitingOneResponse.Content.ReadAsStringAsync();
- // Check if the waiting response starts with "Wait". This verifies if the server is available to convert the file.
- if (!waitingOneResponseContent.StartsWith("Wait"))
- return new OfflibertyResponse(false, "Server is not available");
- // Declare data without the "video_file" key
- var data = new Dictionary<string, string> { {"track", mediaUri.ToString()} };
- // If the specificed media type is an MP4 add the "video_file" key to the data.
- if (mediaType == MediaType.Mp4)
- data.Add("video_file", "1");
- // Declare postData with data as the first parameter to the FormUrlEncodedContent constructor.
- var postData = new FormUrlEncodedContent(data);
- // Send the post data to the server.
- var off54Response = await Client.PostAsync(Off54Uri, postData);
- var off54ResponseContent = await off54Response.Content.ReadAsStringAsync();
- // Checks the source if it contains the media file with the UrlRegex instance.
- // Send the "OfflibertyResponse" with the status "false" if the url is not matched.
- if (!UrlRegex.IsMatch(off54ResponseContent))
- return new OfflibertyResponse(false, "Converted media url is not found.");
- var url = UrlRegex.Match(off54ResponseContent).Groups[1].Value;
- var convertedMediaUrl = string.Join(".", url.Substring(0, url.LastIndexOf('.')),
- mediaType.ToString().ToLower());
- // Send the "OfflibertyResponse" along with the converted media Url.
- return new OfflibertyResponse(true, convertedMediaUrl);
- }
- catch (Exception ex)
- { return new OfflibertyResponse(false, "Exception Occured", ex); }
- finally
- { _cookieContainer = new CookieContainer(); }
- }
- }
- /// <summary>
- /// Class that returns whether a exception occured or if the convertion was successful.
- /// </summary>
- public class OfflibertyResponse
- {
- /// <summary>
- /// The boolean that states wheter the convertion was successful or not.
- /// </summary>
- public bool Status { get; private set; }
- /// <summary>
- /// The string that stores an input given. (This may be an error message or the converted media url)
- /// </summary>
- public string Message { get; private set; }
- /// <summary>
- /// The exception that has been thrown when trying to convert media.
- /// </summary>
- public Exception Exception { get; private set; }
- /// <summary>
- /// The constructor that initializes the OfflibertyResponse class.
- /// </summary>
- /// <param name="status"></param>
- /// <param name="message"></param>
- /// <param name="exception"></param>
- public OfflibertyResponse(bool status, string message = null, Exception exception = null)
- {
- Status = status;
- Message = string.IsNullOrWhiteSpace(message) ? string.Empty : message;
- Exception = exception;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment