internal static Paste Create(string userKey, string text, string title = null, Language language = null, Visibility visibility = Visibility.Public, Expiration expiration = null) { var result = Utills.PostRequest(Utills.URL_API, //required parameters "api_dev_key=" + Pastebin.DevKey, "api_option=" + "paste", "api_paste_code=" + Uri.EscapeDataString(text), //optional parameters "api_user_key=" + userKey, "api_paste_name=" + Uri.EscapeDataString(title ?? "Untitled"), "api_paste_format=" + (language ?? Language.Default), "api_paste_private=" + (int)visibility, "api_paste_expire_date=" + (expiration ?? Expiration.Default)); if (result.Contains(Utills.ERROR)) { throw new PastebinException(result); } var paste = new Paste(); paste.Key = result.Replace(Utills.URL, string.Empty); paste.CreateDate = DateTime.Now; paste.Title = title; paste.Size = Encoding.UTF8.GetByteCount(text); paste.ExpireDate = paste.CreateDate + expiration.Time; paste.Expiration = expiration; paste.Visibility = visibility; paste.Language = language; paste.Hits = 0; paste.Url = result; paste.Text = text; return(paste); }
/// <summary> /// Gets the raw text for a given url /// </summary> public string GetRaw() { if (Visibility == Visibility.Private) { throw new PastebinException("Private pastes can not be accessed"); } return(Text = Utills.PostRequest(Utills.URL_RAW + Key)); }
/// <summary> /// Lists the currently trending pastes /// </summary> /// <returns>Enumerable of the trending pastes</returns> public static IEnumerable <Paste> ListTrendingPastes() { var result = Utills.PostRequest(Utills.URL_API, "api_dev_key=" + DevKey, "api_option=" + "trends"); if (result.Contains(Utills.ERROR)) { throw new PastebinException(result); } return(Utills.PastesFromXML(result)); }
/// <summary> /// Deletes a paste created by this user /// </summary> public void DeletePaste(Paste paste) { var result = Utills.PostRequest(Utills.URL_API, "api_dev_key=" + Pastebin.DevKey, "api_user_key=" + userKey, "api_paste_key=" + paste.Key, "api_option=" + "delete"); if (result.Contains(Utills.ERROR)) { throw new PastebinException(result); } }
/// <summary> /// Lists all pastes created by user /// </summary> /// <param name="resultsLimit">limits the paste count</param> /// <returns>Enumerable of pastes of this user</returns> public IEnumerable <Paste> ListPastes(int resultsLimit = 50) { var result = Utills.PostRequest(Utills.URL_API, "api_dev_key=" + Pastebin.DevKey, "api_user_key=" + userKey, "api_results_limit=" + resultsLimit, "api_option=" + "list"); if (result.Contains(Utills.ERROR)) { throw new PastebinException(result); } return(Utills.PastesFromXML(result)); }
/// <summary> /// Log-in to Pastebin /// </summary> /// <returns>new User object</returns> public static User Login(string username, string password) { var result = Utills.PostRequest(Utills.URL_LOGIN, "api_dev_key=" + DevKey, "api_user_name=" + username, "api_user_password=" + password); if (result.Contains(Utills.ERROR)) { throw new PastebinException(result); } var user = new User(result); user.RequestPreferences(); return(user); }
/// <summary> /// Updates user preferences information properties /// </summary> public void RequestPreferences() { var result = Utills.PostRequest(Utills.URL_API, "api_dev_key=" + Pastebin.DevKey, "api_user_key=" + userKey, "api_option=" + "userdetails"); if (result.Contains(Utills.ERROR)) { throw new PastebinException(result); } /* Example user xml * <user> * <user_name>wiz_kitty</user_name> * <user_format_short>text</user_format_short> * <user_expiration>N</user_expiration> * <user_avatar_url>http://pastebin.com/cache/a/1.jpg</user_avatar_url> * <user_private>1</user_private> (0 Public, 1 Unlisted, 2 Private) * <user_website>http://myawesomesite.com</user_website> * <user_email>[email protected]</user_email> * <user_location>New York</user_location> * <user_account_type>1</user_account_type> (0 normal, 1 PRO) * </user>*/ XElement xuser = XElement.Parse(result); Name = xuser.Element("user_name").Value; PreferedLanguage = Language.Parse(xuser.Element("user_format_short").Value); PreferedExpiration = Expiration.Parse(xuser.Element("user_expiration").Value); PreferedVisibility = (Visibility)(int)xuser.Element("user_private"); AvatarURL = xuser.Element("user_avatar_url").Value; Website = xuser.Element("user_website").Value; Email = xuser.Element("user_email").Value; Location = xuser.Element("user_location").Value; IsPro = xuser.Element("user_account_type").Value == "1"; }