Created
November 12, 2024 20:08
-
-
Save squarepegsys/88fd78d3795bde39d5c5d37e8472a433 to your computer and use it in GitHub Desktop.
python script for TTRSS to Wallabag
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 python3 | |
""" | |
I couldn't get the "official" TT-RSS plugin to work so I make a Python script to utilize their APIs. | |
Overall, this is simple -- query TT-RSS for all starred articles and put the new ones into Wallybag. Very straight forward | |
Needs API keys for both your TT-RSS and Wallabag instances. And set the environment variables below appropriated | |
Does require `requests` library but you probably have that anyway | |
""" | |
import requests | |
import os | |
import json | |
import hashlib | |
import logging as logger | |
logger.basicConfig(level=logger.INFO) | |
TTS_USERNAME = os.getenv("tts_username") | |
TTS_PASSWORD = os.getenv("tts_password") | |
TTS_URL = "http://192.168.4.193/tt-rss/api/" | |
WALLY_USERNAME = os.getenv("wally_username") | |
WALLY_PASSWORD = os.getenv("wally_password") | |
WALLY_CLIENTID = os.getenv("wally_client_id") | |
WALLY_SECRET = os.getenv("wally_client_secret") | |
WALLY_HOST = "http://192.168.4.193/wallabag/web" | |
def get_wally_token(): | |
gettoken = { | |
"username": WALLY_USERNAME, | |
"password": WALLY_PASSWORD, | |
"client_id": WALLY_CLIENTID, | |
"client_secret": WALLY_SECRET, | |
"grant_type": "password", | |
} | |
r = requests.post("{}/oauth/v2/token".format(WALLY_HOST), gettoken) | |
r.raise_for_status() | |
access = r.json().get("access_token") | |
logger.info("Wally access token: %s", access) | |
return access | |
def get_tts_session(): | |
session_call = json.dumps( | |
{"op": "login", "user": TTS_USERNAME, "password": TTS_PASSWORD} | |
) | |
r = requests.post(TTS_URL, session_call) | |
r.raise_for_status() | |
session_id = r.json().get("content", {}).get("session_id", "") | |
return session_id | |
def get_ttrs_starred(session_id): | |
query = json.dumps({"sid": session_id, "op": "getHeadlines", "feed_id": "-1"}) | |
r = requests.post(TTS_URL, query) | |
r.raise_for_status() | |
starred_headlines = r.json().get("content", []) | |
for headline in starred_headlines: | |
yield headline["link"] | |
def save_to_wallabag(token, url): | |
hashed_url = hashlib.sha256(url.encode()).hexdigest() | |
auth = {"Authorization": f"Bearer {token}"} | |
# auth = {"access_token": token} | |
post_data = { | |
"url": url, | |
} | |
r = requests.get(f"{WALLY_HOST}/api/entries/exists?url={url}", headers=auth) | |
r.raise_for_status() | |
if r.json().get("exists"): | |
logger.info("Already saved: %s", url) | |
return | |
r = requests.post(f"{WALLY_HOST}/api/entries", headers=auth, json=post_data) | |
r.raise_for_status() | |
logger.info("Saved to Wallabag: %s", url) | |
if __name__ == "__main__": | |
ttrs_session = get_tts_session() | |
wally_token = get_wally_token() | |
for x in get_ttrs_starred(ttrs_session): | |
save_to_wallabag(wally_token, x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment