-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocket-command.py
executable file
·118 lines (104 loc) · 4.12 KB
/
pocket-command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python3
# Send a URL to pocket (https://getpocket.com/) from the comand line
# Copyright (C) 2017 Alex King
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
import requests
import http.server
import json
import subprocess
import sys
import appdirs
import yaml
import os
BROWSER='firefox'
class PocketRequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write('<html><body>Thank you from PocketCommand</body></html>'.encode('UTF-8'))
self.server.callback(self.requestline)
class PocketHTTPServer(http.server.HTTPServer):
def __init__(self, port, callback):
http.server.HTTPServer.__init__(self, ('localhost',port), PocketRequestHandler)
self.callback = callback
class PocketCommand:
PLATFORM_CONSUMER_KEY = "69837-cd901fd9f9a7a4538f6686cd"
port=8910
headers = {'Content-Type': 'application/json; charset=UTF-8',
'X-Accept': 'application/json'}
def __init__(self):
try:
os.mkdir(appdirs.user_data_dir('pocketcommand'))
except OSError:
pass
self.config_file = os.path.join(appdirs.user_data_dir('pocketcommand'),'sessiondata')
def _post(self, path, data, phase):
uri = 'https://getpocket.com/v3/%s' % path
r = requests.post(uri, data = json.dumps(data),
headers = self.headers )
if r.status_code != requests.codes.ok:
print('%s to pocket failed with %d, %s' % (phase, r.status_code, r.headers['X-Error']))
r.raise_for_status()
return r
def authenticate(self, online=False):
if online:
self.authenticate_online()
return
try:
cdata = yaml.safe_load(open(self.config_file))
except OSError:
self.authenticate_online()
return
try:
self.access_token = cdata["access_token"]
self.username = cdata["username"]
self.auth_from_file = True
except (KeyError, TypeError):
os.ulink(self.config_file)
self.authenticate_online()
def authenticate_online(self):
self.server = PocketHTTPServer(self.port, self.recv_callback)
redirect_uri = 'http://localhost:%d/reportback' % self.port
data = {'consumer_key': self.PLATFORM_CONSUMER_KEY,
'redirect_uri': redirect_uri}
r = self._post('oauth/request', data, 'connecting')
self.request_token = r.json()['code']
uri="https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s" % (self.request_token, redirect_uri)
subprocess.Popen([BROWSER, uri])
self.server.handle_request()
print("Logging in and making a POST request...")
data = {"consumer_key": self.PLATFORM_CONSUMER_KEY,
"code": self.request_token}
r = self._post('oauth/authorize', data, 'authorizing')
self.access_token = r.json()['access_token']
self.username = r.json()['username']
print("Logged in as %s" % self.username)
try:
yaml.safe_dump({'access_token': self.access_token, 'username': self.username}, open((self.config_file),"w"))
except:
pass
self.auth_from_file = False
def recv_callback(self, url):
pass
def add_url(self, url):
data = {'url': url,
"consumer_key": self.PLATFORM_CONSUMER_KEY,
"access_token": self.access_token}
r = self._post('add', data, 'adding')
if r.status_code == requests.codes.ok:
print("Added url successfully")
if __name__=="__main__":
url=sys.argv[1]
p=PocketCommand()
print("Adding %s to Pocket...." % url)
p.authenticate()
try:
p.add_url(url)
except requests.exceptions.HTTPError:
if p.auth_from_file:
p.authenticate(online=True)
p.add_url(url)
raise