Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
radiopanik
django-panik-nonstop
Commits
89bc776e
Commit
89bc776e
authored
Feb 20, 2022
by
fred
Browse files
add import-tracks management command
parent
23da0446
Changes
1
Hide whitespace changes
Inline
Side-by-side
nonstop/management/commands/import-tracks.py
0 → 100644
View file @
89bc776e
import
datetime
import
os
import
mutagen
from
django.core.files.storage
import
default_storage
from
django.core.management.base
import
BaseCommand
from
django.utils.timezone
import
now
from
emissions.models
import
Nonstop
from
...models
import
Artist
,
NonstopFile
,
Track
class
Command
(
BaseCommand
):
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'--directory'
,
metavar
=
'DIRECTORY'
)
parser
.
add_argument
(
'--zone'
,
metavar
=
'ZONE'
)
def
handle
(
self
,
verbosity
,
directory
,
zone
=
None
,
**
kwargs
):
monthdir
=
datetime
.
datetime
.
today
().
strftime
(
'%Y-%m'
)
nonstop_zone
=
Nonstop
.
objects
.
get
(
slug
=
zone
)
if
zone
else
None
filepaths
=
[]
for
basedir
,
dirnames
,
filenames
in
os
.
walk
(
directory
):
filepaths
.
extend
(
[
os
.
path
.
join
(
basedir
,
x
)
for
x
in
filenames
if
os
.
path
.
splitext
(
x
)[
-
1
]
in
(
'.mp3'
,
'.ogg'
,
'.flac'
,
'.opus'
)
]
)
filepaths
.
sort
()
total
=
len
(
filepaths
)
for
i
,
filepath
in
enumerate
(
filepaths
):
try
:
metadata
=
mutagen
.
File
(
filepath
,
easy
=
True
)
except
:
print
(
'failed to get metadata'
,
filepath
)
continue
if
not
metadata
or
not
metadata
.
get
(
'artist'
)
or
not
metadata
.
get
(
'title'
):
print
(
'missing metadata'
,
filepath
)
continue
artist_name
=
metadata
.
get
(
'artist'
)[
0
]
track_title
=
metadata
.
get
(
'title'
)[
0
]
if
verbosity
:
print
(
'[%s/%s] %s - %s'
%
(
i
,
total
,
artist_name
,
track_title
))
new_filepath
=
'%s/%s - %s - %s%s'
%
(
monthdir
,
datetime
.
datetime
.
today
().
strftime
(
'%y%m%d'
),
artist_name
[:
50
].
replace
(
'/'
,
' '
).
strip
(),
track_title
[:
80
].
replace
(
'/'
,
' '
).
strip
(),
os
.
path
.
splitext
(
filepath
)[
-
1
],
)
artist
,
created
=
Artist
.
objects
.
get_or_create
(
name
=
artist_name
)
track
,
created
=
Track
.
objects
.
get_or_create
(
title
=
track_title
,
artist
=
artist
,
)
if
created
or
not
track
.
file_exists
():
with
open
(
filepath
,
'rb'
)
as
fd
:
default_storage
.
save
(
os
.
path
.
join
(
'nonstop'
,
'tracks'
,
new_filepath
),
content
=
fd
)
nonstop_file
=
NonstopFile
()
nonstop_file
.
set_track_filepath
(
new_filepath
)
nonstop_file
.
track
=
track
nonstop_file
.
save
()
if
nonstop_zone
:
track
.
nonstop_zones
.
add
(
nonstop_zone
)
if
not
track
.
added_to_nonstop_timestamp
:
track
.
added_to_nonstop_timestamp
=
now
()
track
.
save
()
else
:
if
nonstop_zone
:
track
.
nonstop_zones
.
add
(
nonstop_zone
)
if
not
track
.
added_to_nonstop_timestamp
:
track
.
added_to_nonstop_timestamp
=
now
()
track
.
save
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment