Giter Club home page Giter Club logo

better-pastebin's Introduction

better-pastebin

NPM

The Pastebin API wrapper Node deserves. better-pastebin is the most fully-featured module available for connecting to pastebin.com, both to its API and directly to the site itself for accomplishing tasks that the API doesn't support. It uses cheerio, request, and xml2js.

Installation

npm install better-pastebin

Example

var paste = require("better-pastebin");

paste.setDevKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
paste.login("username", "password", function(success, data) {
    if(!success) {
        console.log("Failed (" + data + ")");
        return false;
    }

    paste.create({
        contents: "test contents",
        name: "test paste",
        privacy: "1"
    }, function(success, data) {
        if(success) {
            //data contains the URL of the created paste
        } else {
            //data contains an Error object indicating why the creation failed
        }
    });
});

Usage

Setting the devkey

paste.setDevKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

This is necessary before performing any other API call. It's synchronous, so it doesn't require a callback. You can find your devkey on pastebin.com after signing in.

Logging in

paste.login(username, password, function(success, data) {
    //data contains this session's userkey
});

Logs in to Pastebin. Any actions requiring a login should be placed within the callback.

Getting a paste

paste.get(paste_id, function(success, data) {
    //data contains the contents of the paste 
});

Gets the contents of a paste. Either the ID or the URL of a paste can be supplied.

Creating a paste

paste.create(options, function(success, data) {
    //data contains the URL of the created paste
});

Creates a new paste. You do not need to be logged in to do this, but you can only create global or unlisted pastes as a guest. options can either be the contents of the paste as a string, or an object containing one or more of the following keys:

  • options.contents: The contents of the paste; this is the only mandatory property.
  • options.anonymous (default: false): If true, the paste will be created as a guest even if logged in.
  • options.expires (default: "N"): When the paste should expire.
  • options.format (default: "text"): The syntax highlighting of the paste.
  • options.privacy (default: "0"): The privacy of the paste.
  • options.name (default: ""): The name of the paste.

Creating a paste from a file

paste.createFromFile(options, function(success, data) {
    //data contains the URL of the created paste
});

Creates a new paste from the contents of a file. This works the same way that paste.create does, except that options.path should be specified instead of options.contents, or options should be the path to the file you want to upload as a string. It also accepts an additional key, options.encoding:

  • options.encoding (default: "utf8"): The encoding of the file.

Editing a paste

paste.edit(paste_id, options, function(success, data) {
    //data contains the new contents of the paste
});

Updates a paste's contents. You must be logged in to do this, and you can only edit a paste that you created. Either the ID or the URL of a paste can be supplied. options can either be the new contents of the paste as a string, or an object containing one or more of the following keys:

  • options.contents: The new contents of the paste.
  • options.expires: When the paste should expire.
  • options.format: The syntax highlighting of the paste.
  • options.privacy: The privacy of the paste.
  • options.name: The name of the paste.

If left unspecified, these default to the paste's existing options.

Listing the logged-in user's pastes

paste.list(limit, function(success, data) {
    //data contains an array of objects of information about each paste
});

Lists up to 1,000 of the logged-in user's created pastes. limit specifies how many pastes to return; it can be a number between 1 and 1,000, and defaults to 50. You must be logged in to do this.

Listing trending pastes

paste.listTrending(function(success, data) {
    //data contains an array of objects of information about each paste
});

Lists currently trending pastes. The array of pastes is identical in structure to the one returned by paste.list.

Deleting a paste

paste.delete(paste_id, function(success, data) {
    //data contains the ID of the deleted paste
});

Deletes a paste. You must be logged in to do this, and you can only delete a paste that you created. Either the ID or the URL of a paste can be supplied.

Getting information about the logged-in user

paste.user(function(success, data) {
    //data contains an object of information about the logged-in user
});

Returns information about the logged-in user. You must be logged in to do this.

Valid options

expires

N = Never
10M = 10 Minutes
1H = 1 Hour
1D = 1 Day
1W = 1 Week
2W = 2 Weeks
1M = 1 Month

privacy

0 = Public
1 = Unlisted
2 = Private

Note that private pastes can only be created if you are signed in. These values should be passed as a string, because 0 is a falsy value and can cause some issues.

format

Refer to http://pastebin.com/api#5 for the full list of syntax highlighting options.

License

All code in this repository is licensed under the MIT license. See LICENSE for more information.

better-pastebin's People

Contributors

dowzhong avatar hydrothermal avatar leonardosnt avatar ralyodio avatar sirdonovan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

better-pastebin's Issues

edit does nothing

Looks like edit is not supported by pastebin.com api.

Working on a fix for it. PR imminent....

need to change to https for api

Pastebin.com announced that all http urls will not work.

I'm already seeing api calls have stopped working since March. I think it may be due to this:

    api = {
        urls: {
            prelogin: "http://pastebin.com/login",
            login: "http://pastebin.com/login.php",
            apilogin: "http://pastebin.com/api/api_login.php",
            raw: "http://pastebin.com/raw/",
            edit: "http://pastebin.com/edit",
            post: "http://pastebin.com/api/api_post.php"
        }
    },

These should all be https

Will there be Dependencies update?

I have one project which uses this awesome module to store data on pastebin & retireve data on pastebin.
And this was the exact module which actually fetched pastebin contents. Other modules, as far as I could search, didn't.

Coming to actual point, some of the dependencies are having security issues which are fixed in newer versions.
I'm still new to Javascript so this stuff felt complicated when I forked this repo & went through the code.

Will there be any update on dependencies?

Also can there be something called "404 page detecion" ?
Because wrong links crashed by project & I cannot prevent users from entering wrong links ๐Ÿ˜…

Edit 1: my bot didn't parse thing properly hence crashed. But it would still help to get 404 detection

Editing a paste resets the existing options

The docs say:

If left unspecified, these [options] default to the paste's existing options.

But when I try to edit an existing paste it seems to reset all the options I don't specify:

  • it resets the privacy to public
  • it resets the name to "Untitled"
  • it resets the format to "" (tested with the format originally set to "lua")

This is also the case if you pass only the new contents as a string.

Presumably the rest of the options such as expiration are reset as well, although I haven't tested this.

Edit: to clarify, if I supply the paste's original options object to the edit function it keeps all of them except format. I tested this with these options:

{
      "anonymous": false,
      "expires": "N",
      "format": "lua",
      "privacy": "1",
      "name": "test"
}

Support for folders

Pastebin PRO lets you select a folder to put your paste in, or create a new one if it doesn't exist.
You can select the folder on the paste creation and edit pages.

I can look into adding support for this feature in better-pastebin, although I'm not sure where to start as it's not listed in the API docs.

'edit' functionality not working

edit seems to be broken. It's returning success, but the specified paste is not actually modified. I've created and read a paste from within the callback which confirmed the dev key and login credentials are valid.

var paste = require('better-pastebin');
paste.setDevKey('***');
paste.login('***', '***', function(success, data) {
  if(!success) {
    console.log('Login Failed (' + data + ')');
      return false;
  }

  paste.edit('***', 'i am editing this',  function(success, data) {
    if(!success) {
      console.log('Edit Failed (' + data + ')');
    } else {
      console.log('Edit Succeeded (' + data + ')');
    }
  });
});

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.