I need to make a series of YouTube playlists that categorises every NoCopyrightSounds release, including old and removed ones, by genre.
Luckily, this doesn't have to be manual. The fandom wiki for NoCopyrightSounds has a page for every confirmed song and categorises them by genre.
To accomplish my goal, I need multiple steps.
Step 1. Compile Info
I need a program that can scrape these pages:
https://nocopyrightsounds.fandom.com/wiki/Category:NoCopyrightSounds_songshttps://nocopyrightsounds.fandom.com/wiki/Category:NCS_Arcade_songshttps://nocopyrightsounds.fandom.com/wiki/List_of_NCS_SoundCloud_promotions_by_date
The SoundCloud promotions is messier because it is not a category page. It contains a table with the artists name first, the song name second, the release date third, and proof of the promotion last.
The artists name table contains an emoji that categorises the promotion further - we're looking to search the songs where the artist name has a 🟡 emoji next to it, which means "Deleted SoundCloud Exclusive release". Keep in mind that this emoji is used in the artists name box in the table, and so, to enter the song's page, you'd have to click the hyperlink on the second box, which would be the song name. SoundCloud releases don't have YouTube links, so I'll have to fill them in myself.
All of the pages should be scraped for all data in their infobox, with any absent values left blank. This is a list of all possible infobox entries with comments:
Name
Image (Cover Art)
Original Artist
Original Feature
Cover Artist
Cover Feature
Artist (Their names are put in [[brackets]] which need to be cleaned)
Remix
Mix Artist
Mix
Mashup Artist
Mashup
Featuring
Uncredited Artist(s)
Writer(s)
Album
Brand
Released (Date)
Removed (Date)
Converted (Date)
Genre (Usually bulletted to include "YT" genre and "Site" genre.)
Community Genre
Subgenre(s)
Mood(s)
Length
Catalog Number
BPM (May be bulleted with timestamps if multiple)
Key
Key2
Background
Background2
Amazon Music
Anghami
Apple Music
Beatport
Deezer
NCS
Linkfire
Pandora
Presave
SoundCloud
Spotify
Tidal
YouTube
YouTube Music
Step 2.
Another program should place all of these songs in their corresponding community genre and subgenre YouTube playlists by order of date using Selenium to avoid API limits (using a fresh, empty YT account). Some will be put in several playlists. If a song is labelled under an umbrella genre (like Drumstep) without any subgenre, it should be placed in its own playlist. The Umbrella playlist itself would be named "All Drumstep", including Melodic and Regular, while the Drumstep playlist only has the regular. This will apply to other genres and Drumstep is just an example. The playlist name prefix is "NCS - ".
Assistant
Step 3.
Opinions change and new songs release over time, so there needs to be a script that handles updating. Rescraping all of the fandom pages to note any community genre and subgenre changes and add in the new songs and their details (Do not update the YouTube link on SoundCloud releases - those will be located by me manually and replacing them would just reset them every time I ran the update script since they wouldn't have a YouTube link entry, or just have a link to a non-NCS music video). The script should then automatically move the songs that need be moved, placing them in the appropriate, release date accurate spot, also adding in the new releases.
Keep in mind the pagination method for fandom and that infoboxes can't be parsed through HTML (I believe).
Here's an entire song page for reference (with infobox and more). They all have this same structure. Avoid scraping links outside of the infobox, and note that genres can be separated with newlines and |. Subgenres are also linked in a tree process, starting with a marker that it's the subgenre, then the parent genre(s), and finishing at the subgenre. Example: {{Subgenre|House|Commercial House}}. "Verified" means that the wiki admins have confirmed the community genres as accurate and they are locked and don't need to be checked as often for changes or additions.
{{Infobox song
|Image = Back On Dash.jpg
|Artist = [[DJVI]]
|Remix =
|Featuring =
|Uncredited =
|Album =
|Brand = NoCopyrightSounds
|Released = November 8, 2024
|Genre = {{Subgenre|House|Progressive House}}
|Community Genre = {{Genre|House|verified}}
|Subgenre(s) = {{Subgenre|House|Commercial House}}
|Mood(s) = *{{Mood|Energetic}}
*{{Mood|Exciting}}
*{{Mood|Positive}}
|Length = 3:06
|Catalog Number = N/A
|BPM = 138
|Key = G minor
|Background = Back on Dash BG
|Amazon Music = B0DKLDYDXS
|Anghami = 1182859030
|Apple Music = 1775735684?i=1775735686
|Beatport =
|Deezer = track/3057282571
|NCS = backondash
|Pandora = djvi/back-on-dash/ALZ2rtrPPpthbv6
|SoundCloud = nocopyrightsounds/djvi-back-on-dash-geometry-dash
|Spotify = track/66i7MyCc1Tq4iGZxxHOCej
|Tidal = 395013787
|YouTube = VDbVXpJWA-k
|YouTube Music = RYdNsUZ9LyI
}}'''Back on Dash''' is a song by [[DJVI]] released on November 8, 2024. It was uploaded by NCS on YouTube on November 9, 2024.
==Trivia==
*This track is the third part of DJVI's original track, [https://www.youtube.com/watch?v=N9vDTYZpqXM Back On Track].
*The circle visualizer switches over to a square visualizer between 0:14 and 1:15 of the YouTube upload.
**This is the first upload to feature a rounded square visualizer.
{{DJVI}}
{{2024}}
[[Category:Commercial House music]]
To accomplish your goal of categorizing every NoCopyrightSounds (NCS) release by genre and organizing them into YouTube playlists, you can follow a structured approach. Below, I'll guide you through each step, providing insights and code snippets to help you implement the solution.
Step 1: Compile Info
Objective: Scrape the NCS fandom wiki pages to collect all song data, including old and removed songs, and compile this information into a structured dataset.
1.1. Scrape Category Pages
First, you need to scrape the following category pages:
Parse the table and extract rows where the artist name contains the 🟡 emoji (Deleted SoundCloud Exclusive release).
Extract the song names and their corresponding links.
Code Snippet:
python
def get_deleted_soundcloud_songs(page_url):
response = requests.get(page_url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table')
songs = []
for row in table.find_all('tr')[1:]: # Skip header row
columns = row.find_all('td')
if '🟡' in columns[0].text:
song_link = columns[1].find('a')
if song_link:
songs.append('https://nocopyrightsounds.fandom.com' + song_link.get('href'))
return songs
1.3. Extract Data from Infoboxes
The song pages have an infobox that contains all the required information. Since infobox data isn't always easily parsed from rendered HTML, you can use the Fandom API to get the raw wikitext of the page and parse it.
import mwparserfromhell
def parse_infobox(wikitext):
wikicode = mwparserfromhell.parse(wikitext)
templates = wikicode.filter_templates()
for template in templates:
if template.name.strip().lower() == 'infobox song':
infobox_data = {}
for param in template.params:
key = param.name.strip()
value = param.value.strip()
infobox_data[key] = value
return infobox_data
return {}
1.4. Clean and Structure the Data
After extracting the data, you need to clean it:
Remove wiki markup, such as [[brackets]] and templates.
Parse and extract information from templates within infobox parameters, such as {{Genre|House|verified}}.
Example Cleaning Functions:
python
import re
def clean_wiki_markup(text):
text = re.sub(r'\[\[|\]\]', '', text) # Remove brackets
text = re.sub(r'\{\{.*?\}\}', '', text) # Remove templates
return text.strip()
def parse_genre_field(genre_field):
genres = []
wikicode = mwparserfromhell.parse(genre_field)
templates = wikicode.filter_templates()
for template in templates:
if template.name.matches('Genre') or template.name.matches('Subgenre'):
genres.append(template.params[0].value.strip())
return genres
1.5. Compile All Data
Loop through all song links, extract infobox data, clean it, and compile it into a structured format like a list of dictionaries or a DataFrame.
Example:
python
import pandas as pd
def compile_song_data(song_links):
all_songs = []
for song_url in song_links:
page_title = song_url.split('/')[-1]
wikitext = get_page_wikitext(page_title)
infobox = parse_infobox(wikitext)
if infobox:
song_data = {}
for field in ['Name', 'Artist', 'Genre', 'Community Genre', 'Subgenre(s)', 'Released', 'YouTube']:
value = infobox.get(field, '')
if field in ['Genre', 'Community Genre', 'Subgenre(s)']:
genres = parse_genre_field(value)
song_data[field] = genres
else:
song_data[field] = clean_wiki_markup(value)
all_songs.append(song_data)
df = pd.DataFrame(all_songs)
return df
Step 2: Create YouTube Playlists
Objective: Use Selenium to automate the creation of YouTube playlists and add songs based on their genres.
2.1. Set Up Selenium
Install Selenium and the appropriate WebDriver (e.g., ChromeDriver for Google Chrome).
bash
pip install selenium
2.2. Log in to YouTube
Automate the login process using Selenium. Be cautious to handle two-step verification if enabled.
Note: Automating login may violate YouTube's terms of service. Proceed with caution and consider using a dedicated account.
2.3. Create Playlists
For each genre and subgenre, create a new playlist if it doesn't already exist.
Example Code to Create a Playlist:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
def create_playlist(driver, playlist_name):
driver.get('https://www.youtube.com/playlist?list=WL') # Navigate to "Watch Later" as a starting point
time.sleep(2)
driver.find_element(By.XPATH, '//yt-formatted-string[text()="New playlist"]').click()
time.sleep(2)
driver.find_element(By.ID, 'input-0').send_keys(playlist_name)
driver.find_element(By.XPATH, '//yt-formatted-string[text()="Create"]').click()
time.sleep(2)
2.4. Add Songs to Playlists
For each song, use Selenium to navigate to its YouTube URL and add it to the appropriate playlists.
Example Code to Add a Song to a Playlist:
python
def add_song_to_playlist(driver, song_url, playlist_name):
driver.get(song_url)
time.sleep(3)
# Click on the "Save" button
driver.find_element(By.XPATH, '//button[@aria-label="Save to playlist"]').click()
time.sleep(1)
# Select the playlist
playlist_elements = driver.find_elements(By.XPATH, '//yt-formatted-string[@id="label"]')
for element in playlist_elements:
if element.text == playlist_name:
element.click()
break
# Close the dropdown
driver.find_element(By.XPATH, '//yt-icon-button[@id="close-button"]').click()
time.sleep(1)
2.5. Handle Multiple Playlists and Order
Ensure that songs are added to all relevant playlists.
Since YouTube playlists don't guarantee order when adding via automation, you might need to reorder them manually or implement code to reorder songs if necessary.
2.6. Playlist Naming Convention
Use the naming convention NCS - [Genre/Subgenre]. For umbrella genres, create an "All [Genre]" playlist that includes all songs under that genre.
Step 3: Update Playlists
Objective: Develop a script that detects changes in the song data and updates the playlists accordingly.
3.1. Rescrape and Compare Data
Periodically rescrape the NCS wiki pages to get the latest data.
Compare the new dataset with the previous one to find:
New songs
Songs with updated genres
Removed songs
Example to Compare Datasets:
python
def compare_datasets(old_df, new_df):
old_songs = old_df.set_index('Name')
new_songs = new_df.set_index('Name')
added_songs = new_songs.index.difference(old_songs.index)
removed_songs = old_songs.index.difference(new_songs.index)
# Detect changes in genres
genre_changes = []
for song in old_songs.index.intersection(new_songs.index):
if old_songs.loc[song, 'Community Genre'] != new_songs.loc[song, 'Community Genre']:
genre_changes.append(song)
return added_songs, removed_songs, genre_changes
3.2. Update Playlists Using Selenium
For new songs: Add them to the appropriate playlists.
For removed songs: Optionally, remove them from playlists if they are no longer relevant.
For songs with genre changes:
Remove them from old playlists.
Add them to the new playlists.
3.3. Preserve Manual Edits
Since you will manually add YouTube links for SoundCloud releases, ensure that the script does not overwrite these links or remove them during the update process.
Implement checks in your script to skip updates for songs without YouTube links unless they have been added.
Additional Considerations
Handling MediaWiki Templates
The genres and subgenres are provided using templates like {{Subgenre|House|Progressive House}}.
Use mwparserfromhell to extract these nested templates.
Example:
python
def parse_subgenre(template_value):
wikicode = mwparserfromhell.parse(template_value)
templates = wikicode.filter_templates()
subgenres = []
for template in templates:
if template.name.matches('Subgenre'):
parent_genre = template.get(1).value.strip()
subgenre = template.get(2).value.strip()
subgenres.append((parent_genre, subgenre))
return subgenres
Pagination Handling in Selenium
When working with Selenium to order songs or handle long playlists, be aware that elements may not be immediately available in the DOM, and you might need to scroll or wait for elements to load.
Legal and Ethical Considerations
YouTube's Terms of Service: Automating interactions on YouTube using scripts may violate their terms. Ensure you review and comply with YouTube's policies.
Rate Limiting: Implement delays between actions to avoid being flagged for unusual activity.
Data Privacy: Be cautious with account credentials and personal data.
Testing and Debugging
Start by testing your scripts with a small subset of data.
Use logging to track actions performed by your script.
Handle exceptions gracefully to prevent the script from crashing.
Automation Limitations
Web automation is subject to breaking when websites update their layouts.
Regular maintenance may be required to keep the script functioning.
Summary
By:
Scraping the NCS fandom wiki to compile song data.
Cleaning and structuring the data for ease of use.
Automating YouTube interactions with Selenium to create playlists and add songs.
Updating the playlists by detecting changes in the data.
You can automate the organization of NCS releases into genre-specific playlists.
If you need further assistance with specific parts of the implementation or have questions about handling certain edge cases, feel free to ask!