Coder Social home page Coder Social logo

cs.pastebin-api's Introduction

Pastebin API for C#

Nuget Nuget

This is a simple library for accessing Pastebin from C#

Example

using System;
using System.Threading.Tasks;
using PastebinAPI;

namespace Example
{
    class Example
    {
        static void Main()
        {
            PastebinExample().GetAwaiter().GetResult();
        }

        static async Task PastebinExample()
        {
            //before using any class in the api you must enter your api dev key
            Pastebin.DevKey = "your dev key goes here";
            //you can see yours here: https://pastebin.com/api#1
            try
            {
                // login and get user object
                User me = await Pastebin.LoginAsync("user", "pass");
                // user contains information like e-mail, location etc...
                Console.WriteLine("{0}({1}) lives in {2}", me, me.Email, me.Location);
                // lists all pastes for this user
                foreach (Paste paste in await me.ListPastesAsync(3)) // we limmit the results to 3
                {
                    Console.WriteLine(paste.Title);
                }

                string code = "<your fancy &code#() goes here>";
                //creates a new paste and get paste object
                Paste newPaste = await me.CreatePasteAsync(code, "MyPasteTitle", Language.HTML5, Visibility.Public, Expiration.TenMinutes);
                //newPaste now contains the link returned from the server
                Console.WriteLine("URL: {0}", newPaste.Url);
                Console.WriteLine("Paste key: {0}", newPaste.Key);
                Console.WriteLine("Content: {0}", newPaste.Text);
                //deletes the paste we just created
                await me.DeletePasteAsync(newPaste);

                //lists all currently trending pastes(similar to me.ListPastes())
                foreach (Paste paste in await Pastebin.ListTrendingPastesAsync())
                {
                    Console.WriteLine("{0} - {1}", paste.Title, paste.Url);
                }
                //you can create pastes directly from Pastebin static class but they are created as guests and you have a limited number of guest uploads
                Paste anotherPaste = await Paste.CreateAsync("another paste", "MyPasteTitle2", Language.CSharp, Visibility.Unlisted, Expiration.OneHour);
                Console.WriteLine(anotherPaste.Title);
            }
            catch (PastebinException ex) //api throws PastebinException
            {
                //in the Parameter property you can see what invalid parameter was sent
                //here we check if the exeption is thrown because of invalid login details
                if (ex.Parameter == PastebinException.ParameterType.Login)
                {
                    Console.Error.WriteLine("Invalid username/password");
                }
                else
                {
                    throw; //all other types are rethrown and not swalowed!
                }
            }
            Console.ReadKey();
        }
    }
}

cs.pastebin-api's People

Contributors

nikibobi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.