|
| 1 | +import inquirer from 'inquirer'; |
| 2 | +import { tokenGuard } from './functions.js'; |
| 3 | +import fetch from 'node-fetch'; |
| 4 | +import { LOGIN_LINK, TABLE_HEADERS } from './constant.js'; |
| 5 | +import { rmSync, writeFileSync } from 'fs'; |
| 6 | +import { homedir } from 'os'; |
| 7 | +import { JSDOM } from 'jsdom'; |
| 8 | +import { table } from 'table'; |
| 9 | + |
| 10 | +export async function loginUser(argv, apiToken) { |
| 11 | + if (tokenGuard(apiToken)) { |
| 12 | + console.log( |
| 13 | + 'Please provide your pastebin.com API token in the ~/.pasty.api file.' |
| 14 | + ); |
| 15 | + process.exit(1); |
| 16 | + } |
| 17 | + |
| 18 | + const username = argv.username; |
| 19 | + const password = ( |
| 20 | + await inquirer.prompt([ |
| 21 | + { |
| 22 | + type: 'password', |
| 23 | + message: 'enter your password:', |
| 24 | + name: 'password', |
| 25 | + mask: '*', |
| 26 | + }, |
| 27 | + ]) |
| 28 | + ).password; |
| 29 | + |
| 30 | + const response = await fetch(LOGIN_LINK, { |
| 31 | + body: `api_dev_key=${apiToken}&api_user_name=${username}&api_user_password=${password}`, |
| 32 | + headers: { |
| 33 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 34 | + }, |
| 35 | + method: 'POST', |
| 36 | + }); |
| 37 | + |
| 38 | + if (response.status === 200) { |
| 39 | + const userToken = await response.text(); |
| 40 | + writeFileSync(`${homedir()}/.pasty.user`, userToken, 'utf-8'); |
| 41 | + console.log(`Success! You're now logged in as ${username}`); |
| 42 | + } else { |
| 43 | + console.log(await response.text()); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export async function listPastes(argv, apiToken, userToken) { |
| 49 | + if (tokenGuard(apiToken)) { |
| 50 | + console.log( |
| 51 | + 'Please provide your pastebin.com API token in the ~/.pasty.api file.' |
| 52 | + ); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + if (tokenGuard(userToken)) { |
| 57 | + console.log('Please login first via pasty login <username>'); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + |
| 61 | + const amount = argv.amount; |
| 62 | + |
| 63 | + const response = await fetch('https://pastebin.com/api/api_post.php', { |
| 64 | + body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=list&api_results_limit=${amount}`, |
| 65 | + headers: { |
| 66 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 67 | + }, |
| 68 | + method: 'POST', |
| 69 | + }); |
| 70 | + const data = await response.text(); |
| 71 | + |
| 72 | + if (response.status === 200) { |
| 73 | + const tableData = []; |
| 74 | + tableData.push(TABLE_HEADERS); |
| 75 | + |
| 76 | + const xmlResponse = new JSDOM(data); |
| 77 | + |
| 78 | + xmlResponse.window.document.querySelectorAll('paste').forEach((paste) => { |
| 79 | + const pasteRow = []; |
| 80 | + |
| 81 | + pasteRow.push(paste.querySelector('paste_key').textContent); |
| 82 | + pasteRow.push(paste.querySelector('paste_title').textContent); |
| 83 | + pasteRow.push(paste.querySelector('paste_private').textContent); |
| 84 | + pasteRow.push(paste.querySelector('paste_expire_date').textContent); |
| 85 | + pasteRow.push(paste.querySelector('paste_format_short').textContent); |
| 86 | + |
| 87 | + tableData.push(pasteRow); |
| 88 | + }); |
| 89 | + |
| 90 | + console.log(table(tableData)); |
| 91 | + } else { |
| 92 | + console.log('Could not get pastes: ', data); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export function logout() { |
| 97 | + try { |
| 98 | + rmSync(`${homedir()}/.pasty.user`); |
| 99 | + console.log('Success! Logged you out.'); |
| 100 | + } catch (e) { |
| 101 | + console.log("You're currently not logged in"); |
| 102 | + } |
| 103 | +} |
0 commit comments