Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
# gab.ai

A wrapper for the gab.ai API
A wrapper for the gab.ai API.

| Service | Status |
| --- | --- |
| CircleCI | [![CircleCI](https://circleci.com/gh/lyndsysimon/gab.svg?style=svg)](https://circleci.com/gh/lyndsysimon/gab) |

If you find this project useful: [![SayThanks.io](https://img.shields.io/badge/SayThanks.io-%E2%98%BC-1EAEDB.svg)](https://saythanks.io/to/lyndsysimon)
This is a fork from https://saythanks.io/to/lyndsysimon. Please thank him.

## Usage

The API is exposed as a single `Gab` object, which must be initialized with an authorized JWT.
The API is exposed as a single `Gab` object, which must be initialized with an authorized JWT. This is a token that is set as "HttpOnly", so it's generally not visible and queryable through JavaScript. Use your browser's features to display secure cookies and copy the value of the cookie named "remember_..." for the domain "Gab.ai" (for example, in Chrome, under Application -> Storage -> Cookies in the developer tools).


```javascript
var Gab = require('gab.ai').Gab;

var api = new Gab({
authToken: '<your JWT>'
})
var api = new Gab('<your JWT>')
```

Once you have an instance of the Gab object, calling each API method will return a Promise object. For example, to get information for a user:

```javascript
// Get information about a user.
api.getUser('a')
.then(function(data) {
console.log(data);
}).catch(function() {
console.log('Request failed.');
});

// Submit a post.
// The ID of the GIF can be copied from the URL used for sharing the GIPHY GIF you want to
// attach, such as 'nO2ttDiHPqEP6' in 'https://media.giphy.com/media/nO2ttDiHPqEP6/giphy.gif'
// or queried from their own API.
api.putPost({body: "<p>This is the message of the post</p>", gif: '3PrOqZRpbmqFa'})
.then(function(data) {
console.log(data);
}).catch(function() {
console.log('Request failed.');
});

// Get information about a post.
api.getPost('23916771')
.then(function(data) {
console.log(data);
}).catch(function() {
console.log('Request failed.');
});
```

For information on Promise objects, see the [documentation for request-promise](https://github.com/request/request-promise).
Expand Down
147 changes: 138 additions & 9 deletions lib/Gab.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,152 @@
var rp = require('request-promise');

function Gab(options) {
this.options = options;

function Gab(token) {
this._baseRequest = rp.defaults({
baseUrl: 'https://gab.ai/',
//baseUrl: 'http://localhost:8888/',
followRedirect: false,
headers: {
authorization: 'Bearer ' + this.options.authToken
authorization: 'Bearer ' + token
},
transform2xxOnly: true,
transform: function(content) { return JSON.parse(content) }
})
}
json: true
});
};

Gab.prototype.getUser = function (username) {
return this._baseRequest('/users/' + username);
};

Gab.prototype.getFeed = function () {
return this._baseRequest('/feed/');
};

Gab.prototype.getPost = function (postid) {
return this._baseRequest('/posts/' + postid);
};

Gab.prototype.getConversation = function (postid) {
return this._baseRequest('/posts/'+postid+'/conversation');
};

Gab.prototype.getComments = function (postid) {
return this._baseRequest('/posts/'+postid+'/comments');
};

Gab.prototype.getTopics = function () {
return this._baseRequest('/api/topics/');
};

Gab.prototype.getTopic = function (topic_id) {
return this._baseRequest('/api/topics/'+topic_id);
}

Gab.prototype.getTopicsArchive = function () {
return this._baseRequest('/api/topics/archive/');
};

Gab.prototype.getFavoriteFeeds = function () {
return this._baseRequest('/api/favorite-feeds/');
};

Gab.prototype.getFeatured = function () {
return this._baseRequest('/api/tv/featured');
};

Gab.prototype.getBroadcasts = function () {
return this._baseRequest('/api/broadcasts/');
};

Gab.prototype.getUserVideos = function (user) {
return this._baseRequest('/api/users/'+user+'/tv');
};

Gab.prototype.getNotifications = function () {
return this._baseRequest('/api/notifications/');
};

Gab.prototype.getCategory = function (category, sort = 'score', include_replies = 0) {
return this._baseRequest({
uri: '/api/category/'+category,
qs: {
sort: sort,
include_replies: include_replies
}
});
};

Gab.prototype.getPopularUsers = function () {
return this._baseRequest('/popular/users');
};

Gab.prototype.followUser = function (user_id) {
return this._baseRequest({
method: 'POST',
uri: '/users/'+user_id+'/follow',
body: {}
});
};

Gab.prototype.unFollowUser = function (user_id) {
return this._baseRequest({
method: 'POST',
uri: '/users/'+user_id+'/follow',
body: {
_method: 'delete'
}
});
};

Gab.prototype.addFavoriteFeed = function(feed_id) {
return this._baseRequest({
method: 'POST',
uri: '/api/favorite-feeds',
body: {},
qs: {
feed_type: 'topic',
feed_id: feed_id
}
});
}

Gab.prototype.removeFavoriteFeed = function(feed_id) {
return this._baseRequest({
method: 'POST',
uri: '/api/favorite-feeds',
body: {
_method: 'delete'
},
qs: {
feed_type: 'topic',
feed_id: feed_id
}
});
}

Gab.prototype.putPost = function(options) {
let body = {
body: "",
reply_to: "",
is_quote: "0",
is_html: "1",
nsfw: "0",
is_premium: "0",
_method: "post",
gif: "",
category: null,
topic: null,
share_facebook: null,
share_twitter: null,
media_attachments: []
};
// Override template values with options.
for(var key in options) body[key] = options[key];
return this._baseRequest({
method: 'POST',
uri: '/posts/',
body: body
});
};

if (!(typeof exports === 'undefined')) {
exports.Gab = Gab;
exports.Gab = Gab;
}