First commit

This commit is contained in:
Andros Fenollosa 2019-01-13 13:32:02 +01:00
parent 7ea8499ae3
commit 93f244c701
4 changed files with 140 additions and 1 deletions

14
Pipfile Normal file
View File

@ -0,0 +1,14 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
pick = "*"
click = "*"
pyperclip = "*"
[dev-packages]
[requires]
python_version = "3.6"

20
Pipfile.lock generated Normal file
View File

@ -0,0 +1,20 @@
{
"_meta": {
"hash": {
"sha256": "415dfdcb118dd9bdfef17671cb7dcd78dbd69b6ae7d4f39e8b44e71d60ca72e7"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.6"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {},
"develop": {}
}

View File

@ -1,2 +1,3 @@
# get-my-pastebin
# Get-my-pastebin
Terminal application to find and copy your own Paste for Pastebin

104
get-pastebin.py Normal file
View File

@ -0,0 +1,104 @@
import click
from pick import pick
import urllib.request
import urllib.parse
import xml.etree.ElementTree as ET
import pyperclip
API_RESULTS_LIMIT = 1000
LOGIN_URL = 'https://pastebin.com/api/api_login.php'
API_LIST_URL = f'https://pastebin.com/api/api_post.php'
API_RAW_PASTE_URL = 'https://pastebin.com/api/api_raw.php'
@click.command()
@click.option('--api-key', required=True, help='API Dev key')
@click.option('--username', required=True, help='Your username account')
@click.option('--password', required=True, help='Your password account')
@click.argument('search')
def main(api_key, username, password, search):
"""Main application"""
user_key = get_user_key(api_key, username, password)
title = f'Results: {search}'
options = get_list(api_key, user_key, search)
if not options:
click.echo('Not results')
else:
# Show list to pick item
def get_label(option): return option.get('label')
option, index = pick(options, title, indicator='*', options_map_func=get_label)
raw_paste = get_raw_paste(api_key, user_key, option['paste_key'])
# Print raw paste
click.echo(raw_paste)
# Copy to clipboard
pyperclip.copy(raw_paste)
def get_user_key(api_key, username, password):
"""Get login key"""
post_url = 'http://localhost:3000/vim'
headers = {}
headers['Content-Type'] = 'application/json'
post_data = urllib.parse.urlencode(
{
'api_dev_key' : api_key,
'api_user_name': username,
'api_user_password': password
}
).encode('ascii')
post_response_user_key = urllib.request.urlopen(url=LOGIN_URL, data=post_data)
return post_response_user_key.read().decode("utf-8")
def get_list(api_key, user_key, search):
# Get all list
post_data = urllib.parse.urlencode(
{
'api_option': 'list',
'api_dev_key' : api_key,
'api_user_key': user_key,
'results_limit': API_RESULTS_LIMIT
}
).encode('ascii')
response_list = urllib.request.urlopen(url=API_LIST_URL, data=post_data)
data_list = '<list>' + response_list.read().decode("utf-8") + '</list>'
data_list = ET.fromstring(data_list)
# Search data
results = []
for paste in data_list:
found = False
label = ''
paste_key = ''
# Search in node. Save label and key
for item in paste:
if item.tag == 'paste_title' and search.upper() in item.text.upper():
found = True
label = item.text
if item.tag == 'paste_key':
paste_key = item.text
# Save node
if found:
results.append(
{
'label': label,
'paste_key': paste_key
}
)
return results
def get_raw_paste(api_key, user_key, paste_key):
# Get raw paste
post_data = urllib.parse.urlencode(
{
'api_option': 'show_paste',
'api_dev_key' : api_key,
'api_user_key': user_key,
'api_paste_key': paste_key
}
).encode('ascii')
response_paste = urllib.request.urlopen(url=API_RAW_PASTE_URL, data=post_data)
raw_paste = response_paste.read().decode("utf-8")
return raw_paste
if __name__ == '__main__':
main()