-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (98 loc) · 4.97 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from bs4 import BeautifulSoup
import configparser
import logging
from tqdm import tqdm
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger()
# Function to load the details from the configuration file
def load_details():
config = configparser.ConfigParser()
config.read('config.ini')
html_file_path = config.get('Anghami', 'html_file_path')
client_id = config.get('Spotify', 'client_id')
client_secret = config.get('Spotify', 'client_secret')
redirect_url = config.get('Spotify', 'redirect_url')
username = config.get('Spotify', 'username')
spotify_playlist_name = config.get('Spotify', 'playlist_name')
save_to_text = config.getboolean('General', 'save_to_text')
txt_save_path = config.get('General', 'txt_save_path')
txt_song_artist_separator = config.get('General', 'txt_song_artist_separator')
return html_file_path, client_id, client_secret, redirect_url, username, spotify_playlist_name, save_to_text, txt_save_path, txt_song_artist_separator
def read_html_file(html_file_path):
with open(html_file_path, encoding="utf8") as f:
content = f.read()
return content
def extract_songs_and_artists(content):
soup = BeautifulSoup(content, 'html.parser')
class_lst = ["cell cell-title", "cell cell-title marquee"]
song_divs = soup.find_all("div", class_=class_lst)
artist_divs = soup.find_all("div", {"class": "cell cell-artist"})
songs = [div.find("span").text for div in song_divs]
artists = [div.text for div in artist_divs]
return songs, artists
def save_playlist_to_text(songs, artists, txt_save_path, txt_song_artist_separator):
with open(txt_save_path, 'w', encoding="utf8") as fp:
for song, artist in zip(songs, artists):
fp.write(f"{song}{txt_song_artist_separator}{artist}\n")
def authenticate_spotify(client_id, client_secret, redirect_url, username):
scope = 'playlist-modify-public'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_url,
scope=scope,
username=username))
return sp
def create_spotify_playlist(sp, username, spotify_playlist_name):
playlist = sp.user_playlist_create(user=username, name=spotify_playlist_name, public=True,
description="Imported from Anghami")
return playlist['id']
def search_and_add_tracks(sp, playlist_id, songs, artists, username):
not_found = []
for song, artist in tqdm(zip(songs, artists), total=len(songs), bar_format="{l_bar}{bar}{r_bar}", colour='green'):
try:
res = sp.search(q=f"{song} {artist}", type='track', limit=1)
if len(res['tracks']['items']) > 0:
uri = res['tracks']['items'][0]['uri']
sp.user_playlist_add_tracks(user=username, playlist_id=playlist_id, tracks=[uri])
else:
not_found.append(f"{song} {artist}")
except spotipy.SpotifyException as e:
logger.error(f"Spotify Error: {str(e)}")
not_found.append(f"{song} {artist}")
return not_found
def main():
# Load details from configuration file
html_file_path, client_id, client_secret, redirect_url, username, spotify_playlist_name, save_to_text, txt_save_path, txt_song_artist_separator = load_details()
# Read the HTML file
content = read_html_file(html_file_path)
# Extract songs and artists from the HTML
songs, artists = extract_songs_and_artists(content)
# Check if the number of songs matches the number of artists
if len(songs) != len(artists):
logger.error("Error: Number of songs and artists do not match.")
return
# Print the playlist details
logger.info("\nPlaylist Details:")
for song, artist in zip(songs, artists):
logger.info(f"{song} {txt_song_artist_separator} {artist}")
# Save the playlist to a text file
if save_to_text:
save_playlist_to_text(songs, artists, txt_save_path, txt_song_artist_separator)
logger.info("Playlist saved to text file.")
# Authenticate and create a new playlist on Spotify
sp = authenticate_spotify(client_id, client_secret, redirect_url, username)
playlist_id = create_spotify_playlist(sp, username, spotify_playlist_name)
# Search and add tracks to the Spotify playlist
logger.info("Importing playlist to Spotify...")
not_found = search_and_add_tracks(sp, playlist_id, songs, artists, username)
logger.info("Playlist import completed.")
# Print the not found songs
if len(not_found) > 0:
logger.info("\nThe following songs could not be found on Spotify:")
for song in not_found:
logger.info(song)
if __name__ == '__main__':
main()