🎉 Initial commit

This commit is contained in:
Marcel Kapfer 2023-04-20 21:55:49 +02:00
commit f942dab834
Signed by: mmk2410
GPG Key ID: CADE6F0C09F21B09
10 changed files with 295 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/__pycache__/
/venv/
/config.json

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2023 Marcel Kapfer <opensource@mmk2410.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

30
README.org Normal file
View File

@ -0,0 +1,30 @@
#+title: YouTube2FreshRSS
One-Way-Synchronization of your YouTube subscriptions to FreshRSS.
* Requirements
- YouTube channel/account with a /public/ subscription list
- [[https://www.freshrss.org/][FreshRSS]] instance
- [[https://rss-bridge.github.io/rss-bridge/][rss-bridge]] instance
- Python 3
* Features
- Adds YouTube subscriptions as rss-bridge Atom feeds to the category "YouTube" in your FreshRSS instance
- Removes YouTube Atom feeds from the YouTube category in FreshRSS if they are (no longer) YouTube subscriptions
* Shortcomings
- I did not implement support for using OAuth2 with Google. Therefore it is only possible to access public resources and this is the reason why the channels subscription lists needs to be public.
- Perhaps many more that I don't know/mind at the moment.
* Setup
- Create a new project in the [[https://console.cloud.google.com/apis/dashboard][Google Cloud Console]] and follow the [[https://support.google.com/googleapi/answer/6158862?hl=en&ref_topic=7013279][instructions to get an API key]]
- Enable the YouTube bridge in rss-bridge (if necessary)
- Copy =config.example.json= to =config.json= and fill it out
- (optional) Create a Python virtual environment with =python3 -m venv venv= and activate it
- Install Python requirements with =pip install -r requirements.txt=
- Execute the program with =python youtube2freshrss.py=
- (optional) Set up a Cron job for running the =run.sh= once a day with the working directory as first argument

8
config.example.json Normal file
View File

@ -0,0 +1,8 @@
{
"youtube_api_key": "Google API key with YouTube access. See https://support.google.com/googleapi/answer/6158862?hl=en&ref_topic=7013279",
"youtube_channel_id": "Your YouTube channel ID with a public(!) subscription list",
"freshrss_username": "Your FreshRSS user name",
"freshrss_api_password": "Your FreshRSS API password. See https://freshrss.github.io/FreshRSS/en/users/06_Mobile_access.html",
"freshrss_url": "Your FreshRSS URL with trailing slash, e.g. https://rss.example.org/",
"rss_bridge_url": "Your rss-bridge URL with trailing slash, e.g. https://bridge.rss.example.org/"
}

82
freshrss.py Normal file
View File

@ -0,0 +1,82 @@
"""Google Reader/FreshRSS API code for Youtube2FreshRSS."""
import requests
class FreshRss:
"""FreshRSS greader API helper class."""
base_path = "/api/greader.php"
base_api_path = f"{base_path}/reader/api/0/"
def __init__(self, baseurl, username, password):
self.base_url = baseurl
self.username = username
self.password = password
self.auth_header = ""
def get_auth_token(self):
"Retrieve greader auth token"
res = requests.post(
f"{self.base_url}{self.base_path}/accounts/ClientLogin",
params={'Email': self.username, 'Passwd': self.password},
timeout=15
)
auth_token = res.text.split("=")[-1].strip()
self.auth_header = f"GoogleLogin auth={auth_token}"
def post(self, path, params=None, data=None):
"Make a POST request to a greader API."
res = requests.post(
f"{self.base_url}{self.base_api_path}{path}",
data=data,
params=params,
headers={"Authorization": self.auth_header},
timeout=15
)
return res.text
def get(self, path, params=None):
"""Make a GET request to a greader API."""
res = requests.get(
f"{self.base_url}{self.base_api_path}{path}?output=json",
params=params,
headers={"Authorization": self.auth_header},
timeout=15
)
return res.json()
def get_feeds(self):
"Retrieve greader subscriptions."
return self.get("/subscription/list")
def add_feed(self, title, feed_url):
"""Create greader feed."""
self.post(
"/subscription/edit",
data={
"ac": "subscribe",
"s": "feed/" + feed_url,
"t": title,
"a": "user/-/label/YouTube"
}
)
def remove_feed(self, feed_url):
"""Remove greader feed."""
self.post(
"/subscription/edit",
data={
"ac": "unsubscribe",
"s": "feed/" + feed_url,
}
)
def parse_feeds_urls(self, feeds):
"""Parse active feed URLs from feeds list."""
urls = []
for feed in feeds['subscriptions']:
if feed['categories'][0]['id'] == "user/-/label/YouTube":
urls.append(feed['url'])
return urls

33
helpers.py Normal file
View File

@ -0,0 +1,33 @@
"""Helper functions for YouTube2FreshRSS."""
import json
def determine_additions(subscriptions, feed_urls):
"""Determine which feeds need to be added to FreshRSS."""
additions = []
for subscription in subscriptions:
if subscription['url'] not in feed_urls:
additions.append(subscription)
return additions
def determine_deletions(feeds, subscriptions):
"""Determine which feeds need to be deleted from FreshRSS."""
urls = []
for feed in feeds:
if feed not in subscriptions:
urls.append(feed)
return urls
def read_config_file():
"""Read secrets JSON file."""
with open("config.json", "rb") as config_file:
data = json.load(config_file)
return data

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
requests
google-api-python-client

17
run.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
set -euo pipefail
WORKING_DIR="${1:-.}"
pushd "$WORKING_DIR"
if [ ! -d "./venv/" ]; then
echo "This is script only works with a python virtual environment setup at $(pwd)/venv"
exit 1
fi
source ./venv/bin/activate
./venv/bin/python youtube2freshrss.py
popd

62
youtube.py Normal file
View File

@ -0,0 +1,62 @@
"""YouTube API code for Youtube2FreshRSS."""
import googleapiclient.discovery
class YouTube:
"""YouTube API helper class."""
api_service_name = "youtube"
api_version = "v3"
def __init__(self, api_key, channel_id, rss_bridge_base_url):
self.api_key = api_key
self.channel_id = channel_id
self.rss_bridge_base_url = rss_bridge_base_url
self.api_client = googleapiclient.discovery.build(
self.api_service_name,
self.api_version,
developerKey=self.api_key
)
def get_subscription_list_page(self, page_token):
"""Retrieve subscription list page from YouTube."""
request = self.api_client.subscriptions().list(
part="snippet",
order="alphabetical",
maxResults=50,
pageToken=page_token,
channelId=self.channel_id
)
return request.execute()
def get_subscriptions(self):
"""Retrieve subscriptions list page from YouTube."""
next_page_token = ""
subscriptions = []
while True:
response = self.get_subscription_list_page(next_page_token)
subscriptions.extend(response['items'])
if 'nextPageToken' not in response:
break
next_page_token = response['nextPageToken']
return subscriptions
def parse_subscriptions(self, subscriptions):
"""Build feed URLs from YouTube subscriptions."""
base_feed_url = f"{self.rss_bridge_base_url}?action=display&bridge=Youtube&context=By+channel+id&duration_min=&duration_max=&format=Atom&c="
urls = []
parsed = []
for subscription in subscriptions:
channel_id = subscription['snippet']['resourceId']['channelId']
urls.append(base_feed_url + channel_id)
parsed.append({
"url": base_feed_url + channel_id,
"title": subscription['snippet']['title']
})
return urls, parsed

51
youtube2freshrss.py Normal file
View File

@ -0,0 +1,51 @@
"""Transfer YouTube subscriptions to FreshRSS."""
from youtube import YouTube
from freshrss import FreshRss
from helpers import read_config_file, determine_additions, determine_deletions
def main():
"""Entry point."""
config = read_config_file()
youtube = YouTube(
config['youtube_api_key'],
config['youtube_channel_id'],
config['rss_bridge_url']
)
subscriptions = youtube.get_subscriptions()
sub_urls, parsed_subs = youtube.parse_subscriptions(subscriptions)
print(f"Found {len(subscriptions)} subscriptions on YouTube.")
freshrss = FreshRss(
config['freshrss_url'],
config['freshrss_username'],
config['freshrss_api_password'],
)
freshrss.get_auth_token()
feeds = freshrss.get_feeds()
active_feeds = freshrss.parse_feeds_urls(feeds)
print(f"Found {len(active_feeds)} YouTube feeds on FreshRSS.")
additions = determine_additions(parsed_subs, active_feeds)
print(f"{len(additions)} feeds will be added to FreshRSS.")
deletions = determine_deletions(active_feeds, sub_urls)
print(f"{len(deletions)} feeds will be deleted from FreshRSS.")
for addition in additions:
freshrss.add_feed(addition['title'], addition['url'])
if len(additions) > 0:
print("Added missing feeds to FreshRSS.")
for deletion in deletions:
freshrss.remove_feed(deletion)
if len(deletions) > 0:
print("Removed missing YouTube subscriptions from FreshRSS.")
if __name__ == "__main__":
main()