Skip to content

Commit

Permalink
[#86] - Added segmentfault support and new test article contains lots…
Browse files Browse the repository at this point in the history
… of images.
  • Loading branch information
JimmyLv committed Nov 20, 2016
1 parent bee930c commit a6d4ace
Show file tree
Hide file tree
Showing 5 changed files with 533 additions and 4 deletions.
5 changes: 3 additions & 2 deletions scripts/auto_publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const nightmare = require('./nightmare')
const readFile = (path) => fs.readFileSync(path, { encoding: 'utf-8' })

const config = jsyaml.load(readFile('scripts/config.yml'))
const result = readFile('source/_posts/hello-world.md').split('---')
const result = readFile('source/_posts/2016-09-16-sorting-out-knowledge-from-information.md').split('---')
const article = {
meta: jsyaml.load(result[1]),
content: result.slice(2).join('---')
}

const { jianshu, zhihu } = config.accounts
const { jianshu, zhihu, segmentfault } = config.accounts

require('./jianshu')(nightmare, jianshu, article)
require('./segmentfault')(nightmare, segmentfault, article)
4 changes: 2 additions & 2 deletions scripts/jianshu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TIMES_TO_DELAY = 5000 // need decrease/increase deps on network.
const TIMES_TO_DELAY = 3000 // need decrease/increase deps on network.

function publish(nightmare, { username, password }, { meta, content }) {
nightmare
Expand Down Expand Up @@ -26,7 +26,7 @@ function publish(nightmare, { username, password }, { meta, content }) {
.wait(TIMES_TO_DELAY) // publish button will hidden when saving draft
.wait('#publish-button')
.click('#publish-button')
.wait(TIMES_TO_DELAY) // publish process need some times
.wait(TIMES_TO_DELAY * 2) // publish process need some times
.evaluate(() => {
const success = $('h3:contains("文章发布成功")')[0]
if (success) {
Expand Down
44 changes: 44 additions & 0 deletions scripts/segmentfault.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const TAGS = require('./tags.json')

const TIMES_TO_DELAY = 2000 // need decrease/increase deps on network.

function publish(nightmare, { username, password }, { meta, content }) {
nightmare
.goto('https://segmentfault.com/write')
.exists('.user-avatar') // already login?
.then(isLogin => {
console.info('already login?', isLogin)
if (!isLogin) {
nightmare
.click('a.SFLogin')
.wait('h1.h4.text-muted.login-title')
.insert('input[name="username"]', username)
.insert('input[name="password"]', password)
.click('button.btn.btn-primary.pull-right.pl20.pr20')
}
// will redirect to write page directly
nightmare
.wait('#myTitle')
.insert('#myTitle', meta.title)
.insert('textarea#myEditor', content)
.evaluate(() => $('input.tagsInput').removeClass('hidden'))
.then(
nightmare
.insert('input.tagsInput', TAGS['开发语言'][0].id) // javascript
.click('#publishIt')
.wait(TIMES_TO_DELAY) // publish need some times
.url()
.then(url => {
if (url === 'https://segmentfault.com/write') {
throw new Error('文章发布异常...')
}
console.info(`发布成功 => [${meta.title}](${url})`)
return nightmare.end()
}
)
.catch(err => console.error(err))
)
})
}

module.exports = publish
Loading

0 comments on commit a6d4ace

Please sign in to comment.