Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for publishing to a publication #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -55,6 +55,15 @@ $ medium login
· Saved!
```

### `publications`

```
$ medium publications
List of medium publications (id - url)
c8sdks9df - https://medium.com/publication_url

```

This command asks you for your Integration Token (which can be retrieved at the bottom of this [page](https://medium.com/me/settings)). It also auths into Medium to get your user information. Lastly, it saves the token, your `userId`, and your URL to your ~/.netrc file.

### `publish`
Expand Down Expand Up @@ -91,7 +100,7 @@ license: all-rights-reserved

Note that `title` is the only field that is required in the front matter, with all the other ones optional. For a list of the accepted possible parameters, see Medium's API documentation [here](https://github.com/Medium/medium-api-docs/#creating-a-post). `contentFormat` and `content` are not required in the front matter, as they are both added later by the `medium publish` command.

For each successful post, the `medium publish` command also adds `published: true` to the front matter.
For each successful post, the `medium publish` command also adds `published: true` to the front matter. Note that you can add a `publicationID` to the front matter to publish to a publication instead of your personal blog.

### `open`

Expand Down
11 changes: 6 additions & 5 deletions bin/medium
Expand Up @@ -21,10 +21,11 @@ program
program.on('--help', function(){
console.log(' Commands:');
console.log();
console.log(' medium create create a local blog for the first time');
console.log(' medium login authenticate Medium account with an access token');
console.log(' medium publish publish new blog posts to Medium');
console.log(' medium open open your Medium in the browser');
console.log(' medium create create a local blog for the first time');
console.log(' medium login authenticate Medium account with an access token');
console.log(' medium publish publish new blog posts to Medium');
console.log(' medium open open your Medium in the browser');
console.log(' medium publications lists your publications');
console.log();
});

Expand Down Expand Up @@ -54,4 +55,4 @@ if (!exists(bin)) {
*/

var child = spawn(bin, args, { stdio: 'inherit' });
child.on('close', process.exit.bind(process));
child.on('close', process.exit.bind(process));
56 changes: 56 additions & 0 deletions bin/medium-publications
@@ -0,0 +1,56 @@
#!/usr/bin/env node

var logger = require('../lib/logger');
var medium = require('../lib/medium');
var resolve = require('path').resolve;
var program = require('commander');
var prompt = require('co-prompt');
var netrc = require('node-netrc');
var chalk = require('chalk');
var co = require('co');

/**
* Program
*/

program
.parse(process.argv);

/**
* Look for new articles.
*/

co(function*() {

/**
* Get token from .netrc
*/

var auth = netrc('api.medium.com');
if (!auth.token) {
logger.log('Looks like you haven\'t set your Integration Token yet!');
logger.log('Run `medium login` for more information.');
process.exit(1);
}



/**
* Get Publications
*/

try {
let res = yield medium.getPublications(auth.token, auth.userId);
let data = res.body.data
console.log("List of medium publications (id - url)")
data.forEach((d) => {
console.log(`${d.id} - ${d.url}`);
});
} catch (e) {
console.log(e)
var err = JSON.parse(e.response.error.text);
logger.fatal('Had trouble getting Publications from Medium: %s', err.errors[0].message);
}

process.exit(0);
});
15 changes: 14 additions & 1 deletion lib/medium.js
Expand Up @@ -28,14 +28,27 @@ exports.userId = function(token) {
};
}

exports.getPublications = function(token, userId) {
return function(fn) {
request
.get(uri + 'users/' + userId + '/publications')
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.set('Accept-Charset', 'utf-8')
.end(fn);
}
}

/**
* Publish a post.
*/

exports.publish = function(token, userId, post) {
return function(fn) {
let url = !!post.publicationID ? uri + '/publications/' + publicationID + '/posts' : uri + 'users/' + userId + '/posts';
request
.post(uri + 'users/' + userId + '/posts')
.post(url)
.send(clean(post))
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand Down