17 Commits
v0.4 ... v0.6

Author SHA1 Message Date
meeb
12eac049e5 bump to 0.6 2020-12-19 18:14:31 +11:00
meeb
304cc153cf add DJANGO_FORCE_SCRIPT_NAME env var to change Djangos FORCE_SCRIPT_NAME if needed, part of support for running TubeSync in a /suburi and not a domain root, resolves #18 2020-12-19 18:10:10 +11:00
meeb
b45231f533 add secondary time based cap to allow sources to not download everything in a channel, resolves #15 2020-12-19 18:05:01 +11:00
meeb
26eb9d30e8 tweak field help text 2020-12-19 18:04:26 +11:00
meeb
97fa62d12b add playlist_index and playlist_title as media format options, fix paths for files in media format subdirs post download, resolves #13 2020-12-19 17:33:08 +11:00
meeb
1b092fe955 use xml parsing for tests to fix annoying attr ordering 2020-12-19 16:31:44 +11:00
meeb
18a59fe835 use OrderedDict for XML attrs so testing is consistent 2020-12-19 16:09:19 +11:00
meeb
410906ad8e add XML NFO file writing support, rework media cleanup deletion, resolves #11 2020-12-19 16:00:37 +11:00
meeb
8f4b09f346 add {mm} and {dd} media format support, resolves #12 2020-12-18 21:02:06 +11:00
meeb
cda021cbbf update screenshots 2020-12-18 19:01:35 +11:00
meeb
ee4df99cd8 update screenshots 2020-12-18 18:57:52 +11:00
meeb
53f1873a9b Merge branch 'main' of github.com:meeb/tubesync into main 2020-12-18 18:41:36 +11:00
meeb
9434293a84 fix dupe info on dashboard 2020-12-18 18:41:24 +11:00
meeb
ed69fe9dcc README tweaks 2020-12-18 18:35:58 +11:00
meeb
67af70569b bump to 0.5 2020-12-18 18:07:33 +11:00
meeb
68a62d8a7c add full support for YouTube channels with no vanity name, resolves #9 2020-12-18 17:43:58 +11:00
meeb
55578f4de7 add pretty-json-info-spam wrapper command to aid debugging urls 2020-12-18 17:31:47 +11:00
34 changed files with 1808 additions and 102 deletions

View File

@@ -18,33 +18,34 @@ hands-free as possible, TubeSync has gradual retrying of failures with back-off
so media which fails to download will be retried for an extended period making it, so media which fails to download will be retried for an extended period making it,
hopefully, quite reliable. hopefully, quite reliable.
# Latest container image # Latest container image
```yaml ```yaml
ghcr.io/meeb/tubesync:v0.4 ghcr.io/meeb/tubesync:v0.6
``` ```
# Screenshots # Screenshots
### Dashboard ### Dashboard
![TubeSync Dashboard](https://github.com/meeb/tubesync/blob/main/docs/dashboard.png?raw=true) ![TubeSync Dashboard](https://github.com/meeb/tubesync/blob/main/docs/dashboard-v0.5.png?raw=true)
### Sources overview ### Sources overview
![TubeSync sources overview](https://github.com/meeb/tubesync/blob/main/docs/sources.png?raw=true) ![TubeSync sources overview](https://github.com/meeb/tubesync/blob/main/docs/sources-v0.5.png?raw=true)
### Source details ### Source details
![TubeSync source details](https://github.com/meeb/tubesync/blob/main/docs/source.png?raw=true) ![TubeSync source details](https://github.com/meeb/tubesync/blob/main/docs/source-v0.5.png?raw=true)
### Media overview ### Media overview
![TubeSync media overview](https://github.com/meeb/tubesync/blob/main/docs/media.png?raw=true) ![TubeSync media overview](https://github.com/meeb/tubesync/blob/main/docs/media-v0.5.png?raw=true)
### Media details ### Media details
![TubeSync media-details](https://github.com/meeb/tubesync/blob/main/docs/media-item.png?raw=true) ![TubeSync media-details](https://github.com/meeb/tubesync/blob/main/docs/media-item-v0.5.png?raw=true)
# Requirements # Requirements
@@ -98,7 +99,7 @@ Finally, download and run the container:
```bash ```bash
# Pull a versioned image # Pull a versioned image
$ docker pull ghcr.io/meeb/tubesync:v0.4 $ docker pull ghcr.io/meeb/tubesync:v0.6
# Start the container using your user ID and group ID # Start the container using your user ID and group ID
$ docker run \ $ docker run \
-d \ -d \
@@ -109,7 +110,7 @@ $ docker run \
-v /some/directory/tubesync-config:/config \ -v /some/directory/tubesync-config:/config \
-v /some/directory/tubesync-downloads:/downloads \ -v /some/directory/tubesync-downloads:/downloads \
-p 4848:4848 \ -p 4848:4848 \
ghcr.io/meeb/tubesync:v0.4 ghcr.io/meeb/tubesync:v0.6
``` ```
Once running, open `http://localhost:4848` in your browser and you should see the Once running, open `http://localhost:4848` in your browser and you should see the
@@ -121,7 +122,7 @@ Alternatively, for Docker Compose, you can use something like:
```yaml ```yaml
tubesync: tubesync:
image: ghcr.io/meeb/tubesync:v0.4 image: ghcr.io/meeb/tubesync:v0.6
container_name: tubesync container_name: tubesync
restart: unless-stopped restart: unless-stopped
ports: ports:
@@ -196,10 +197,10 @@ $ docker logs --follow tubesync
### 1. Index frequency ### 1. Index frequency
It's a good idea to add sources with as low an index frequency as possible. This is the It's a good idea to add sources with as long of an index frequency as possible. This is
duration between indexes of the source. An index is when TubeSync checks to see the duration between indexes of the source. An index is when TubeSync checks to see
what videos available on a channel or playlist to find new media. Try and keep this as what videos available on a channel or playlist to find new media. Try and keep this as
long as possible, 24 hours if possible. long as possible, up to 24 hours.
### 2. Indexing massive channels ### 2. Indexing massive channels
@@ -222,7 +223,7 @@ automatically.
### Does TubeSync support any other video platforms? ### Does TubeSync support any other video platforms?
At the moment, no. This is a first release. The library TubeSync uses that does most At the moment, no. This is a pre-release. The library TubeSync uses that does most
of the downloading work, `youtube-dl`, supports many hundreds of video sources so it's of the downloading work, `youtube-dl`, supports many hundreds of video sources so it's
likely more will be added to TubeSync if there is demand for it. likely more will be added to TubeSync if there is demand for it.
@@ -236,7 +237,7 @@ your install is doing check the container logs.
### Are there alerts when a download is complete? ### Are there alerts when a download is complete?
No, this feature is best served by existing services such as the execelent No, this feature is best served by existing services such as the execelent
[tautulli](https://tautulli.com/) which can monitor your Plex server and send alerts [Tautulli](https://tautulli.com/) which can monitor your Plex server and send alerts
that way. that way.
### There's errors in my "tasks" tab! ### There's errors in my "tasks" tab!
@@ -293,23 +294,24 @@ Just `amd64` for the moment. Others may be made available if there is demand.
# Advanced configuration # Advanced configuration
There are a number of other environment variables you can set. These are, mostly, There are a number of other environment variables you can set. These are, mostly,
**NOT** required to be set in the default container installation, they are mostly **NOT** required to be set in the default container installation, they are really only
useful if you are manually installing TubeSync in some other environment. These are: useful if you are manually installing TubeSync in some other environment. These are:
| Name | What | Example | | Name | What | Example |
| ----------------- | ------------------------------------- | ---------------------------------- | | ------------------------ | ------------------------------------- | ---------------------------------- |
| DJANGO_SECRET_KEY | Django secret key | YJySXnQLB7UVZw2dXKDWxI5lEZaImK6l | | DJANGO_SECRET_KEY | Django's SECRET_KEY | YJySXnQLB7UVZw2dXKDWxI5lEZaImK6l |
| TUBESYNC_DEBUG | Enable debugging | True | | DJANGO_FORCE_SCRIPT_NAME | Django's FORCE_SCRIPT_NAME | /somepath |
| TUBESYNC_HOSTS | Django's ALLOWED_HOSTS | tubesync.example.com,otherhost.com | | TUBESYNC_DEBUG | Enable debugging | True |
| GUNICORN_WORKERS | Number of gunicorn workers to spawn | 3 | | TUBESYNC_HOSTS | Django's ALLOWED_HOSTS | tubesync.example.com,otherhost.com |
| LISTEN_HOST | IP address for gunicorn to listen on | 127.0.0.1 | | GUNICORN_WORKERS | Number of gunicorn workers to spawn | 3 |
| LISTEN_PORT | Port number for gunicorn to listen on | 8080 | | LISTEN_HOST | IP address for gunicorn to listen on | 127.0.0.1 |
| LISTEN_PORT | Port number for gunicorn to listen on | 8080 |
# Manual, non-containerised, installation # Manual, non-containerised, installation
As a relatively normal Django app you can run TubeSync without the container. Beyond As a relatively normal Django app you can run TubeSync without the container. Beyond
the following rough guide you are on your own and should be knowledgeable about following this rough guide you are on your own and should be knowledgeable about
installing and running WSGI-based Python web applications before attempting this. installing and running WSGI-based Python web applications before attempting this.
1. Clone or download this repo 1. Clone or download this repo

BIN
docs/dashboard-v0.5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

BIN
docs/media-item-v0.5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 420 KiB

BIN
docs/media-v0.5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 666 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 530 KiB

BIN
docs/source-v0.5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 137 KiB

BIN
docs/sources-v0.5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

1183
tubesync/spam Normal file

File diff suppressed because it is too large Load Diff

View File

View File

@@ -0,0 +1,18 @@
import json
from django.core.management.base import BaseCommand, CommandError
from sync.youtube import get_media_info
class Command(BaseCommand):
help = 'Displays information obtained by youtube-dl in JSON to the console'
def add_arguments(self, parser):
parser.add_argument('url', type=str)
def handle(self, *args, **options):
url = options['url']
self.stdout.write(f'Showing information for URL: {url}')
info = get_media_info(url)
self.stdout.write(json.dumps(info, indent=4, sort_keys=True))
self.stdout.write('Done')

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-19 03:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sync', '0004_source_media_format'),
]
operations = [
migrations.AlterField(
model_name='source',
name='source_type',
field=models.CharField(choices=[('c', 'YouTube channel'), ('i', 'YouTube channel by ID'), ('p', 'YouTube playlist')], db_index=True, default='c', help_text='Source type', max_length=1, verbose_name='source type'),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-19 03:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sync', '0005_auto_20201219_0312'),
]
operations = [
migrations.AddField(
model_name='source',
name='write_nfo',
field=models.BooleanField(default=False, help_text='Write an NFO file with the media, these may be detected and used by some media servers', verbose_name='write nfo'),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-19 06:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sync', '0006_source_write_nfo'),
]
operations = [
migrations.AlterField(
model_name='source',
name='write_nfo',
field=models.BooleanField(default=False, help_text='Write an NFO file in XML with the media info, these may be detected and used by some media servers', verbose_name='write nfo'),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-19 06:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sync', '0007_auto_20201219_0645'),
]
operations = [
migrations.AddField(
model_name='source',
name='download_cap',
field=models.IntegerField(choices=[(0, 'No cap'), (604800, '1 week (7 days)'), (2592000, '1 month (30 days)'), (7776000, '3 months (90 days)'), (15552000, '6 months (180 days)'), (31536000, '1 year (365 days)'), (63072000, '2 years (730 days)'), (94608000, '3 years (1095 days)'), (157680000, '5 years (1825 days)'), (315360000, '10 years (3650 days)')], default=0, help_text='Do not download media older than this capped date', verbose_name='download cap'),
),
]

View File

@@ -1,7 +1,9 @@
import os import os
import uuid import uuid
import json import json
from datetime import datetime from xml.etree import ElementTree
from collections import OrderedDict
from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
@@ -28,10 +30,13 @@ class Source(models.Model):
''' '''
SOURCE_TYPE_YOUTUBE_CHANNEL = 'c' SOURCE_TYPE_YOUTUBE_CHANNEL = 'c'
SOURCE_TYPE_YOUTUBE_CHANNEL_ID = 'i'
SOURCE_TYPE_YOUTUBE_PLAYLIST = 'p' SOURCE_TYPE_YOUTUBE_PLAYLIST = 'p'
SOURCE_TYPES = (SOURCE_TYPE_YOUTUBE_CHANNEL, SOURCE_TYPE_YOUTUBE_PLAYLIST) SOURCE_TYPES = (SOURCE_TYPE_YOUTUBE_CHANNEL, SOURCE_TYPE_YOUTUBE_CHANNEL_ID,
SOURCE_TYPE_YOUTUBE_PLAYLIST)
SOURCE_TYPE_CHOICES = ( SOURCE_TYPE_CHOICES = (
(SOURCE_TYPE_YOUTUBE_CHANNEL, _('YouTube channel')), (SOURCE_TYPE_YOUTUBE_CHANNEL, _('YouTube channel')),
(SOURCE_TYPE_YOUTUBE_CHANNEL_ID, _('YouTube channel by ID')),
(SOURCE_TYPE_YOUTUBE_PLAYLIST, _('YouTube playlist')), (SOURCE_TYPE_YOUTUBE_PLAYLIST, _('YouTube playlist')),
) )
@@ -98,24 +103,40 @@ class Source(models.Model):
# Fontawesome icons used for the source on the front end # Fontawesome icons used for the source on the front end
ICONS = { ICONS = {
SOURCE_TYPE_YOUTUBE_CHANNEL: '<i class="fab fa-youtube"></i>', SOURCE_TYPE_YOUTUBE_CHANNEL: '<i class="fab fa-youtube"></i>',
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: '<i class="fab fa-youtube"></i>',
SOURCE_TYPE_YOUTUBE_PLAYLIST: '<i class="fab fa-youtube"></i>', SOURCE_TYPE_YOUTUBE_PLAYLIST: '<i class="fab fa-youtube"></i>',
} }
# Format to use to display a URL for the source # Format to use to display a URL for the source
URLS = { URLS = {
SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/c/{key}', SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/c/{key}',
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'https://www.youtube.com/channel/{key}',
SOURCE_TYPE_YOUTUBE_PLAYLIST: 'https://www.youtube.com/playlist?list={key}', SOURCE_TYPE_YOUTUBE_PLAYLIST: 'https://www.youtube.com/playlist?list={key}',
} }
# Callback functions to get a list of media from the source # Callback functions to get a list of media from the source
INDEXERS = { INDEXERS = {
SOURCE_TYPE_YOUTUBE_CHANNEL: get_youtube_media_info, SOURCE_TYPE_YOUTUBE_CHANNEL: get_youtube_media_info,
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: get_youtube_media_info,
SOURCE_TYPE_YOUTUBE_PLAYLIST: get_youtube_media_info, SOURCE_TYPE_YOUTUBE_PLAYLIST: get_youtube_media_info,
} }
# Field names to find the media ID used as the key when storing media # Field names to find the media ID used as the key when storing media
KEY_FIELD = { KEY_FIELD = {
SOURCE_TYPE_YOUTUBE_CHANNEL: 'id', SOURCE_TYPE_YOUTUBE_CHANNEL: 'id',
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'id',
SOURCE_TYPE_YOUTUBE_PLAYLIST: 'id', SOURCE_TYPE_YOUTUBE_PLAYLIST: 'id',
} }
class CapChoices(models.IntegerChoices):
CAP_NOCAP = 0, _('No cap')
CAP_7DAYS = 604800, _('1 week (7 days)')
CAP_30DAYS = 2592000, _('1 month (30 days)')
CAP_90DAYS = 7776000, _('3 months (90 days)')
CAP_6MONTHS = 15552000, _('6 months (180 days)')
CAP_1YEAR = 31536000, _('1 year (365 days)')
CAP_2YEARs = 63072000, _('2 years (730 days)')
CAP_3YEARs = 94608000, _('3 years (1095 days)')
CAP_5YEARs = 157680000, _('5 years (1825 days)')
CAP_10YEARS = 315360000, _('10 years (3650 days)')
class IndexSchedule(models.IntegerChoices): class IndexSchedule(models.IntegerChoices):
EVERY_HOUR = 3600, _('Every hour') EVERY_HOUR = 3600, _('Every hour')
EVERY_2_HOURS = 7200, _('Every 2 hours') EVERY_2_HOURS = 7200, _('Every 2 hours')
@@ -188,6 +209,12 @@ class Source(models.Model):
default=IndexSchedule.EVERY_6_HOURS, default=IndexSchedule.EVERY_6_HOURS,
help_text=_('Schedule of how often to index the source for new media') help_text=_('Schedule of how often to index the source for new media')
) )
download_cap = models.IntegerField(
_('download cap'),
choices=CapChoices.choices,
default=CapChoices.CAP_NOCAP,
help_text=_('Do not download media older than this capped date')
)
delete_old_media = models.BooleanField( delete_old_media = models.BooleanField(
_('delete old media'), _('delete old media'),
default=False, default=False,
@@ -246,6 +273,11 @@ class Source(models.Model):
default=False, default=False,
help_text=_('Copy thumbnails with the media, these may be detected and used by some media servers') help_text=_('Copy thumbnails with the media, these may be detected and used by some media servers')
) )
write_nfo = models.BooleanField(
_('write nfo'),
default=False,
help_text=_('Write an NFO file in XML with the media info, these may be detected and used by some media servers')
)
has_failed = models.BooleanField( has_failed = models.BooleanField(
_('has failed'), _('has failed'),
default=False, default=False,
@@ -276,6 +308,14 @@ class Source(models.Model):
def is_video(self): def is_video(self):
return not self.is_audio return not self.is_audio
@property
def download_cap_date(self):
delta = self.download_cap
if delta > 0:
return timezone.now() - timedelta(seconds=delta)
else:
return False
@property @property
def extension(self): def extension(self):
''' '''
@@ -363,12 +403,16 @@ class Source(models.Model):
'yyyymmdd': timezone.now().strftime('%Y%m%d'), 'yyyymmdd': timezone.now().strftime('%Y%m%d'),
'yyyy_mm_dd': timezone.now().strftime('%Y-%m-%d'), 'yyyy_mm_dd': timezone.now().strftime('%Y-%m-%d'),
'yyyy': timezone.now().strftime('%Y'), 'yyyy': timezone.now().strftime('%Y'),
'mm': timezone.now().strftime('%m'),
'dd': timezone.now().strftime('%d'),
'source': self.slugname, 'source': self.slugname,
'source_full': self.name, 'source_full': self.name,
'title': 'some-media-title-name', 'title': 'some-media-title-name',
'title_full': 'Some Media Title Name', 'title_full': 'Some Media Title Name',
'key': 'SoMeUnIqUiD', 'key': 'SoMeUnIqUiD',
'format': '-'.join(fmt), 'format': '-'.join(fmt),
'playlist_index': 1,
'playlist_title': 'Some Playlist Title',
'ext': self.extension, 'ext': self.extension,
'resolution': self.source_resolution if self.source_resolution else '', 'resolution': self.source_resolution if self.source_resolution else '',
'height': '720' if self.source_resolution else '', 'height': '720' if self.source_resolution else '',
@@ -382,7 +426,7 @@ class Source(models.Model):
def get_example_media_format(self): def get_example_media_format(self):
try: try:
return self.media_format.format(**self.example_media_format_dict) return self.media_format.format(**self.example_media_format_dict)
except Exception: except Exception as e:
return '' return ''
def index_media(self): def index_media(self):
@@ -433,34 +477,81 @@ class Media(models.Model):
# Format to use to display a URL for the media # Format to use to display a URL for the media
URLS = { URLS = {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/watch?v={key}', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/watch?v={key}',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'https://www.youtube.com/watch?v={key}',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'https://www.youtube.com/watch?v={key}', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'https://www.youtube.com/watch?v={key}',
} }
# Maps standardised names to names used in source metdata # Maps standardised names to names used in source metdata
METADATA_FIELDS = { METADATA_FIELDS = {
'upload_date': { 'upload_date': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'upload_date', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'upload_date',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'upload_date',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'upload_date', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'upload_date',
}, },
'title': { 'title': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'title', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'title',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'title',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'title', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'title',
}, },
'thumbnail': { 'thumbnail': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'thumbnail', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'thumbnail',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'thumbnail',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'thumbnail', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'thumbnail',
}, },
'description': { 'description': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'description', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'description',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'description',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'description', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'description',
}, },
'duration': { 'duration': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'duration', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'duration',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'duration',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'duration', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'duration',
}, },
'formats': { 'formats': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'formats', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'formats',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'formats',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'formats', Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'formats',
} },
'categories': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'categories',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'categories',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'categories',
},
'rating': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'average_rating',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'average_rating',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'average_rating',
},
'age_limit': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'age_limit',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'age_limit',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'age_limit',
},
'uploader': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'uploader',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'uploader',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'uploader',
},
'upvotes': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'like_count',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'like_count',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'like_count',
},
'downvotes': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'dislike_count',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'dislike_count',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'dislike_count',
},
'playlist_index': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'playlist_index',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'playlist_index',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'playlist_index',
},
'playlist_title': {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'playlist_title',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'playlist_title',
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'playlist_title',
},
} }
STATE_UNKNOWN = 'unknown' STATE_UNKNOWN = 'unknown'
STATE_SCHEDULED = 'scheduled' STATE_SCHEDULED = 'scheduled'
@@ -791,12 +882,16 @@ class Media(models.Model):
'yyyymmdd': dateobj.strftime('%Y%m%d'), 'yyyymmdd': dateobj.strftime('%Y%m%d'),
'yyyy_mm_dd': dateobj.strftime('%Y-%m-%d'), 'yyyy_mm_dd': dateobj.strftime('%Y-%m-%d'),
'yyyy': dateobj.strftime('%Y'), 'yyyy': dateobj.strftime('%Y'),
'mm': dateobj.strftime('%m'),
'dd': dateobj.strftime('%d'),
'source': self.source.slugname, 'source': self.source.slugname,
'source_full': self.source.name, 'source_full': self.source.name,
'title': self.slugtitle, 'title': self.slugtitle,
'title_full': self.title, 'title_full': self.title,
'key': self.key, 'key': self.key,
'format': '-'.join(display_format['format']), 'format': '-'.join(display_format['format']),
'playlist_index': self.playlist_index,
'playlist_title': self.playlist_title,
'ext': self.source.extension, 'ext': self.source.extension,
'resolution': display_format['resolution'], 'resolution': display_format['resolution'],
'height': display_format['height'], 'height': display_format['height'],
@@ -865,26 +960,78 @@ class Media(models.Model):
return seconds_to_timestr(duration) return seconds_to_timestr(duration)
return '??:??:??' return '??:??:??'
@property
def categories(self):
field = self.get_metadata_field('categories')
return self.loaded_metadata.get(field, [])
@property
def rating(self):
field = self.get_metadata_field('rating')
return self.loaded_metadata.get(field, 0)
@property
def votes(self):
field = self.get_metadata_field('upvotes')
upvotes = self.loaded_metadata.get(field, 0)
field = self.get_metadata_field('downvotes')
downvotes = self.loaded_metadata.get(field, 0)
return upvotes + downvotes
@property
def age_limit(self):
field = self.get_metadata_field('age_limit')
return self.loaded_metadata.get(field, 0)
@property
def uploader(self):
field = self.get_metadata_field('uploader')
return self.loaded_metadata.get(field, '')
@property @property
def formats(self): def formats(self):
field = self.get_metadata_field('formats') field = self.get_metadata_field('formats')
return self.loaded_metadata.get(field, []) return self.loaded_metadata.get(field, [])
@property
def playlist_index(self):
field = self.get_metadata_field('playlist_index')
return self.loaded_metadata.get(field, 0)
@property
def playlist_title(self):
field = self.get_metadata_field('playlist_title')
return self.loaded_metadata.get(field, '')
@property @property
def filename(self): def filename(self):
# If a media_file has been downloaded use its existing name
if self.media_file:
return os.path.basename(self.media_file.name)
# Otherwise, create a suitable filename from the source media_format # Otherwise, create a suitable filename from the source media_format
media_format = str(self.source.media_format) media_format = str(self.source.media_format)
media_details = self.format_dict media_details = self.format_dict
return media_format.format(**media_details) return media_format.format(**media_details)
@property
def thumbname(self):
filename = self.filename
prefix, ext = os.path.splitext(filename)
return f'{prefix}.jpg'
@property
def thumbpath(self):
return self.source.directory_path / self.thumbname
@property
def nfoname(self):
filename = self.filename
prefix, ext = os.path.splitext(filename)
return f'{prefix}.nfo'
@property
def nfopath(self):
return self.source.directory_path / self.nfoname
@property @property
def directory_path(self): def directory_path(self):
# If a media_file has been downloaded use its existing directory
if self.media_file:
return os.path.dirname(self.media_file.name)
# Otherwise, create a suitable filename from the source media_format # Otherwise, create a suitable filename from the source media_format
media_format = str(self.source.media_format) media_format = str(self.source.media_format)
media_details = self.format_dict media_details = self.format_dict
@@ -907,6 +1054,103 @@ class Media(models.Model):
return False return False
return os.path.exists(self.media_file.path) return os.path.exists(self.media_file.path)
@property
def nfoxml(self):
'''
Returns an NFO formatted (prettified) XML string.
'''
nfo = ElementTree.Element('episodedetails')
nfo.text = '\n '
# title = media metadata title
title = nfo.makeelement('title', {})
title.text = str(self.name).strip()
title.tail = '\n '
nfo.append(title)
# showtitle = source name
showtitle = nfo.makeelement('showtitle', {})
showtitle.text = str(self.source.name).strip()
showtitle.tail = '\n '
nfo.append(showtitle)
# ratings = media metadata youtube rating
value = nfo.makeelement('value', {})
value.text = str(self.rating)
value.tail = '\n '
votes = nfo.makeelement('votes', {})
votes.text = str(self.votes)
votes.tail = '\n '
rating_attrs = OrderedDict()
rating_attrs['name'] = 'youtube'
rating_attrs['max'] = '5'
rating_attrs['default'] = 'True'
rating = nfo.makeelement('rating', rating_attrs)
rating.text = '\n '
rating.append(value)
rating.append(votes)
rating.tail = '\n '
ratings = nfo.makeelement('ratings', {})
ratings.text = '\n '
ratings.append(rating)
ratings.tail = '\n '
nfo.append(ratings)
# plot = media metadata description
plot = nfo.makeelement('plot', {})
plot.text = str(self.description).strip()
plot.tail = '\n '
nfo.append(plot)
# thumb = local path to media thumbnail
thumb = nfo.makeelement('thumb', {})
thumb.text = self.thumbname if self.source.copy_thumbnails else ''
thumb.tail = '\n '
nfo.append(thumb)
# mpaa = media metadata age requirement
mpaa = nfo.makeelement('mpaa', {})
mpaa.text = str(self.age_limit)
mpaa.tail = '\n '
nfo.append(mpaa)
# runtime = media metadata duration in seconds
runtime = nfo.makeelement('runtime', {})
runtime.text = str(self.duration)
runtime.tail = '\n '
nfo.append(runtime)
# id = media key
idn = nfo.makeelement('id', {})
idn.text = str(self.key).strip()
idn.tail = '\n '
nfo.append(idn)
# uniqueid = media key
uniqueid_attrs = OrderedDict()
uniqueid_attrs['type'] = 'youtube'
uniqueid_attrs['default'] = 'True'
uniqueid = nfo.makeelement('uniqueid', uniqueid_attrs)
uniqueid.text = str(self.key).strip()
uniqueid.tail = '\n '
nfo.append(uniqueid)
# studio = media metadata uploader
studio = nfo.makeelement('studio', {})
studio.text = str(self.uploader).strip()
studio.tail = '\n '
nfo.append(studio)
# aired = media metadata uploaded date
aired = nfo.makeelement('aired', {})
upload_date = self.upload_date
aired.text = upload_date.strftime('%Y-%m-%d') if upload_date else ''
aired.tail = '\n '
nfo.append(aired)
# dateadded = date and time media was created in tubesync
dateadded = nfo.makeelement('dateadded', {})
dateadded.text = self.created.strftime('%Y-%m-%d %H:%M:%S')
dateadded.tail = '\n '
nfo.append(dateadded)
# genre = any media metadata categories if they exist
for category_str in self.categories:
genre = nfo.makeelement('genre', {})
genre.text = str(category_str).strip()
genre.tail = '\n '
nfo.append(genre)
nfo[-1].tail = '\n'
# Return XML tree as a prettified string
return ElementTree.tostring(nfo, encoding='utf8', method='xml').decode('utf8')
def get_download_state(self, task=None): def get_download_state(self, task=None):
if self.downloaded: if self.downloaded:
return self.STATE_DOWNLOADED return self.STATE_DOWNLOADED

View File

@@ -145,20 +145,6 @@ def media_pre_delete(sender, instance, **kwargs):
if thumbnail_url: if thumbnail_url:
delete_task_by_media('sync.tasks.download_media_thumbnail', delete_task_by_media('sync.tasks.download_media_thumbnail',
(str(instance.pk), thumbnail_url)) (str(instance.pk), thumbnail_url))
# Delete media thumbnail if it exists
if instance.thumb:
log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}')
delete_file(instance.thumb.path)
# Delete the media file if it exists
if instance.media_file:
filepath = instance.media_file.path
log.info(f'Deleting media for: {instance} path: {filepath}')
delete_file(filepath)
# Delete thumbnail copy if it exists
barefilepath, fileext = os.path.splitext(filepath)
thumbpath = f'{barefilepath}.jpg'
log.info(f'Deleting thumbnail for: {instance} path: {thumbpath}')
delete_file(thumbpath)
@receiver(post_delete, sender=Media) @receiver(post_delete, sender=Media)

View File

@@ -23,7 +23,8 @@ from background_task.models import Task, CompletedTask
from common.logger import log from common.logger import log
from common.errors import NoMediaException, DownloadFailedException from common.errors import NoMediaException, DownloadFailedException
from .models import Source, Media, MediaServer from .models import Source, Media, MediaServer
from .utils import get_remote_image, resize_image_to_height, delete_file from .utils import (get_remote_image, resize_image_to_height, delete_file,
write_text_file)
def get_hash(task_name, pk): def get_hash(task_name, pk):
@@ -186,6 +187,14 @@ def index_source_task(source_id):
else: else:
log.error(f'Media has no upload date, skipping: {source} / {media}') log.error(f'Media has no upload date, skipping: {source} / {media}')
continue continue
# If the source has a download cap date check the upload date is allowed
max_cap_age = source.download_cap_date
if max_cap_age:
if media.published < max_cap_age:
# Media was published after the cap date, skip it
log.warn(f'Media: {source} / {media} is older than cap age '
f'{max_cap_age}, skipping')
continue
# If the source has a cut-off check the upload date is within the allowed delta # If the source has a cut-off check the upload date is within the allowed delta
if source.delete_old_media and source.days_to_keep > 0: if source.delete_old_media and source.days_to_keep > 0:
delta = timezone.now() - timedelta(days=source.days_to_keep) delta = timezone.now() - timedelta(days=source.days_to_keep)
@@ -317,11 +326,13 @@ def download_media(media_id):
media.save() media.save()
# If selected, copy the thumbnail over as well # If selected, copy the thumbnail over as well
if media.source.copy_thumbnails and media.thumb: if media.source.copy_thumbnails and media.thumb:
barefilepath, fileext = os.path.splitext(filepath)
thumbpath = f'{barefilepath}.jpg'
log.info(f'Copying media thumbnail from: {media.thumb.path} ' log.info(f'Copying media thumbnail from: {media.thumb.path} '
f'to: {thumbpath}') f'to: {media.thumbpath}')
copyfile(media.thumb.path, thumbpath) copyfile(media.thumb.path, media.thumbpath)
# If selected, write an NFO file
if media.source.write_nfo:
log.info(f'Writing media NFO file to: to: {media.nfopath}')
write_text_file(media.nfopath, media.nfoxml)
# Schedule a task to update media servers # Schedule a task to update media servers
for mediaserver in MediaServer.objects.all(): for mediaserver in MediaServer.objects.all():
log.info(f'Scheduling media server updates') log.info(f'Scheduling media server updates')

View File

@@ -11,18 +11,28 @@
<tr> <tr>
<td>{yyyymmdd}</td> <td>{yyyymmdd}</td>
<td>Media publish date in YYYYMMDD</td> <td>Media publish date in YYYYMMDD</td>
<td>20210101</td> <td>20210131</td>
</tr> </tr>
<tr> <tr>
<td>{yyyy_mm_dd}</td> <td>{yyyy_mm_dd}</td>
<td>Media publish date in YYYY-MM-DD</td> <td>Media publish date in YYYY-MM-DD</td>
<td>2021-01-01</td> <td>2021-01-31</td>
</tr> </tr>
<tr> <tr>
<td>{yyyy}</td> <td>{yyyy}</td>
<td>Media publish year in YYYY</td> <td>Media publish year in YYYY</td>
<td>2021</td> <td>2021</td>
</tr> </tr>
<tr>
<td>{mm}</td>
<td>Media publish year in MM</td>
<td>01</td>
</tr>
<tr>
<td>{dd}</td>
<td>Media publish year in DD</td>
<td>31</td>
</tr>
<tr> <tr>
<td>{source}</td> <td>{source}</td>
<td>Lower case source name, max 80 chars</td> <td>Lower case source name, max 80 chars</td>
@@ -53,6 +63,16 @@
<td>Media format string</td> <td>Media format string</td>
<td>720p-avc1-mp4a</td> <td>720p-avc1-mp4a</td>
</tr> </tr>
<tr>
<td>{playlist_index}</td>
<td>Playlist index of media, if it's in a playlist</td>
<td>12</td>
</tr>
<tr>
<td>{playlist_title}</td>
<td>Playlist title of media, if it's in a playlist</td>
<td>Some Playlist</td>
</tr>
<tr> <tr>
<td>{ext}</td> <td>{ext}</td>
<td>File extension</td> <td>File extension</td>

View File

@@ -71,7 +71,7 @@
<div class="collection"> <div class="collection">
{% for media in latest_downloads %} {% for media in latest_downloads %}
<a href="{% url 'sync:media-item' pk=media.pk %}" class="collection-item"> <a href="{% url 'sync:media-item' pk=media.pk %}" class="collection-item">
<div class="truncate"><strong>{{ media.name }}</strong> ({{ media.source }})</div> <div class="truncate"><strong>{{ media.name }}</strong></div>
<div class="truncate"><strong>{{ media.download_date|timesince:now }}</strong> ago from &quot;{{ media.source.name }}&quot;</div> <div class="truncate"><strong>{{ media.download_date|timesince:now }}</strong> ago from &quot;{{ media.source.name }}&quot;</div>
</a> </a>
{% empty %} {% empty %}
@@ -89,7 +89,7 @@
{% for media in largest_downloads %} {% for media in largest_downloads %}
<a href="{% url 'sync:media-item' pk=media.pk %}" class="collection-item"> <a href="{% url 'sync:media-item' pk=media.pk %}" class="collection-item">
<div class="truncate">{{ media.name }}</div> <div class="truncate">{{ media.name }}</div>
<div class="truncate"><strong>{{ media.downloaded_filesize|filesizeformat }}</strong>{% if media.downloaded_format %} in {{ media.downloaded_format }}{% endif %}</div> <div class="truncate"><strong>{{ media.downloaded_filesize|filesizeformat }}</strong>{% if media.downloaded_format %} in {{ media.downloaded_format }}{% endif %} from &quot;{{ media.source.name }}&quot;</div>
</a> </a>
{% empty %} {% empty %}
<span class="collection-item">No media has been downloaded.</span> <span class="collection-item">No media has been downloaded.</span>

View File

@@ -25,7 +25,7 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
{% include 'mediaformatvars.html' %} {% include 'sync/_mediaformatvars.html' %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@@ -27,7 +27,7 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
{% include 'mediaformatvars.html' %} {% include 'sync/_mediaformatvars.html' %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@@ -51,6 +51,12 @@
<td class="hide-on-small-only">Example filename</td> <td class="hide-on-small-only">Example filename</td>
<td><span class="hide-on-med-and-up">Example filename<br></span><strong>{{ source.get_example_media_format }}</strong></td> <td><span class="hide-on-med-and-up">Example filename<br></span><strong>{{ source.get_example_media_format }}</strong></td>
</tr> </tr>
{% if source.download_cap > 0 %}
<tr title="Do not download videos older than this cap">
<td class="hide-on-small-only">Download cap</td>
<td><span class="hide-on-med-and-up">Download cap<br></span><strong>{{ source.get_download_cap_display }}</strong></td>
</tr>
{% endif %}
<tr title="Schedule of how often to index the source for new media"> <tr title="Schedule of how often to index the source for new media">
<td class="hide-on-small-only">Index schedule</td> <td class="hide-on-small-only">Index schedule</td>
<td><span class="hide-on-med-and-up">Index schedule<br></span><strong>{{ source.get_index_schedule_display }}</strong></td> <td><span class="hide-on-med-and-up">Index schedule<br></span><strong>{{ source.get_index_schedule_display }}</strong></td>
@@ -97,6 +103,10 @@
<td class="hide-on-small-only">Copy thumbnails?</td> <td class="hide-on-small-only">Copy thumbnails?</td>
<td><span class="hide-on-med-and-up">Copy thumbnails?<br></span><strong>{% if source.copy_thumbnails %}<i class="fas fa-check"></i>{% else %}<i class="fas fa-times"></i>{% endif %}</strong></td> <td><span class="hide-on-med-and-up">Copy thumbnails?<br></span><strong>{% if source.copy_thumbnails %}<i class="fas fa-check"></i>{% else %}<i class="fas fa-times"></i>{% endif %}</strong></td>
</tr> </tr>
<tr title="Should an NFO file be written with the media?">
<td class="hide-on-small-only">Write NFO?</td>
<td><span class="hide-on-med-and-up">Write NFO?<br></span><strong>{% if source.write_nfo %}<i class="fas fa-check"></i>{% else %}<i class="fas fa-times"></i>{% endif %}</strong></td>
</tr>
{% if source.delete_old_media and source.days_to_keep > 0 %} {% if source.delete_old_media and source.days_to_keep > 0 %}
<tr title="Days after which your media from this source will be locally deleted"> <tr title="Days after which your media from this source will be locally deleted">
<td class="hide-on-small-only">Delete old media</td> <td class="hide-on-small-only">Delete old media</td>

View File

@@ -10,10 +10,13 @@
</div> </div>
{% include 'infobox.html' with message=message %} {% include 'infobox.html' with message=message %}
<div class="row"> <div class="row">
<div class="col s12 l6 margin-bottom"> <div class="col m12 xl4 margin-bottom">
<a href="{% url 'sync:validate-source' source_type='youtube-channel' %}" class="btn">Add a YouTube channel <i class="fab fa-youtube"></i></a> <a href="{% url 'sync:validate-source' source_type='youtube-channel' %}" class="btn">Add a YouTube channel <i class="fab fa-youtube"></i></a>
</div> </div>
<div class="col s12 l6 margin-bottom"> <div class="col m12 xl4 margin-bottom">
<a href="{% url 'sync:validate-source' source_type='youtube-channel-id' %}" class="btn">Add a YouTube channel by ID <i class="fab fa-youtube"></i></a>
</div>
<div class="col m12 xl4 margin-bottom">
<a href="{% url 'sync:validate-source' source_type='youtube-playlist' %}" class="btn">Add a YouTube playlist <i class="fab fa-youtube"></i></a> <a href="{% url 'sync:validate-source' source_type='youtube-playlist' %}" class="btn">Add a YouTube playlist <i class="fab fa-youtube"></i></a>
</div> </div>
</div> </div>

View File

@@ -3,15 +3,24 @@
"upload_date":"20170911", "upload_date":"20170911",
"license":null, "license":null,
"creator":null, "creator":null,
"title":"no fancy stuff", "title":"no fancy stuff title",
"alt_title":null, "alt_title":null,
"description":"no fancy stuff", "description":"no fancy stuff desc",
"categories":[], "average_rating": 1.2345,
"dislike_count": 123,
"like_count": 456,
"playlist_index": 789,
"playlist_title": "test playlist",
"uploader": "test uploader",
"categories":[
"test category 1",
"test category 2"
],
"tags":[], "tags":[],
"subtitles":{}, "subtitles":{},
"automatic_captions":{}, "automatic_captions":{},
"duration":401.0, "duration":401.0,
"age_limit":0, "age_limit":50,
"annotations":null, "annotations":null,
"chapters":null, "chapters":null,
"formats":[ "formats":[

View File

@@ -6,7 +6,9 @@
import logging import logging
from datetime import datetime
from urllib.parse import urlsplit from urllib.parse import urlsplit
from xml.etree import ElementTree
from django.conf import settings from django.conf import settings
from django.test import TestCase, Client from django.test import TestCase, Client
from django.utils import timezone from django.utils import timezone
@@ -28,6 +30,7 @@ class FrontEndTestCase(TestCase):
def test_validate_source(self): def test_validate_source(self):
test_source_types = { test_source_types = {
'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL, 'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL,
'youtube-channel-id': Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID,
'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST, 'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST,
} }
test_sources = { test_sources = {
@@ -36,8 +39,6 @@ class FrontEndTestCase(TestCase):
'https://www.youtube.com/testchannel', 'https://www.youtube.com/testchannel',
'https://www.youtube.com/c/testchannel', 'https://www.youtube.com/c/testchannel',
'https://www.youtube.com/c/testchannel/videos', 'https://www.youtube.com/c/testchannel/videos',
'https://www.youtube.com/channel/testchannel',
'https://www.youtube.com/channel/testchannel/videos',
), ),
'invalid_schema': ( 'invalid_schema': (
'http://www.youtube.com/c/playlist', 'http://www.youtube.com/c/playlist',
@@ -53,13 +54,37 @@ class FrontEndTestCase(TestCase):
), ),
'invalid_is_playlist': ( 'invalid_is_playlist': (
'https://www.youtube.com/c/playlist', 'https://www.youtube.com/c/playlist',
'https://www.youtube.com/c/playlist', ),
'invalid_channel_with_id': (
'https://www.youtube.com/channel/channelid',
'https://www.youtube.com/channel/channelid/videos',
),
},
'youtube-channel-id': {
'valid': (
'https://www.youtube.com/channel/channelid',
'https://www.youtube.com/channel/channelid/videos',
),
'invalid_schema': (
'http://www.youtube.com/channel/channelid',
'ftp://www.youtube.com/channel/channelid',
),
'invalid_domain': (
'https://www.test.com/channel/channelid',
'https://www.example.com/channel/channelid',
),
'invalid_path': (
'https://www.youtube.com/test/invalid',
'https://www.youtube.com/channel/test/invalid',
),
'invalid_is_named_channel': (
'https://www.youtube.com/c/testname',
), ),
}, },
'youtube-playlist': { 'youtube-playlist': {
'valid': ( 'valid': (
'https://www.youtube.com/playlist?list=testplaylist' 'https://www.youtube.com/playlist?list=testplaylist',
'https://www.youtube.com/watch?v=testvideo&list=testplaylist' 'https://www.youtube.com/watch?v=testvideo&list=testplaylist',
), ),
'invalid_schema': ( 'invalid_schema': (
'http://www.youtube.com/playlist?list=testplaylist', 'http://www.youtube.com/playlist?list=testplaylist',
@@ -76,6 +101,7 @@ class FrontEndTestCase(TestCase):
'invalid_is_channel': ( 'invalid_is_channel': (
'https://www.youtube.com/testchannel', 'https://www.youtube.com/testchannel',
'https://www.youtube.com/c/testchannel', 'https://www.youtube.com/c/testchannel',
'https://www.youtube.com/channel/testchannel',
), ),
} }
} }
@@ -86,19 +112,21 @@ class FrontEndTestCase(TestCase):
response = c.get('/source-validate/invalid') response = c.get('/source-validate/invalid')
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
for (source_type, tests) in test_sources.items(): for (source_type, tests) in test_sources.items():
for test, field in tests.items(): for test, urls in tests.items():
source_type_char = test_source_types.get(source_type) for url in urls:
data = {'source_url': field, 'source_type': source_type_char} source_type_char = test_source_types.get(source_type)
response = c.post(f'/source-validate/{source_type}', data) data = {'source_url': url, 'source_type': source_type_char}
if test == 'valid': response = c.post(f'/source-validate/{source_type}', data)
# Valid source tests should bounce to /source-add if test == 'valid':
self.assertEqual(response.status_code, 302) # Valid source tests should bounce to /source-add
url_parts = urlsplit(response.url) self.assertEqual(response.status_code, 302)
self.assertEqual(url_parts.path, '/source-add') url_parts = urlsplit(response.url)
else: self.assertEqual(url_parts.path, '/source-add')
# Invalid source tests should reload the page with an error message else:
self.assertEqual(response.status_code, 200) # Invalid source tests should reload the page with an error
self.assertIn('<ul class="errorlist">', response.content.decode()) self.assertEqual(response.status_code, 200)
self.assertIn('<ul class="errorlist">',
response.content.decode())
def test_add_source_prepopulation(self): def test_add_source_prepopulation(self):
c = Client() c = Client()
@@ -135,6 +163,7 @@ class FrontEndTestCase(TestCase):
'name': 'testname', 'name': 'testname',
'directory': 'testdirectory', 'directory': 'testdirectory',
'media_format': settings.MEDIA_FORMATSTR_DEFAULT, 'media_format': settings.MEDIA_FORMATSTR_DEFAULT,
'download_cap': 0,
'index_schedule': 3600, 'index_schedule': 3600,
'delete_old_media': False, 'delete_old_media': False,
'days_to_keep': 14, 'days_to_keep': 14,
@@ -175,6 +204,7 @@ class FrontEndTestCase(TestCase):
'name': 'testname', 'name': 'testname',
'directory': 'testdirectory', 'directory': 'testdirectory',
'media_format': settings.MEDIA_FORMATSTR_DEFAULT, 'media_format': settings.MEDIA_FORMATSTR_DEFAULT,
'download_cap': 0,
'index_schedule': Source.IndexSchedule.EVERY_HOUR, 'index_schedule': Source.IndexSchedule.EVERY_HOUR,
'delete_old_media': False, 'delete_old_media': False,
'days_to_keep': 14, 'days_to_keep': 14,
@@ -203,6 +233,7 @@ class FrontEndTestCase(TestCase):
'name': 'testname', 'name': 'testname',
'directory': 'testdirectory', 'directory': 'testdirectory',
'media_format': settings.MEDIA_FORMATSTR_DEFAULT, 'media_format': settings.MEDIA_FORMATSTR_DEFAULT,
'download_cap': 0,
'index_schedule': Source.IndexSchedule.EVERY_2_HOURS, # changed 'index_schedule': Source.IndexSchedule.EVERY_2_HOURS, # changed
'delete_old_media': False, 'delete_old_media': False,
'days_to_keep': 14, 'days_to_keep': 14,
@@ -404,7 +435,6 @@ class FrontEndTestCase(TestCase):
response = c.get('/tasks-completed') response = c.get('/tasks-completed')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_mediasevrers(self): def test_mediasevrers(self):
# Media servers overview page # Media servers overview page
c = Client() c = Client()
@@ -481,6 +511,12 @@ class FilepathTestCase(TestCase):
self.source.media_format = 'test-{yyyy}' self.source.media_format = 'test-{yyyy}'
self.assertEqual(self.source.get_example_media_format(), self.assertEqual(self.source.get_example_media_format(),
'test-' + timezone.now().strftime('%Y')) 'test-' + timezone.now().strftime('%Y'))
self.source.media_format = 'test-{mm}'
self.assertEqual(self.source.get_example_media_format(),
'test-' + timezone.now().strftime('%m'))
self.source.media_format = 'test-{dd}'
self.assertEqual(self.source.get_example_media_format(),
'test-' + timezone.now().strftime('%d'))
self.source.media_format = 'test-{source}' self.source.media_format = 'test-{source}'
self.assertEqual(self.source.get_example_media_format(), self.assertEqual(self.source.get_example_media_format(),
'test-' + self.source.slugname) 'test-' + self.source.slugname)
@@ -499,6 +535,12 @@ class FilepathTestCase(TestCase):
self.source.media_format = 'test-{format}' self.source.media_format = 'test-{format}'
self.assertEqual(self.source.get_example_media_format(), self.assertEqual(self.source.get_example_media_format(),
'test-1080p-vp9-opus') 'test-1080p-vp9-opus')
self.source.media_format = 'test-{playlist_index}'
self.assertEqual(self.source.get_example_media_format(),
'test-1')
self.source.media_format = 'test-{playlist_title}'
self.assertEqual(self.source.get_example_media_format(),
'test-Some Playlist Title')
self.source.media_format = 'test-{ext}' self.source.media_format = 'test-{ext}'
self.assertEqual(self.source.get_example_media_format(), self.assertEqual(self.source.get_example_media_format(),
'test-' + self.source.extension) 'test-' + self.source.extension)
@@ -556,7 +598,79 @@ class FilepathTestCase(TestCase):
self.source.media_format = ('{title}_{key}_{resolution}-{height}x{width}-' self.source.media_format = ('{title}_{key}_{resolution}-{height}x{width}-'
'{acodec}-{vcodec}-{fps}fps-{hdr}.{ext}') '{acodec}-{vcodec}-{fps}fps-{hdr}.{ext}')
self.assertEqual(test_media.filename, self.assertEqual(test_media.filename,
'no-fancy-stuff_test_720p-720x1280-opus-vp9-30fps-hdr.mkv') ('no-fancy-stuff-title_test_720p-720x1280-opus'
'-vp9-30fps-hdr.mkv'))
class MediaTestCase(TestCase):
def setUp(self):
# Disable general logging for test case
logging.disable(logging.CRITICAL)
# Add a test source
self.source = Source.objects.create(
source_type=Source.SOURCE_TYPE_YOUTUBE_CHANNEL,
key='testkey',
name='testname',
directory='testdirectory',
media_format=settings.MEDIA_FORMATSTR_DEFAULT,
index_schedule=3600,
delete_old_media=False,
days_to_keep=14,
source_resolution=Source.SOURCE_RESOLUTION_1080P,
source_vcodec=Source.SOURCE_VCODEC_VP9,
source_acodec=Source.SOURCE_ACODEC_OPUS,
prefer_60fps=False,
prefer_hdr=False,
fallback=Source.FALLBACK_FAIL
)
# Add some test media
self.media = Media.objects.create(
key='mediakey',
source=self.source,
metadata=metadata,
)
# Fix a created datetime for predictable testing
self.media.created = datetime(year=2020, month=1, day=1, hour=1,
minute=1, second=1)
def test_nfo(self):
expected_nfo = [
"<?xml version='1.0' encoding='utf8'?>",
'<episodedetails>',
' <title>no fancy stuff title</title>',
' <showtitle>testname</showtitle>',
' <ratings>',
' <rating default="True" max="5" name="youtube">',
' <value>1.2345</value>',
' <votes>579</votes>',
' </rating>',
' </ratings>',
' <plot>no fancy stuff desc</plot>',
' <thumb />', # media.thumbfile is empty without media existing
' <mpaa>50</mpaa>',
' <runtime>401</runtime>',
' <id>mediakey</id>',
' <uniqueid default="True" type="youtube">mediakey</uniqueid>',
' <studio>test uploader</studio>',
' <aired>2017-09-11</aired>',
' <dateadded>2020-01-01 01:01:01</dateadded>',
' <genre>test category 1</genre>',
' <genre>test category 2</genre>',
'</episodedetails>',
]
expected_tree = ElementTree.fromstring('\n'.join(expected_nfo))
nfo_tree = ElementTree.fromstring(self.media.nfoxml)
# Check each node with attribs in expected_tree is present in test_nfo
for expected_node in expected_tree:
# Ignore checking <genre>, only tag we may have multiple of
if expected_node.tag == 'genre':
continue
# Find the same node in the NFO XML tree
nfo_node = nfo_tree.find(expected_node.tag)
self.assertEqual(expected_node.attrib, nfo_node.attrib)
self.assertEqual(expected_node.tag, nfo_node.tag)
self.assertEqual(expected_node.text, nfo_node.text)
class FormatMatchingTestCase(TestCase): class FormatMatchingTestCase(TestCase):

View File

@@ -108,6 +108,14 @@ def file_is_editable(filepath):
return False return False
def write_text_file(filepath, filedata):
if not isinstance(filedata, str):
raise ValueError(f'filedata must be a str, got "{type(filedata)}"')
with open(filepath, 'wt') as f:
bytes_written = f.write(filedata)
return bytes_written
def delete_file(filepath): def delete_file(filepath):
if file_is_editable(filepath): if file_is_editable(filepath):
return os.remove(filepath) return os.remove(filepath)

View File

@@ -128,10 +128,12 @@ class ValidateSourceView(FormView):
} }
source_types = { source_types = {
'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL, 'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL,
'youtube-channel-id': Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID,
'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST, 'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST,
} }
help_item = { help_item = {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: _('YouTube channel'), Source.SOURCE_TYPE_YOUTUBE_CHANNEL: _('YouTube channel'),
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: _('YouTube channel ID'),
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: _('YouTube playlist'), Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: _('YouTube playlist'),
} }
help_texts = { help_texts = {
@@ -141,6 +143,13 @@ class ValidateSourceView(FormView):
'where <strong>CHANNELNAME</strong> is the name of the channel you want ' 'where <strong>CHANNELNAME</strong> is the name of the channel you want '
'to add.' 'to add.'
), ),
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: _(
'Enter a YouTube channel URL by channel ID into the box below. A channel '
'URL by channel ID will be in the format of <strong>'
'https://www.youtube.com/channel/BiGLoNgUnIqUeId</strong> '
'where <strong>BiGLoNgUnIqUeId</strong> is the ID of the channel you want '
'to add.'
),
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: _( Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: _(
'Enter a YouTube playlist URL into the box below. A playlist URL will be ' 'Enter a YouTube playlist URL into the box below. A playlist URL will be '
'in the format of <strong>https://www.youtube.com/playlist?list=' 'in the format of <strong>https://www.youtube.com/playlist?list='
@@ -150,6 +159,8 @@ class ValidateSourceView(FormView):
} }
help_examples = { help_examples = {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/google', Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/google',
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: ('https://www.youtube.com/channel/'
'UCK8sQmJBp8GCxrOtXWBpyEA'),
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: ('https://www.youtube.com/playlist?list=' Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: ('https://www.youtube.com/playlist?list='
'PL590L5WQmH8dpP0RyH5pCfIaDEdt9nk7r') 'PL590L5WQmH8dpP0RyH5pCfIaDEdt9nk7r')
} }
@@ -157,12 +168,21 @@ class ValidateSourceView(FormView):
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: { Source.SOURCE_TYPE_YOUTUBE_CHANNEL: {
'scheme': 'https', 'scheme': 'https',
'domain': 'www.youtube.com', 'domain': 'www.youtube.com',
'path_regex': '^\/(c\/|channel\/)?([^\/]+)(\/videos)?$', 'path_regex': '^\/(c\/)?([^\/]+)(\/videos)?$',
'path_must_not_match': ('/playlist', '/c/playlist'), 'path_must_not_match': ('/playlist', '/c/playlist'),
'qs_args': [], 'qs_args': [],
'extract_key': ('path_regex', 1), 'extract_key': ('path_regex', 1),
'example': 'https://www.youtube.com/SOMECHANNEL' 'example': 'https://www.youtube.com/SOMECHANNEL'
}, },
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: {
'scheme': 'https',
'domain': 'www.youtube.com',
'path_regex': '^\/channel\/([^\/]+)(\/videos)?$',
'path_must_not_match': ('/playlist', '/c/playlist'),
'qs_args': [],
'extract_key': ('path_regex', 0),
'example': 'https://www.youtube.com/channel/CHANNELID'
},
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: { Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: {
'scheme': 'https', 'scheme': 'https',
'domain': 'www.youtube.com', 'domain': 'www.youtube.com',
@@ -175,6 +195,7 @@ class ValidateSourceView(FormView):
} }
prepopulate_fields = { prepopulate_fields = {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: ('source_type', 'key', 'name', 'directory'), Source.SOURCE_TYPE_YOUTUBE_CHANNEL: ('source_type', 'key', 'name', 'directory'),
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: ('source_type', 'key'),
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: ('source_type', 'key'), Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: ('source_type', 'key'),
} }
@@ -253,9 +274,9 @@ class AddSourceView(CreateView):
template_name = 'sync/source-add.html' template_name = 'sync/source-add.html'
model = Source model = Source
fields = ('source_type', 'key', 'name', 'directory', 'media_format', fields = ('source_type', 'key', 'name', 'directory', 'media_format',
'index_schedule', 'delete_old_media', 'days_to_keep', 'index_schedule', 'download_cap', 'delete_old_media', 'days_to_keep',
'source_resolution', 'source_vcodec', 'source_acodec', 'prefer_60fps', 'source_resolution', 'source_vcodec', 'source_acodec', 'prefer_60fps',
'prefer_hdr', 'fallback', 'copy_thumbnails') 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo')
errors = { errors = {
'invalid_media_format': _('Invalid media format, the media format contains ' 'invalid_media_format': _('Invalid media format, the media format contains '
'errors or is empty. Check the table at the end of ' 'errors or is empty. Check the table at the end of '
@@ -313,8 +334,8 @@ class SourceView(DetailView):
messages = { messages = {
'source-created': _('Your new source has been created. If you have added a ' 'source-created': _('Your new source has been created. If you have added a '
'very large source such as a channel with hundreds of ' 'very large source such as a channel with hundreds of '
'videos it can take several minutes for media to start ' 'videos it can take several minutes or up to an hour '
'to appear.'), 'for media to start to appear.'),
'source-updated': _('Your source has been updated.'), 'source-updated': _('Your source has been updated.'),
} }
@@ -344,9 +365,9 @@ class UpdateSourceView(UpdateView):
template_name = 'sync/source-update.html' template_name = 'sync/source-update.html'
model = Source model = Source
fields = ('source_type', 'key', 'name', 'directory', 'media_format', fields = ('source_type', 'key', 'name', 'directory', 'media_format',
'index_schedule', 'delete_old_media', 'days_to_keep', 'index_schedule', 'download_cap', 'delete_old_media', 'days_to_keep',
'source_resolution', 'source_vcodec', 'source_acodec', 'prefer_60fps', 'source_resolution', 'source_vcodec', 'source_acodec', 'prefer_60fps',
'prefer_hdr', 'fallback', 'copy_thumbnails') 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo')
errors = { errors = {
'invalid_media_format': _('Invalid media format, the media format contains ' 'invalid_media_format': _('Invalid media format, the media format contains '
'errors or is empty. Check the table at the end of ' 'errors or is empty. Check the table at the end of '
@@ -390,7 +411,12 @@ class DeleteSourceView(DeleteView, FormMixin):
source = self.get_object() source = self.get_object()
for media in Media.objects.filter(source=source): for media in Media.objects.filter(source=source):
if media.media_file: if media.media_file:
# Delete the media file
delete_file(media.media_file.name) delete_file(media.media_file.name)
# Delete thumbnail copy if it exists
delete_file(media.thumbpath)
# Delete NFO file if it exists
delete_file(media.nfopath)
return super().post(request, *args, **kwargs) return super().post(request, *args, **kwargs)
def get_success_url(self): def get_success_url(self):
@@ -535,13 +561,12 @@ class MediaRedownloadView(FormView, SingleObjectMixin):
self.object.thumb = None self.object.thumb = None
# If the media file exists on disk, delete it # If the media file exists on disk, delete it
if self.object.media_file_exists: if self.object.media_file_exists:
filepath = self.object.media_file.path delete_file(self.object.media_file.path)
delete_file(filepath)
self.object.media_file = None self.object.media_file = None
# If the media has an associated thumbnail copied, also delete it # If the media has an associated thumbnail copied, also delete it
barefilepath, fileext = os.path.splitext(filepath) delete_file(self.object.thumbpath)
thumbpath = f'{barefilepath}.jpg' # If the media has an associated NFO file with it, also delete it
delete_file(thumbpath) delete_file(self.object.nfopath)
# Reset all download data # Reset all download data
self.object.downloaded = False self.object.downloaded = False
self.object.downloaded_audio_codec = None self.object.downloaded_audio_codec = None
@@ -581,13 +606,12 @@ class MediaSkipView(FormView, SingleObjectMixin):
delete_task_by_media('sync.tasks.download_media', (str(self.object.pk),)) delete_task_by_media('sync.tasks.download_media', (str(self.object.pk),))
# If the media file exists on disk, delete it # If the media file exists on disk, delete it
if self.object.media_file_exists: if self.object.media_file_exists:
filepath = self.object.media_file.path
delete_file(self.object.media_file.path) delete_file(self.object.media_file.path)
self.object.media_file = None self.object.media_file = None
# If the media has an associated thumbnail copied, also delete it # If the media has an associated thumbnail copied, also delete it
barefilepath, fileext = os.path.splitext(filepath) delete_file(self.object.thumbpath)
thumbpath = f'{barefilepath}.jpg' # If the media has an associated NFO file with it, also delete it
delete_file(thumbpath) delete_file(self.object.nfopath)
# Reset all download data # Reset all download data
self.object.downloaded = False self.object.downloaded = False
self.object.downloaded_audio_codec = None self.object.downloaded_audio_codec = None

View File

@@ -15,6 +15,7 @@ SECRET_KEY = str(os.getenv('DJANGO_SECRET_KEY', 'tubesync-django-secret'))
ALLOWED_HOSTS_STR = str(os.getenv('TUBESYNC_HOSTS', '127.0.0.1,localhost')) ALLOWED_HOSTS_STR = str(os.getenv('TUBESYNC_HOSTS', '127.0.0.1,localhost'))
ALLOWED_HOSTS = ALLOWED_HOSTS_STR.split(',') ALLOWED_HOSTS = ALLOWED_HOSTS_STR.split(',')
DEBUG = True if os.getenv('TUBESYNC_DEBUG', False) else False DEBUG = True if os.getenv('TUBESYNC_DEBUG', False) else False
FORCE_SCRIPT_NAME = os.getenv('DJANGO_FORCE_SCRIPT_NAME', None)
TIME_ZONE = os.getenv('TZ', 'UTC') TIME_ZONE = os.getenv('TZ', 'UTC')

View File

@@ -6,7 +6,7 @@ CONFIG_BASE_DIR = BASE_DIR
DOWNLOADS_BASE_DIR = BASE_DIR DOWNLOADS_BASE_DIR = BASE_DIR
VERSION = 0.4 VERSION = 0.6
SECRET_KEY = '' SECRET_KEY = ''
DEBUG = False DEBUG = False
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
@@ -41,6 +41,7 @@ MIDDLEWARE = [
ROOT_URLCONF = 'tubesync.urls' ROOT_URLCONF = 'tubesync.urls'
FORCE_SCRIPT_NAME = None
TEMPLATES = [ TEMPLATES = [