Skip to content

Commit

Permalink
Merge pull request #7 from microlinkhq/examples
Browse files Browse the repository at this point in the history
docs: add examples section
  • Loading branch information
Kikobeats committed Mar 27, 2019
2 parents 67251ab + 23c2818 commit 0ab1a4c
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 1 deletion.
34 changes: 34 additions & 0 deletions bin/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const createGradient = require('gradient-string')
const jsome = require('jsome')
const chalk = require('chalk')

const gradient = createGradient([
{ color: '#F76698', pos: 0 },
{ color: '#EA407B', pos: 0.29 },
{ color: '#654EA3', pos: 1 }
])

const pink = chalk.hex('#EA407B')

jsome.colors = {
num: 'cyan',
str: 'green',
bool: 'red',
regex: 'blue',
undef: 'grey',
null: 'grey',
attr: 'reset',
quot: 'gray',
punc: 'gray',
brack: 'gray'
}

module.exports = {
gradient,
jsome,
pink,
gray: chalk.gray,
white: chalk.white
}
26 changes: 26 additions & 0 deletions bin/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const path = require('path')
const fs = require('fs')

const { gray, gradient } = require('./colors')

const examplesPath = path.resolve(__dirname, '../examples')

const examples = fs.readdirSync(examplesPath).map(name => {
const example = require(`${examplesPath}/${name}`)
return ` ${name} ${example.help}`
})

module.exports = gray(`${gradient('Microlink Query Language')}
Usage
$ mql-run <example>
Flags
--copy copy output to clipboard. [default=false]
--quiet don't show additional information. [default=false]
Examples Availables
${examples}
`)
83 changes: 83 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env node
'use strict'

const indentString = require('indent-string')
const terminalLink = require('terminal-link')
const beautyError = require('beauty-error')
const prettyBytes = require('pretty-bytes')
const existsFile = require('exists-file')
const clipboardy = require('clipboardy')
const sizeof = require('object-sizeof')
const prettyMs = require('pretty-ms')
const timeSpan = require('time-span')
const { isEmpty } = require('lodash')
const path = require('path')

const { gray, jsome, gradient, pink } = require('./colors')

const pkg = require('../package.json')

require('update-notifier')({ pkg }).notify()

const cli = require('meow')({
pkg,
description: false,
help: require('./help')
})

const exitOnError = err => {
console.log()
console.error(beautyError(err))
process.exit(1)
}

const main = async () => {
const [exampleName] = cli.input

if (!exampleName) cli.showHelp()

const filepath = path.resolve(__dirname, '../examples', exampleName)

if (!existsFile.sync(filepath)) {
throw TypeError(`Example '${exampleName}' not exist.`)
}

const fn = require(filepath)

if (isEmpty(cli.flags)) {
console.log(gray(`\n${indentString(fn.help, 2)}`))
console.log(gray(` \n Flags${indentString(fn.flags, 2)}`))
process.exit()
}

const end = timeSpan()
return [await fn({ query: cli.flags }), end()]
}

main()
.then(([data, time]) => {
jsome(data)

if (!cli.flags.quiet) {
console.log(`\n `, gradient('Microlink Query Language'), '\n')
console.log(
` ${pink('source:')} ${terminalLink(
`examples/${cli.input[0]}`,
`https://github.com/microlinkhq/mql/tree/master/examples/${
cli.input[0]
}`
)}`
)
console.log(` ${pink('status:')} success`)
console.log(` ${pink('size:')} ${prettyBytes(sizeof(data))}`)
console.log(` ${pink('time:')} ${prettyMs(time)}`)
}

if (cli.flags.copy) {
clipboardy.writeSync(JSON.stringify(data, null, 2))
console.log(`\n ${gray('Copied to clipboard!')}`)
}

process.exit()
})
.catch(exitOnError)
65 changes: 65 additions & 0 deletions examples/twitter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

const mql = require('@microlink/mql')

module.exports = async ({ query }) => {
const { username, force = false } = query

if (!username) {
throw new TypeError(`You need to pass 'username' as query parameter `)
}

const { data } = await mql(`https://twitter.com/${username}`, {
force,
rules: {
avatarUrl: {
type: 'image',
selectors: {
selector: '.ProfileAvatar-image',
attr: 'src'
}
},
bio: {
selectors: {
selector: '.ProfileHeaderCard-bio',
attr: 'text'
}
},
name: {
selectors: {
selector: '.ProfileHeaderCard-nameLink',
attr: 'text'
}
},
tweetsIds: {
selectors: {
selector: 'ol > li a',
attr: 'data-conversation-id'
}
},
tweetsText: {
selectors: {
selector: 'ol > li p',
attr: 'text'
}
}
}
})

const { tweetsIds, tweetsText, bio, name, avatarUrl } = data

const tweets = tweetsIds.map((id, index) => ({
id,
text: tweetsText[index]
}))

const [pinnedTweet, ...restTweets] = tweets

return { pinnedTweet, tweets: restTweets, bio, name, avatarUrl }
}

module.exports.help = 'Get the Twitter profile for any twitter username.'

module.exports.flags = `
--username Twitter username for fetching profile. [required]
`
9 changes: 9 additions & 0 deletions examples/twitter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "mql-twitter",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@microlink/mql": "latest"
}
}
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"version": "0.3.0",
"browser": "src/browser.js",
"main": "src/node.js",
"bin": {
"mql-run": "./bin/index.js"
},
"author": {
"email": "josefrancisco.verdu@gmail.com",
"name": "Kiko Beats",
Expand Down Expand Up @@ -39,18 +42,29 @@
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"ava": "latest",
"beauty-error": "latest",
"chalk": "latest",
"ci-publish": "latest",
"clipboardy": "latest",
"conventional-github-releaser": "latest",
"coveralls": "latest",
"esm": "latest",
"exists-file": "latest",
"finepack": "latest",
"git-authors-cli": "latest",
"git-dirty": "latest",
"gradient-string": "latest",
"husky": "latest",
"indent-string": "latest",
"jsome": "latest",
"lint-staged": "latest",
"meow": "latest",
"npm-check-updates": "latest",
"nyc": "latest",
"object-sizeof": "latest",
"prettier-standard": "latest",
"pretty-bytes": "latest",
"pretty-ms": "latest",
"rollup-plugin-babel": "latest",
"rollup-plugin-commonjs": "latest",
"rollup-plugin-filesize": "latest",
Expand All @@ -59,7 +73,9 @@
"rollup-plugin-terser": "latest",
"rollup-plugin-visualizer": "latest",
"standard": "latest",
"standard-version": "latest"
"standard-version": "latest",
"terminal-link": "latest",
"time-span": "latest"
},
"engines": {
"node": ">= 8"
Expand Down

0 comments on commit 0ab1a4c

Please sign in to comment.