Skip to content

Commit 0ab1a4c

Browse files
authored
Merge pull request #7 from microlinkhq/examples
docs: add examples section
2 parents 67251ab + 23c2818 commit 0ab1a4c

File tree

6 files changed

+234
-1
lines changed

6 files changed

+234
-1
lines changed

bin/colors.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
const createGradient = require('gradient-string')
4+
const jsome = require('jsome')
5+
const chalk = require('chalk')
6+
7+
const gradient = createGradient([
8+
{ color: '#F76698', pos: 0 },
9+
{ color: '#EA407B', pos: 0.29 },
10+
{ color: '#654EA3', pos: 1 }
11+
])
12+
13+
const pink = chalk.hex('#EA407B')
14+
15+
jsome.colors = {
16+
num: 'cyan',
17+
str: 'green',
18+
bool: 'red',
19+
regex: 'blue',
20+
undef: 'grey',
21+
null: 'grey',
22+
attr: 'reset',
23+
quot: 'gray',
24+
punc: 'gray',
25+
brack: 'gray'
26+
}
27+
28+
module.exports = {
29+
gradient,
30+
jsome,
31+
pink,
32+
gray: chalk.gray,
33+
white: chalk.white
34+
}

bin/help.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const fs = require('fs')
5+
6+
const { gray, gradient } = require('./colors')
7+
8+
const examplesPath = path.resolve(__dirname, '../examples')
9+
10+
const examples = fs.readdirSync(examplesPath).map(name => {
11+
const example = require(`${examplesPath}/${name}`)
12+
return ` ${name} ${example.help}`
13+
})
14+
15+
module.exports = gray(`${gradient('Microlink Query Language')}
16+
17+
Usage
18+
$ mql-run <example>
19+
20+
Flags
21+
--copy copy output to clipboard. [default=false]
22+
--quiet don't show additional information. [default=false]
23+
24+
Examples Availables
25+
${examples}
26+
`)

bin/index.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
'use strict'
3+
4+
const indentString = require('indent-string')
5+
const terminalLink = require('terminal-link')
6+
const beautyError = require('beauty-error')
7+
const prettyBytes = require('pretty-bytes')
8+
const existsFile = require('exists-file')
9+
const clipboardy = require('clipboardy')
10+
const sizeof = require('object-sizeof')
11+
const prettyMs = require('pretty-ms')
12+
const timeSpan = require('time-span')
13+
const { isEmpty } = require('lodash')
14+
const path = require('path')
15+
16+
const { gray, jsome, gradient, pink } = require('./colors')
17+
18+
const pkg = require('../package.json')
19+
20+
require('update-notifier')({ pkg }).notify()
21+
22+
const cli = require('meow')({
23+
pkg,
24+
description: false,
25+
help: require('./help')
26+
})
27+
28+
const exitOnError = err => {
29+
console.log()
30+
console.error(beautyError(err))
31+
process.exit(1)
32+
}
33+
34+
const main = async () => {
35+
const [exampleName] = cli.input
36+
37+
if (!exampleName) cli.showHelp()
38+
39+
const filepath = path.resolve(__dirname, '../examples', exampleName)
40+
41+
if (!existsFile.sync(filepath)) {
42+
throw TypeError(`Example '${exampleName}' not exist.`)
43+
}
44+
45+
const fn = require(filepath)
46+
47+
if (isEmpty(cli.flags)) {
48+
console.log(gray(`\n${indentString(fn.help, 2)}`))
49+
console.log(gray(` \n Flags${indentString(fn.flags, 2)}`))
50+
process.exit()
51+
}
52+
53+
const end = timeSpan()
54+
return [await fn({ query: cli.flags }), end()]
55+
}
56+
57+
main()
58+
.then(([data, time]) => {
59+
jsome(data)
60+
61+
if (!cli.flags.quiet) {
62+
console.log(`\n `, gradient('Microlink Query Language'), '\n')
63+
console.log(
64+
` ${pink('source:')} ${terminalLink(
65+
`examples/${cli.input[0]}`,
66+
`https://github.com/microlinkhq/mql/tree/master/examples/${
67+
cli.input[0]
68+
}`
69+
)}`
70+
)
71+
console.log(` ${pink('status:')} success`)
72+
console.log(` ${pink('size:')} ${prettyBytes(sizeof(data))}`)
73+
console.log(` ${pink('time:')} ${prettyMs(time)}`)
74+
}
75+
76+
if (cli.flags.copy) {
77+
clipboardy.writeSync(JSON.stringify(data, null, 2))
78+
console.log(`\n ${gray('Copied to clipboard!')}`)
79+
}
80+
81+
process.exit()
82+
})
83+
.catch(exitOnError)

examples/twitter/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const mql = require('@microlink/mql')
4+
5+
module.exports = async ({ query }) => {
6+
const { username, force = false } = query
7+
8+
if (!username) {
9+
throw new TypeError(`You need to pass 'username' as query parameter `)
10+
}
11+
12+
const { data } = await mql(`https://twitter.com/${username}`, {
13+
force,
14+
rules: {
15+
avatarUrl: {
16+
type: 'image',
17+
selectors: {
18+
selector: '.ProfileAvatar-image',
19+
attr: 'src'
20+
}
21+
},
22+
bio: {
23+
selectors: {
24+
selector: '.ProfileHeaderCard-bio',
25+
attr: 'text'
26+
}
27+
},
28+
name: {
29+
selectors: {
30+
selector: '.ProfileHeaderCard-nameLink',
31+
attr: 'text'
32+
}
33+
},
34+
tweetsIds: {
35+
selectors: {
36+
selector: 'ol > li a',
37+
attr: 'data-conversation-id'
38+
}
39+
},
40+
tweetsText: {
41+
selectors: {
42+
selector: 'ol > li p',
43+
attr: 'text'
44+
}
45+
}
46+
}
47+
})
48+
49+
const { tweetsIds, tweetsText, bio, name, avatarUrl } = data
50+
51+
const tweets = tweetsIds.map((id, index) => ({
52+
id,
53+
text: tweetsText[index]
54+
}))
55+
56+
const [pinnedTweet, ...restTweets] = tweets
57+
58+
return { pinnedTweet, tweets: restTweets, bio, name, avatarUrl }
59+
}
60+
61+
module.exports.help = 'Get the Twitter profile for any twitter username.'
62+
63+
module.exports.flags = `
64+
--username Twitter username for fetching profile. [required]
65+
`

examples/twitter/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "mql-twitter",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"@microlink/mql": "latest"
8+
}
9+
}

package.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"version": "0.3.0",
66
"browser": "src/browser.js",
77
"main": "src/node.js",
8+
"bin": {
9+
"mql-run": "./bin/index.js"
10+
},
811
"author": {
912
"email": "[email protected]",
1013
"name": "Kiko Beats",
@@ -39,18 +42,29 @@
3942
"@commitlint/cli": "latest",
4043
"@commitlint/config-conventional": "latest",
4144
"ava": "latest",
45+
"beauty-error": "latest",
46+
"chalk": "latest",
4247
"ci-publish": "latest",
48+
"clipboardy": "latest",
4349
"conventional-github-releaser": "latest",
4450
"coveralls": "latest",
4551
"esm": "latest",
52+
"exists-file": "latest",
4653
"finepack": "latest",
4754
"git-authors-cli": "latest",
4855
"git-dirty": "latest",
56+
"gradient-string": "latest",
4957
"husky": "latest",
58+
"indent-string": "latest",
59+
"jsome": "latest",
5060
"lint-staged": "latest",
61+
"meow": "latest",
5162
"npm-check-updates": "latest",
5263
"nyc": "latest",
64+
"object-sizeof": "latest",
5365
"prettier-standard": "latest",
66+
"pretty-bytes": "latest",
67+
"pretty-ms": "latest",
5468
"rollup-plugin-babel": "latest",
5569
"rollup-plugin-commonjs": "latest",
5670
"rollup-plugin-filesize": "latest",
@@ -59,7 +73,9 @@
5973
"rollup-plugin-terser": "latest",
6074
"rollup-plugin-visualizer": "latest",
6175
"standard": "latest",
62-
"standard-version": "latest"
76+
"standard-version": "latest",
77+
"terminal-link": "latest",
78+
"time-span": "latest"
6379
},
6480
"engines": {
6581
"node": ">= 8"

0 commit comments

Comments
 (0)