-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend_url_to_wallabag.py
executable file
·62 lines (51 loc) · 1.38 KB
/
send_url_to_wallabag.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
# Given a URL on the command line, authenticate to a Wallabag instance and
# add the link to the database. Returns 0 on success, non-zero on failure.
# By: The Doctor <drwho at virtadpt dot net>
# License: GPLv3
# v1.0 - Initial release.
# TO-DO:
# - Turn this into a real utility. This is just a hack.
# Load modules.
import requests
import sys
# Constants.
client_id = "x_..."
client_secret = "1..."
username = "user"
password = "lovesexsecretgod"
url = "https://wallabg.example.com"
# Global variables.
parameters = {}
headers = {}
request = None
token = None
data = {}
# Core code...
# If no URL was given, ABEND.
if len(sys.argv) < 2:
sys.exit(1)
# Request a bearer token from Wallabag.
parameters["grant_type"] = "password"
parameters["client_id"] = client_id
parameters["client_secret"] = client_secret
parameters["username"] = username
parameters["password"] = password
try:
request = requests.post(url + "/oauth/v2/token", data=parameters)
except:
print("Unable to get bearer token.")
sys.exit(1)
token = request.json()["access_token"]
# Shoot the URL over to Wallabag.
headers["Authorization"] = "Bearer " + token
data ["url"] = sys.argv[1]
try:
request = requests.post(url + "/api/entries.json", data=data,
headers=headers)
except:
sys.exit(1)
# Fin.
sys.exit(0)