-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from microlinkhq/examples
docs: add examples section
- Loading branch information
Showing
6 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters