Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SaveMyPlaylist
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robert Goldmann
SaveMyPlaylist
Commits
339683b9
Commit
339683b9
authored
5 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
Fixed
#1
- use logger
parent
581abfdf
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
SaveMyPlaylist.py
+39
-12
39 additions, 12 deletions
SaveMyPlaylist.py
with
39 additions
and
12 deletions
SaveMyPlaylist.py
+
39
−
12
View file @
339683b9
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
json
import
json
import
logging
import
os
import
os
import
sys
import
sys
...
@@ -11,6 +12,31 @@ import youtube_dl
...
@@ -11,6 +12,31 @@ import youtube_dl
VERSION
=
(
2
,
0
,
0
)
VERSION
=
(
2
,
0
,
0
)
def
prepare_logging
()
->
logging
.
Logger
:
LOG_FORMAT
=
'
[%(levelname)-7s] - %(asctime)s - %(message)s
'
DATE_FORMAT
=
'
%Y-%m-%d %H:%M:%S
'
LOG_FORMATTER
=
logging
.
Formatter
(
fmt
=
LOG_FORMAT
,
datefmt
=
DATE_FORMAT
)
logger
=
logging
.
getLogger
(
'
SaveMyPlaylist
'
)
logger
.
setLevel
(
logging
.
DEBUG
)
outHandler
=
logging
.
StreamHandler
(
sys
.
stdout
)
outHandler
.
setFormatter
(
LOG_FORMATTER
)
outHandler
.
setLevel
(
logging
.
DEBUG
)
outHandler
.
addFilter
(
lambda
record
:
record
.
levelno
<=
logging
.
INFO
)
logger
.
addHandler
(
outHandler
)
errHandler
=
logging
.
StreamHandler
(
sys
.
stderr
)
errHandler
.
setFormatter
(
LOG_FORMATTER
)
errHandler
.
setLevel
(
logging
.
WARNING
)
logger
.
addHandler
(
errHandler
)
return
logger
logger
=
prepare_logging
()
class
MyLogger
(
object
):
class
MyLogger
(
object
):
def
debug
(
self
,
msg
):
def
debug
(
self
,
msg
):
pass
pass
...
@@ -24,7 +50,7 @@ class MyLogger(object):
...
@@ -24,7 +50,7 @@ class MyLogger(object):
def
my_hook
(
d
):
def
my_hook
(
d
):
if
d
[
'
status
'
]
==
'
finished
'
:
if
d
[
'
status
'
]
==
'
finished
'
:
print
(
'
Download finished!
'
)
logger
.
info
(
'
Download finished!
'
)
class
SaveMyPlaylist
:
class
SaveMyPlaylist
:
...
@@ -39,8 +65,8 @@ class SaveMyPlaylist:
...
@@ -39,8 +65,8 @@ class SaveMyPlaylist:
ILLEGAL_CHARS
=
[
'
NUL
'
,
'
\'
,
''
//
'
,
'
:
'
,
'
*
'
,
'"'
,
'
<
'
,
'
>
'
,
'
|
'
]
ILLEGAL_CHARS
=
[
'
NUL
'
,
'
\'
,
''
//
'
,
'
:
'
,
'
*
'
,
'"'
,
'
<
'
,
'
>
'
,
'
|
'
]
def
__init__
(
self
,
apiKey
,
playlistId
):
def
__init__
(
self
,
apiKey
,
playlistId
):
print
(
'
### SaveMyPlaylist v{} ###
'
.
format
(
'
.
'
.
join
(
str
(
i
)
for
i
in
VERSION
)))
logger
.
info
(
'
### SaveMyPlaylist v{} ###
'
.
format
(
'
.
'
.
join
(
str
(
i
)
for
i
in
VERSION
)))
print
(
'
=============================
\n
'
)
logger
.
info
(
'
=============================
'
)
self
.
_apiKey
=
apiKey
self
.
_apiKey
=
apiKey
self
.
_playlistId
=
playlistId
self
.
_playlistId
=
playlistId
self
.
_youtubeApi
=
googleapiclient
.
discovery
.
build
(
self
.
API_NAME
,
self
.
API_VERSION
,
developerKey
=
self
.
_apiKey
)
self
.
_youtubeApi
=
googleapiclient
.
discovery
.
build
(
self
.
API_NAME
,
self
.
API_VERSION
,
developerKey
=
self
.
_apiKey
)
...
@@ -54,7 +80,7 @@ class SaveMyPlaylist:
...
@@ -54,7 +80,7 @@ class SaveMyPlaylist:
pageItems
,
nextPageToken
=
self
.
__fetch_playlist_items
(
nextPageToken
)
pageItems
,
nextPageToken
=
self
.
__fetch_playlist_items
(
nextPageToken
)
items
.
extend
(
pageItems
)
items
.
extend
(
pageItems
)
print
(
'
\n
>>> Found {} items in playlist
\n
'
.
format
(
len
(
items
)))
logger
.
info
(
'
>>> Found {} items in playlist
'
.
format
(
len
(
items
)))
return
items
return
items
def
__fetch_playlist_items
(
self
,
nextPageToken
=
None
):
def
__fetch_playlist_items
(
self
,
nextPageToken
=
None
):
...
@@ -75,13 +101,14 @@ class SaveMyPlaylist:
...
@@ -75,13 +101,14 @@ class SaveMyPlaylist:
response
=
request
.
execute
()
response
=
request
.
execute
()
items
=
[]
items
=
[]
logger
.
info
(
'
Found {} videos in playlist
'
.
format
(
len
(
response
[
'
items
'
])))
for
item
in
response
[
'
items
'
]:
for
item
in
response
[
'
items
'
]:
snippet
=
item
[
'
snippet
'
]
snippet
=
item
[
'
snippet
'
]
title
=
snippet
[
'
title
'
]
title
=
snippet
[
'
title
'
]
channel
=
snippet
[
'
channelTitle
'
]
channel
=
snippet
[
'
channelTitle
'
]
videoId
=
snippet
[
'
resourceId
'
][
'
videoId
'
]
videoId
=
snippet
[
'
resourceId
'
][
'
videoId
'
]
items
.
append
((
channel
,
title
,
videoId
))
items
.
append
((
channel
,
title
,
videoId
))
print
(
'
{} - {} (videoId: {})
'
.
format
(
channel
,
title
,
videoId
))
logger
.
info
(
'
{} - {} (videoId: {})
'
.
format
(
channel
,
title
,
videoId
))
nextPageToken
=
None
nextPageToken
=
None
if
'
nextPageToken
'
in
response
:
if
'
nextPageToken
'
in
response
:
...
@@ -91,22 +118,22 @@ class SaveMyPlaylist:
...
@@ -91,22 +118,22 @@ class SaveMyPlaylist:
def
download_items
(
self
,
destinationFolder
,
debug
=
False
):
def
download_items
(
self
,
destinationFolder
,
debug
=
False
):
os
.
makedirs
(
destinationFolder
,
exist_ok
=
True
)
os
.
makedirs
(
destinationFolder
,
exist_ok
=
True
)
print
(
'
>>> Scanning destination folder...
'
)
logger
.
info
(
'
>>> Scanning destination folder...
'
)
downloadedVideos
=
[
f
for
f
in
os
.
listdir
(
destinationFolder
)
if
downloadedVideos
=
[
f
for
f
in
os
.
listdir
(
destinationFolder
)
if
os
.
path
.
isfile
(
os
.
path
.
join
(
destinationFolder
,
f
))
and
f
.
endswith
(
'
.mp4
'
)]
os
.
path
.
isfile
(
os
.
path
.
join
(
destinationFolder
,
f
))
and
f
.
endswith
(
'
.mp4
'
)]
print
(
'
>>> Found {} videos in destination folder
'
.
format
(
len
(
downloadedVideos
)))
logger
.
info
(
'
>>> Found {} videos in destination folder
'
.
format
(
len
(
downloadedVideos
)))
print
(
'
\n
>>> Started Downloading...
'
)
logger
.
info
(
'
>>> Started Downloading...
'
)
newVideos
=
[]
newVideos
=
[]
for
idx
,
item
in
enumerate
(
self
.
_items
):
for
idx
,
item
in
enumerate
(
self
.
_items
):
fileName
=
'
{} - {}.mp4
'
.
format
(
item
[
self
.
TITLE
],
item
[
self
.
CHANNEL
])
fileName
=
'
{} - {}.mp4
'
.
format
(
item
[
self
.
TITLE
],
item
[
self
.
CHANNEL
])
fileName
=
self
.
__escape_file_name
(
fileName
)
fileName
=
self
.
__escape_file_name
(
fileName
)
if
fileName
in
downloadedVideos
:
if
fileName
in
downloadedVideos
:
print
(
'
Skipping {}/{}:
"
{}
"
as it already exists
'
.
format
(
idx
+
1
,
len
(
self
.
_items
),
fileName
))
logger
.
info
(
'
Skipping {}/{}:
"
{}
"
as it already exists
'
.
format
(
idx
+
1
,
len
(
self
.
_items
),
fileName
))
continue
continue
print
(
'
Downloading {}/{}:
"
{}
"'
.
format
(
idx
+
1
,
len
(
self
.
_items
),
fileName
))
logger
.
info
(
'
Downloading {}/{}:
"
{}
"'
.
format
(
idx
+
1
,
len
(
self
.
_items
),
fileName
))
newVideos
.
append
(
item
)
newVideos
.
append
(
item
)
ydl_opts
=
{
ydl_opts
=
{
...
@@ -123,8 +150,8 @@ class SaveMyPlaylist:
...
@@ -123,8 +150,8 @@ class SaveMyPlaylist:
with
youtube_dl
.
YoutubeDL
(
ydl_opts
)
as
ydl
:
with
youtube_dl
.
YoutubeDL
(
ydl_opts
)
as
ydl
:
ydl
.
download
([
'
https://www.youtube.com/watch?v={}
'
.
format
(
item
[
self
.
VIDEO_ID
])])
ydl
.
download
([
'
https://www.youtube.com/watch?v={}
'
.
format
(
item
[
self
.
VIDEO_ID
])])
print
(
'
\n
>>> Finished Downloading
'
)
logger
.
info
(
'
>>> Finished Downloading
'
)
print
(
'
Downloaded {} new videos
'
.
format
(
len
(
newVideos
)))
logger
.
info
(
'
Downloaded {} new videos
'
.
format
(
len
(
newVideos
)))
def
__escape_file_name
(
self
,
fileName
):
def
__escape_file_name
(
self
,
fileName
):
for
char
in
self
.
ILLEGAL_CHARS
:
for
char
in
self
.
ILLEGAL_CHARS
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment