Skip to content

Commit

Permalink
Simplify code and fix issue on playlist-slug in Vue.JS
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Nov 25, 2024
1 parent af15ed0 commit 90d391b
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 128 deletions.
5 changes: 0 additions & 5 deletions scraper/src/youtube2zim/playlists/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def run(self):
(
playlists,
main_channel_id,
uploads_playlist_id,
user_long_uploads_playlist_id,
user_short_uploads_playlist_id,
user_lives_playlist_id,
Expand All @@ -109,10 +108,6 @@ def run(self):
shutil.rmtree(self.build_dir, ignore_errors=True)

for playlist in playlists:
if playlist.playlist_id == uploads_playlist_id:
logger.info(f"Skipping playlist {playlist.playlist_id} (uploads one)")
continue

logger.info(f"Executing youtube2zim for playlist {playlist.playlist_id}")
success, process = self.run_playlist_zim(playlist)
if success:
Expand Down
2 changes: 1 addition & 1 deletion scraper/src/youtube2zim/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Channel(CamelModel):
profile_path: str | None = None
banner_path: str | None = None
joined_date: str
main_playlist: str | None = None
first_playlist: str | None = None
user_long_uploads_playlist: str | None = None
user_short_uploads_playlist: str | None = None
user_lives_playlist: str | None = None

Check warning on line 111 in scraper/src/youtube2zim/schemas.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/schemas.py#L108-L111

Added lines #L108 - L111 were not covered by tests
Expand Down
91 changes: 4 additions & 87 deletions scraper/src/youtube2zim/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def __init__(

# process-related
self.playlists = []
self.uploads_playlist_id = None
self.user_long_uploads_playlist_id = None
self.user_short_uploads_playlist_id = None
self.user_lives_playlist_id = None

Check warning on line 175 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L173-L175

Added lines #L173 - L175 were not covered by tests
Expand Down Expand Up @@ -232,30 +231,6 @@ def banner_path(self):
def is_single_channel(self):
return len({pl.creator_id for pl in self.playlists}) == 1

@property
def sorted_playlists(self):
"""sorted list of playlists (by title) but with Uploads one at first if any"""
if len(self.playlists) <= 1:
return self.playlists

sorted_playlists = sorted(self.playlists, key=lambda x: x.title)
index = 0
# make sure our Uploads, special playlist is first
if self.uploads_playlist_id:
try:
index = [
index
for index, p in enumerate(sorted_playlists)
if p.playlist_id == self.uploads_playlist_id
][-1]
except Exception:
index = 0
return (
[sorted_playlists[index]]
+ sorted_playlists[0:index]
+ sorted_playlists[index + 1 :]
)

def run(self):
"""execute the scraper step by step"""

Expand Down Expand Up @@ -555,7 +530,6 @@ def extract_playlists(self):
(
self.playlists,
self.main_channel_id,
self.uploads_playlist_id,
self.user_long_uploads_playlist_id,
self.user_short_uploads_playlist_id,
self.user_lives_playlist_id,
Expand Down Expand Up @@ -1158,10 +1132,9 @@ def get_playlist_slug(playlist) -> str:
)

# write playlists JSON files
playlist_list = []
home_playlist_list = []
playlist_list: list[PlaylistPreview] = []
home_playlist_list: list[Playlist] = []

Check warning on line 1136 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L1135-L1136

Added lines #L1135 - L1136 were not covered by tests

main_playlist_slug = None
user_long_uploads_playlist_slug = None
user_short_uploads_playlist_slug = None
user_lives_playlist_slug = None

Check warning on line 1140 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L1138-L1140

Added lines #L1138 - L1140 were not covered by tests
Expand All @@ -1178,18 +1151,6 @@ def get_playlist_slug(playlist) -> str:
if len(self.playlists) == 0:
raise Exception("No playlist succeeded to download")

main_playlist_slug = get_playlist_slug(
self.playlists[0]
) # set first playlist as main playlist

# Initialize placeholders for special playlists
special_playlists: dict[str, dict[str, PlaylistPreview | Playlist]] = {
"user_long_uploads_playlist": {},
"user_short_uploads_playlist": {},
"user_lives_playlist": {},
}
main_playlist = None

for playlist in self.playlists:
playlist_slug = get_playlist_slug(playlist)
playlist_path = f"playlists/{playlist_slug}.json"
Expand All @@ -1214,59 +1175,15 @@ def get_playlist_slug(playlist) -> str:
# modify playlist object for preview on homepage
playlist_obj.videos = playlist_obj.videos[:12]

home_playlist_list.append(playlist_obj)

Check warning on line 1178 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L1178

Added line #L1178 was not covered by tests
if playlist.playlist_id == self.user_long_uploads_playlist_id:
user_long_uploads_playlist_slug = playlist_slug

Check warning on line 1180 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L1180

Added line #L1180 was not covered by tests
special_playlists["user_long_uploads_playlist"] = {
"preview": generate_playlist_preview_object(playlist),
"full": playlist_obj,
}

elif playlist.playlist_id == self.user_short_uploads_playlist_id:
user_short_uploads_playlist_slug = playlist_slug

Check warning on line 1182 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L1182

Added line #L1182 was not covered by tests
special_playlists["user_short_uploads_playlist"] = {
"preview": generate_playlist_preview_object(playlist),
"full": playlist_obj,
}

elif playlist.playlist_id == self.user_lives_playlist_id:
user_lives_playlist_slug = playlist_slug

Check warning on line 1184 in scraper/src/youtube2zim/scraper.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L1184

Added line #L1184 was not covered by tests
special_playlists["user_lives_playlist"] = {
"preview": generate_playlist_preview_object(playlist),
"full": playlist_obj,
}

elif playlist.playlist_id == self.uploads_playlist_id:
main_playlist = playlist
main_playlist_slug = (
playlist_slug # set uploads playlist as main playlist
)
# insert uploads playlist at the beginning of the list
home_playlist_list.insert(0, playlist_obj)
else:
playlist_list.append(generate_playlist_preview_object(playlist))
home_playlist_list.append(playlist_obj)

# Check if only one special playlist exists
special_playlist_count = sum(
1 for k in special_playlists if special_playlists[k]
)

if special_playlist_count == 1:
if main_playlist is not None:
self.playlists.remove(main_playlist)
for key in special_playlists:
if special_playlists[key]:
main_playlist_slug = special_playlists[key]["preview"].slug
home_playlist_list[0] = special_playlists[key]["full"]
else:
# Insert special playlists in the desired order
for key in [
"user_lives_playlist",
"user_short_uploads_playlist",
"user_long_uploads_playlist",
]:
if special_playlists[key]:
home_playlist_list.insert(1, special_playlists[key]["full"])

# write playlists.json file
self.zim_file.add_item_for(
Expand Down Expand Up @@ -1303,7 +1220,7 @@ def get_playlist_slug(playlist) -> str:
channel_description=channel_data["snippet"]["description"],
profile_path="profile.jpg",
banner_path="banner.jpg",
main_playlist=main_playlist_slug,
first_playlist=playlist_list[0].id,
user_long_uploads_playlist=user_long_uploads_playlist_slug,
user_short_uploads_playlist=user_short_uploads_playlist_slug,
user_lives_playlist=user_lives_playlist_slug,
Expand Down
32 changes: 16 additions & 16 deletions scraper/src/youtube2zim/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ def skip_outofrange_videos(date_range, item):
def extract_playlists_details_from(youtube_id: str):
"""prepare a list of Playlist from user request"""

uploads_playlist_id = None
main_channel_id = None
main_channel_id = user_long_uploads_playlist_id = user_short_uploads_playlist_id = (

Check warning on line 346 in scraper/src/youtube2zim/youtube.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/youtube.py#L346

Added line #L346 was not covered by tests
user_lives_playlist_id
) = None
if "," not in youtube_id:
try:
# first try to consider passed ID is a channel ID (or username or handle)
Expand All @@ -369,21 +370,21 @@ def extract_playlists_details_from(youtube_id: str):
)
user_lives_playlist_id = user_lives_json["id"] if user_lives_json else None

Check warning on line 371 in scraper/src/youtube2zim/youtube.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/youtube.py#L371

Added line #L371 was not covered by tests

# Add special playlists if they exists
playlist_ids += filter(
None,
[
user_long_uploads_playlist_id,
user_short_uploads_playlist_id,
user_lives_playlist_id,
],
# Add special playlists if they exists, in proper order
playlist_ids = (

Check warning on line 374 in scraper/src/youtube2zim/youtube.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/youtube2zim/youtube.py#L374

Added line #L374 was not covered by tests
list(
filter(
None,
[
user_long_uploads_playlist_id,
user_short_uploads_playlist_id,
user_lives_playlist_id,
],
)
)
+ playlist_ids
)

# we always include uploads playlist (contains everything)
playlist_ids += [
channel_json["contentDetails"]["relatedPlaylists"]["uploads"]
]
uploads_playlist_id = playlist_ids[-1]
is_playlist = False
except ChannelNotFoundError:
# channel not found, then ID should be a playlist
Expand All @@ -402,7 +403,6 @@ def extract_playlists_details_from(youtube_id: str):
# dict.fromkeys maintains the order of playlist_ids while removing duplicates
[Playlist.from_id(playlist_id) for playlist_id in dict.fromkeys(playlist_ids)],
main_channel_id,
uploads_playlist_id,
user_long_uploads_playlist_id,
user_short_uploads_playlist_id,
user_lives_playlist_id,
Expand Down
2 changes: 1 addition & 1 deletion scraper/tests-integration/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_zim_channel_json():

assert channel_json["id"] == "UC8elThf5TGMpQfQc_VE917Q"
assert channel_json["channelName"] == "openZIM_testing"
assert channel_json["mainPlaylist"] == "uploads_from_openzim_testing-917Q"
assert channel_json["firstPlaylist"] == "uploads_from_openzim_testing-917Q"


def test_zim_videos():
Expand Down
2 changes: 1 addition & 1 deletion zimui/cypress/fixtures/channel/channel.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"profilePath": "profile.jpg",
"bannerPath": "banner.jpg",
"joinedDate": "2024-06-04T13:30:16.232286Z",
"mainPlaylist": "uploads_from_openzim_testing-917Q"
"firstPlaylist": "uploads_from_openzim_testing-917Q"
}
8 changes: 4 additions & 4 deletions zimui/src/components/channel/tabs/ChannelHomeGridTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ const isLoading = ref(true)
// Watch for changes in the main playlist
watch(
() => main.channel?.mainPlaylist,
() => main.channel?.firstPlaylist,
() => {
fetchData()
}
)
// Fetch the videos for the main playlist
const fetchData = async function () {
if (main.channel?.mainPlaylist) {
if (main.channel?.firstPlaylist) {
try {
const resp = await main.fetchPlaylist(main.channel?.mainPlaylist)
const resp = await main.fetchPlaylist(main.channel?.firstPlaylist)
if (resp) {
playlist.value = resp
videos.value = resp.videos
Expand Down Expand Up @@ -54,6 +54,6 @@ onMounted(() => {
:count-text="playlist?.videos.length === 1 ? 'video' : 'videos'"
icon="mdi-video-outline"
/>
<video-grid v-if="videos" :videos="videos" :playlist-slug="main.channel?.mainPlaylist" />
<video-grid v-if="videos" :videos="videos" :playlist-slug="main.channel?.firstPlaylist" />
</div>
</template>
4 changes: 2 additions & 2 deletions zimui/src/components/channel/tabs/ChannelHomeListTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const isLoading = ref(true)
// Watch for changes in the main playlist
watch(
() => main.channel?.mainPlaylist,
() => main.channel?.id,
() => {
fetchData()
}
)
// Fetch the videos for the main playlist
const fetchData = async function () {
if (main.channel?.mainPlaylist) {
if (main.channel?.id) {
try {
const resp = await main.fetchHomePlaylists()
if (resp) {
Expand Down
6 changes: 1 addition & 5 deletions zimui/src/components/channel/tabs/GenericTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ onMounted(() => {
:count-text="playlist?.videos.length === 1 ? 'video' : 'videos'"
icon="mdi-video-outline"
/>
<video-grid
v-if="videos"
:videos="videos"
:playlist-slug="`main.channel?.${props.playlistSlug}`"
/>
<video-grid v-if="videos" :videos="videos" :playlist-slug="playlist?.slug" />
</div>
</template>
4 changes: 2 additions & 2 deletions zimui/src/components/channel/tabs/PlaylistsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const isLoading = ref(true)
// Watch for changes in the main playlist
watch(
() => main.channel?.mainPlaylist,
() => main.channel?.id,
() => {
fetchData()
}
)
// Fetch the playlists for the playlist tab
const fetchData = async function () {
if (main.channel?.mainPlaylist) {
if (main.channel?.id) {
try {
const resp = await main.fetchPlaylists()
if (resp) {
Expand Down
8 changes: 4 additions & 4 deletions zimui/src/types/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export interface Channel {
profilePath?: string
bannerPath?: string
joinedDate: string
mainPlaylist?: string
userLongUploadsPlaylist?:string
userShortUploadsPlaylist?:string
userLivesPlaylist?:string
firstPlaylist?: string
userLongUploadsPlaylist?: string
userShortUploadsPlaylist?: string
userLivesPlaylist?: string
playlistCount: number
}

Expand Down

0 comments on commit 90d391b

Please sign in to comment.