Created
July 19, 2016 06:55
-
-
Save asilachev/e822da1db27ab0463c78fd1d747dc25b to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python | |
import json | |
import os | |
import re | |
import urlparse | |
import configparser | |
import requests | |
# Since no raw_input() exists in Python 3 | |
try: | |
input = raw_input | |
except NameError: | |
pass | |
# Constants | |
POCKET_URL = 'https://getpocket.com' | |
POCKET_API_VERSION = 'v3' | |
POCKET_API_URL = '%s/%s/' % (POCKET_URL, POCKET_API_VERSION) | |
POCKET_REQUEST_TOKEN_URL = POCKET_API_URL + 'oauth/request' | |
POCKET_ACCESS_TOKEN_URL = POCKET_API_URL + 'oauth/authorize' | |
CONFIG_HEADER = 'POCKET CREDENTIALS' | |
CONFIG_FILENAME = '.config' | |
config_path = os.path.join( | |
os.path.dirname(os.path.abspath(__file__)), | |
CONFIG_FILENAME, | |
) | |
def read_env(): | |
try: | |
with open('.env') as f: | |
content = f.read() | |
except IOError: | |
content = '' | |
for line in content.splitlines(): | |
m1 = re.match(r'\A([A-Za-z_0-9]+)=(.*)\Z', line) | |
if m1: | |
key, val = m1.group(1), m1.group(2) | |
m2 = re.match(r"\A'(.*)'\Z", val) | |
if m2: | |
val = m2.group(1) | |
os.environ.setdefault(key, val) | |
# Constants from environment variables | |
read_env() | |
CONSUMER_KEY = os.environ.get('CONSUMER_KEY') | |
REDIRECT_URL = os.environ.get('REDIRECT_URL') | |
def save_config(data): | |
config = configparser.ConfigParser() | |
config.add_section(CONFIG_HEADER) | |
for (k, v) in data.items(): | |
config.set(CONFIG_HEADER, k, v) | |
with open(config_path, 'w+') as cf: | |
config.write(cf) | |
def load_config(): | |
config = configparser.ConfigParser() | |
config.read(config_path) | |
consumer_key = config.get(CONFIG_HEADER, 'consumer_key') | |
access_token = config.get(CONFIG_HEADER, 'access_token') | |
username = config.get(CONFIG_HEADER, 'username') | |
return consumer_key, access_token, username | |
def get_request_token(): | |
req = requests.post(POCKET_REQUEST_TOKEN_URL, json={ | |
'consumer_key': CONSUMER_KEY, | |
'redirect_uri': REDIRECT_URL, | |
}) | |
data = urlparse.parse_qs(req.text) | |
return data['code'][0] | |
def get_access_token(request_token): | |
req = requests.post(POCKET_ACCESS_TOKEN_URL, json={ | |
'consumer_key': CONSUMER_KEY, | |
'code': request_token, | |
}) | |
data = urlparse.parse_qs(req.text) | |
try: | |
return data['access_token'][0], data['username'][0] | |
except KeyError: | |
raise Exception("There is no 'access_token' and 'username' in response. Are you sure you have authorized application?") | |
def get_auth_user_url(request_token): | |
return POCKET_URL + '/auth/authorize?request_token=%s&redirect_uri=%s' % (request_token, REDIRECT_URL) | |
if __name__ == '__main__': | |
request_token = get_request_token() | |
input('Authorize your account using link below and press any key afterwards:\n`%s`\n' % get_auth_user_url(request_token)) | |
access_token, username = get_access_token(request_token) | |
print access_token | |
save_config({ | |
'consumer_key': CONSUMER_KEY, | |
'access_token': access_token, | |
'username': username | |
}) | |
# Processing config file | |
consumer_key, access_token, username = load_config() | |
with open('links.txt') as f: | |
lines = f.readlines() | |
f.close() | |
# Use chunks and generator here, yielding results | |
pointer = 0 | |
while pointer < len(lines): | |
for line in lines[pointer:pointer+100]: | |
actions.append({ | |
'action': 'add', | |
'url': line | |
}) | |
req = requests.post(POCKET_API_URL + 'send', json={ | |
'consumer_key': consumer_key, | |
'access_token': access_token, | |
'actions': actions | |
}) | |
print json.dumps(json.loads(req.text), sort_keys=True, indent=4) | |
pointer += 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment