def summarize_charts_by_area(country_code): charts_file_path = f'{dir_path}/analyse-charts-{chart_type.value}-{freq_type.value}-{country_code}.tsv' print(f'{country_code}: start download') _util.download_charts(charts, chart_type, country_code, freq_type, begin_date=begin_date, end_date=end_date) print(f'{country_code}: start summarize') charts_map = _util.summarize_chart(charts, chart_type, country_code, freq_type, begin_date=begin_date, end_date=end_date) print(f"{country_code}: {len(charts_map.values())} tracks") charts_data = list(charts_map.values()) for i, d in enumerate(charts_data): database.update_track_data(d.get('URL')) d.update(_util.get_tsv_track_data(database, d)) # ジャンルごとのカテゴリ、国、エリアをつめる genres = d.get('Genres', '').split(' / ') categories = [] countries = [] area2_list = [] area1_list = [] for g in genres: genre = database.get_genre(g) if genre is None: continue category = genre.get('Category') country = genre.get('Country') area2 = genre.get('Area 2') area1 = genre.get('Area 1') if category is not None and category != '': categories.append(category) if country is not None and country != '': countries.append(country) if area2 is not None and area2 != '': area2_list.append(area2) if area1 is not None and area1 != '': area1_list.append(area1) categories = list(set(categories)) countries = list(set(countries)) area2_list = list(set(area2_list)) area1_list = list(set(area1_list)) d['Categories'] = ' / '.join(categories) d['Countries'] = ' / '.join(countries) d['Area2'] = ' / '.join(area2_list) d['Area1'] = ' / '.join(area1_list) _util.write_tsv(charts_file_path, charts_data) return charts_data
r.get('audio_features').get('key').get('median'), 'mode': r.get('audio_features').get('mode').get('median'), 'time_signature': r.get('audio_features').get('time_signature').get('median'), 'tempo': r.get('audio_features').get('tempo').get('median'), 'danceability': r.get('audio_features').get('danceability').get('median'), 'energy': r.get('audio_features').get('energy').get('median'), 'loudness': r.get('audio_features').get('loudness').get('median'), 'speechiness': r.get('audio_features').get('speechiness').get('median'), 'acousticness': r.get('audio_features').get('acousticness').get('median'), 'instrumentalness': r.get('audio_features').get('instrumentalness').get('median'), 'liveness': r.get('audio_features').get('liveness').get('median'), 'valence': r.get('audio_features').get('valence').get('median'), } country_count = r.get('country_count', {}) for c in master_country_list: result[c] = country_count.get(c, 0) tsv_results.append(result) _util.write_tsv(f'{dir_path}/result.tsv', tsv_results)
apple_music_album_list = _util.read_tsv(apple_music_album_list_path) save_album_list = _util.read_tsv(save_album_list_path) print(len(apple_music_album_list)) print(len(save_album_list)) apple_music_album_map = {} for a in apple_music_album_list: if 'status' not in a: a['status'] = 'unknown' title = a.get('title') artist = a.get('artist') key = f'{title} / {artist}' apple_music_album_map[key] = a for idx in range(0, len(save_album_list), 30): albums = save_album_list[idx:idx+30] album_ids = [x.get('id') for x in albums] result = spotify.current_user_saved_albums_add(albums=album_ids) print(result) for a in albums: title = a.get('title') artist = a.get('artist') key = f'{title} / {artist}' if key in apple_music_album_map: apple_music_album_map[key]['status'] = 'saved' time.sleep(1/30) _util.write_tsv(apple_music_album_list_path, list(apple_music_album_map.values())) _util.write_tsv(unknown_album_list_path, [x for x in list(apple_music_album_map.values()) if x.get('status') not in ['saved']])
'beat': 'dance', 'funk': 'dance', 'experimental': 'experimental', 'ambient': 'experimental', } for k, v in genre_map.items(): if k in genre: return v return '' for s in selector: genre = s.find_all('td', attrs={'class': 'note'})[1].text if genre not in genre_map: genre_map[genre] = { 'Genre': genre, 'Category': '', 'Country Name': '', 'Country': '', 'Area 2': '', 'Area 1': '' } genre_data = genre_map[genre] # genre_data['Category'] = get_genre_category(genre) category = genre_data.get('Category') if category == '': genre_data['Category'] = get_genre_category(genre) print(len(genre_map.values())) _util.write_tsv(genre_data_path, list(genre_map.values()))
charts = sc.Spotipycharts() # data/tsv フォルダを作る _util.makedirs('data/tsv') # チャート情報(csv)をダウンロードします。 print("start: download csv") _util.download_charts(charts, chart_type, country_code, freq_type) # 曲ごとの情報にまとめます。 print("start: summary spotifycharts") charts_map = _util.summarize_chart(charts, chart_type, country_code, freq_type) print(f"{len(charts_map.values())} tracks") # いったん、ファイル保存する _util.write_tsv(charts_file_path, list(charts_map.values())) # 曲ごとにSpotify APIのトラックデータを取得します。 print("start: get track data") charts_data = _util.read_tsv(charts_file_path) client_id = _const.CLIENT_ID client_secret = _const.CLIENT_SECRET client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials( client_id, client_secret) spotify = spotipy.Spotify( client_credentials_manager=client_credentials_manager) database = _util.SpotifyDatabase(spotify_client=spotify, track_dir_path=track_data_path, artist_dir_path=artist_data_path)
'Area 2': '', 'Area 1': '' } genre_data['Count'] = 1 genre_map[g] = genre_data # TODO 全てのtrack情報の取得 # TODO trackに紐づく、audio_featureの取得 output_album_list.append({ 'album_type': album.get('album_type'), 'name': album.get('name'), 'artist_names': ' / '.join([x.get('name') for x in artists]), 'label': album.get('label'), 'release_year': album.get('release_date', '')[:4], 'release_date': album.get('release_date'), 'release_date_precision': album.get('release_date_precision'), 'artist_ids': ' / '.join([x.get('id') for x in artists]), 'id': album.get('id'), 'total_tracks': album.get('total_tracks'), 'result_total_tracks': len(album.get('tracks', {}).get('items', [])), 'added_at': a.get('added_at'), 'genres': ' / '.join(genres), 'popularity': album.get('popularity'), }) _util.write_tsv('data/saved/my_albums.tsv', output_album_list) _util.write_tsv('data/saved/genre.tsv', list(genre_map.values())) # added_at, album_type, artist_names, artist_ids, genres, id, label, # name, popularity, release_date, release_date_precision, total_tracks, result_total_tracks # track_id -> audio_feature, track
charts = sc.Spotipycharts() # data/tsv フォルダを作る _util.makedirs('data/tsv') # チャート情報(csv)をダウンロードします。 print("start: download csv") _util.download_charts(charts, chart_type, country_code, freq_type) # 曲ごとの情報にまとめます。 print("start: summary spotifycharts") charts_map = _util.summarize_chart(charts, chart_type, country_code, freq_type) print(f"{len(charts_map.values())} tracks") # いったん、ファイル保存する _util.write_tsv(charts_file_path, list(charts_map.values())) # 曲ごとにSpotify APIのトラックデータを取得します。 print("start: get track data") charts_data = _util.read_tsv(charts_file_path) user_id = _const.USER_ID client_id = _const.CLIENT_ID client_secret = _const.CLIENT_SECRET client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials( client_id, client_secret) spotify = spotipy.Spotify( client_credentials_manager=client_credentials_manager) database = _util.SpotifyDatabase(spotify_client=spotify, track_dir_path=track_data_path,
break else: albums_by_unknown.append(album_item) else: unknown.append(album_item) if not is_found: if len(albums_by_unknown) > 0: unmatch_artist_list.extend(albums_by_unknown) album['status'] = 'unmatched' print('unmatched') else: unknown_album_list.extend(unknown) album['status'] = 'unknown' print('unknown') else: album['status'] = 'found' print('found') time.sleep(1 / 30) print(len(found_album_list)) print(len(unmatch_artist_list)) print(len(unknown_album_list)) if len(found_album_list): _util.write_tsv(found_albums_path, found_album_list) if len(unmatch_artist_list): _util.write_tsv(unmatched_artists_path, unmatch_artist_list) if len(unknown_album_list): _util.write_tsv(unknown_albums_path, unknown_album_list) _util.write_tsv(apple_music_albums_path, album_list)
import spotipycharts as sc import _util charts = sc.Spotipycharts() country_list = charts.get_country_list() print(f'country: {len(country_list)}') _util.write_tsv('data/country-data.tsv', country_list) track_data_path = 'data/track' artist_data_path = 'data/artist' genre_data_path = 'data/genre-data.tsv' db = _util.SpotifyDatabase(track_dir_path=track_data_path, artist_dir_path=artist_data_path, genre_file_path=genre_data_path) artist_map = db.get_artist_map() track_map = db.get_track_map() artist_list = _util.artist_map_to_tsv_data_list(artist_map) print(f'artist: {len(artist_list)}') _util.write_tsv('data/artist-data.tsv', artist_list) track_list = _util.track_map_to_tsv_data_list(track_map, artist_map) print(f'track: {len(track_list)}') _util.write_tsv('data/track-data.tsv', track_list) genre_map = _util.get_genre_map(db) print(f"genre: {len(genre_map.keys())}") _util.write_tsv(genre_data_path, list(genre_map.values()))