# Notebook from mrtommyb/spotty
Path: notebooks/tom-version.ipynb

<code>
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import pandas as pd
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import matplotlib.animation as animation
from scipy import stats
from datetime import date

import warnings
warnings.filterwarnings("ignore")

%matplotlib inline_____no_output_____k = 0.0172020989 # Gaussian Grav Constant
grav = k**2 # work in units of AU, DAYS and solar masses
rsun_to_AU = 0.00465047
gm = 2.959122083e-4
rEarth = 6370.# km
rEarth_to_rSun = 0.0091577

mstar = 0.359#MSun
Rstar = 0.4445
Rplanet = 2.6*rEarth_to_rSun
#Rp_Rs = np.sqrt(np.pi/4)*0.054
Rp_Rs = 0.054
tdepth_ = Rp_Rs**2
Pplanet = 32.93963#days
b = 0.#0.774
Prot = 38.6*24.#hours

aplanet = (Pplanet**(2./3.))*((grav*mstar)**(1./3.))/((2.*np.pi)**(2./3.))

tdur_days = (Pplanet/np.pi)*np.arcsin(np.sqrt((Rstar**2 + Rplanet**2) + b**2)/(aplanet/rsun_to_AU))
tdur_hrs = tdur_days*24.
tdur_hrs = 2.7 # Kruse et al. 2019

print('tdur [calc] =', "%0.2f" % tdur_hrs, 'hours')
print('tdur [Kruse 2019] = 2.7 hours')
print('Prot(star) = ', Prot, '[hours]')

#star rotation of 1 degree in (1./360.)*Prot hours
Prot_1hr = (1./360.)*Prot
print('1 degree stellar rotation =', Prot_1hr, 'hours')
print('1 degree stellar rotation =', Prot_1hr/tdur_hrs, 'transit duration')
#
HST_orbit_ = 5./6.#0.001*Pplanet*24# In hours
HST_orbits_per_degree_stellar_rotation = Prot_1hr/HST_orbit_
Degrees_stellar_rotation_per_HST_orbit_ = 1./HST_orbits_per_degree_stellar_rotation
print()
print('1 HST orbit =', HST_orbit_, 'hours (FIXED!!)')
print('1 transit duration =', tdur_hrs/HST_orbit_, 'HST orbits')
print('1 degree stellar rotation =', HST_orbits_per_degree_stellar_rotation, 'HST orbits')
print('1 HST orbit =', Degrees_stellar_rotation_per_HST_orbit_, 'degrees stellar rotation')
#print('6 HST orbit = ', 6*obs_duration_, 'hours')
star_size = 750 # pixels, FIXED!
print('1 HST orbit =', star_size/(tdur_hrs/HST_orbit_), 'pixels traveled across star')
pixels_per_HST_visit_round_of_10 = int((star_size/(tdur_hrs/HST_orbit_) // 10)*10)
print('1 HST orbit =', pixels_per_HST_visit_round_of_10, 'pixels traveled across star')tdur [calc] = 2.70 hours
tdur [Kruse 2019] = 2.7 hours
Prot(star) =  926.4000000000001 [hours]
1 degree stellar rotation = 2.5733333333333337 hours
1 degree stellar rotation = 0.9530864197530865 transit duration

1 HST orbit = 0.8333333333333334 hours (FIXED!!)
1 transit duration = 3.24 HST orbits
1 degree stellar rotation = 3.088 HST orbits
1 HST orbit = 0.3238341968911917 degrees stellar rotation
1 HST orbit = 231.48148148148147 pixels traveled across star
1 HST orbit = 230 pixels traveled across star
amplitude_mag = 10e-3# magnitude
amplitude_flux = 10**(-0.4*amplitude_mag)

amplitude_flux_____no_output_____Teff_ = 3500
Tspots = 3000
#
UV_min, UV_max, IR_min, IR_max = 0.42, 0.88, 0.9, 1.8 # in micron
all_min, all_max = 0.3, 2
spectral_resolution = 500
#
area_covered, number_of_spots, aspect_ratio = 0.05, 20, 1.
#
number_of_rotation_phases = 1440#360
#
HST_filter = [1.12,1.63]
#HST_filter_=np.array([1.135,1.165,1.195,1.225,1.255,1.285,1.315,1.345,1.375,1.405,1.435,1.465,1.495,\
#                    1.525,1.555,1.585,1.615])
IR_min, IR_max = min(HST_filter), max(HST_filter)_____no_output_____fn_model_Teff = f"T{Teff_}_g5.0_solar.txt"
fn_model_spots = f"T{Tspots}_g5.0_solar.txt"
#
stellar_spec_Teff = pd.read_csv(fn_model_Teff, header = 7, sep = '\t')
stellar_spec_Teff = stellar_spec_Teff.values
wavelength_model_Teff = stellar_spec_Teff[:,0]
stellar_flux_model_Teff = stellar_spec_Teff[:,1]
#
stellar_spec_spots = pd.read_csv(fn_model_spots, header = 7, sep = '\t')
stellar_spec_spots = stellar_spec_spots.values
wavelength_model_spots = stellar_spec_spots[:,0]
stellar_flux_model_spots = stellar_spec_spots[:,1]_____no_output_____interp_model_spectra_ = 'n'

if interp_model_spectra_ == 'y':
    print('Model Spectra Interpolated to Spectral Resolution of ' + str(spectral_resolution))
    filter_UV = np.linspace(UV_min, UV_max, spectral_resolution)
    filter_IR = np.linspace(IR_min, IR_max, spectral_resolution)
#
    stellar_flux_model_interp_Teff_IR = np.interp(filter_IR, wavelength_model_Teff/1e4, stellar_flux_model_Teff)
    stellar_flux_model_interp_spots_IR = np.interp(filter_IR, wavelength_model_spots/1e4, stellar_flux_model_spots)
#
    stellar_flux_model_interp_Teff_UV = np.interp(filter_UV, wavelength_model_Teff/1e4, stellar_flux_model_Teff)
    stellar_flux_model_interp_spots_UV = np.interp(filter_UV, wavelength_model_spots/1e4, stellar_flux_model_spots)
else:
    idx_UV_, = np.where((wavelength_model_Teff/1e4 >= UV_min) & (wavelength_model_Teff/1e4 <= UV_max))
    idx_IR_, = np.where((wavelength_model_Teff/1e4 >= IR_min) & (wavelength_model_Teff/1e4 <= IR_max))
#
    filter_UV = wavelength_model_Teff[idx_UV_]/1e4
    filter_IR = wavelength_model_Teff[idx_IR_]/1e4
#
    stellar_flux_model_interp_Teff_IR = stellar_flux_model_Teff[idx_IR_]
    stellar_flux_model_interp_spots_IR = stellar_flux_model_spots[idx_IR_]
#
    stellar_flux_model_interp_Teff_UV = stellar_flux_model_Teff[idx_UV_]
    stellar_flux_model_interp_spots_UV = stellar_flux_model_spots[idx_UV_]_____no_output_____#Sep 2019 Benneke submission: depths_Benneke, depth_err_ = \
#np.array([2937, 2886, 2874, 2912, 2913, 2897, 2901, 2970, 2955, \
#2952, 2978, 2914,2929, 2893, 2862, 2897, 2884]), 30.+np.zeros((17))

# Dec 2019 Benneke accepted:
depths_Benneke, depth_err_ = np.array([2916, 2900, 2873, 2910, 2909, 2898, 2897, 2970, 2955, 2953, 2971, 2907, \
                                       2934, 2891, 2869, 2896, 2878]), 25.+np.zeros((17))

depths_Tsiaras, depth_err_ = np.array([2905, 2939, 2903, 2922, 2891, 2897, 2919, 2965, 2955, 2976, 2990, 2895, \
                                       2930, 2921, 2875, 2927, 2925]), 30.+np.zeros((17))
idx_HST, = np.where((filter_IR >= min(HST_filter)) & (filter_IR <= max(HST_filter)))

HST_depths_ = depths_Benneke
#HST_depths_ = depths_Tsiaras

normalized_HST_depths_ = HST_depths_# - np.nanmean(HST_depths_)_____no_output_____fig, ax = plt.subplots(1,1, figsize=(8,5))
maxval = np.max(stellar_flux_model_interp_Teff_UV)
plt.plot(filter_UV, stellar_flux_model_interp_Teff_UV/maxval, '#ffb000', lw=0.5, label='photosphere')
plt.plot(filter_UV, stellar_flux_model_interp_spots_UV/maxval, '#fb6d04', lw=0.5, label='spot')
plt.plot(filter_IR, stellar_flux_model_interp_Teff_IR/maxval, '#ffb000', lw=0.5)
plt.plot(filter_IR, stellar_flux_model_interp_spots_IR/maxval, '#fb6d04', lw=0.5)
plt.xlabel('Wavelength [$\mu$m]', fontsize = 16)
plt.ylabel('Relative flux density', fontsize = 16)
#plt.plot(filter_IR, stellar_flux_model_interp_Teff_IR - stellar_flux_model_interp_spots_IR)
# plt.xlim(HST_filter[0], HST_filter[1])
#plt.ylim(0.,2e3)
# fig.gca().get_yaxis().set_visible(False)
# fig.gca().get_yaxis().set_ticks([])

plt.scatter(np.average(
    filter_UV, weights=stellar_flux_model_interp_Teff_UV),
            np.mean(stellar_flux_model_interp_Teff_UV) / maxval, color='#ffb000', s=200,
            edgecolors='k', zorder=100)

plt.scatter(np.average(
    filter_UV, weights=stellar_flux_model_interp_spots_UV),
            np.mean(stellar_flux_model_interp_spots_UV) / maxval, color='#fb6d04', s=200,
            edgecolors='k', zorder=100)

plt.scatter(np.average(
    filter_IR, weights=stellar_flux_model_interp_Teff_IR),
            np.mean(stellar_flux_model_interp_Teff_IR) / maxval, color='#ffb000', s=200,
            edgecolors='k', zorder=100)

plt.scatter(np.average(
    filter_IR, weights=stellar_flux_model_interp_spots_IR),
            np.mean(stellar_flux_model_interp_spots_IR) / maxval, color='#fb6d04', s=200,
            edgecolors='k', zorder=100)

plt.legend()_____no_output_____#### MAKE FIGURES ####

def fig_rotated_star_map_(phase_,\
                          star_cube_orbit_1,star_cube_orbit_2,star_cube_orbit_3,star_cube_orbit_4,\
                          nn,ii,observations_per_orbit_,save_fig):

    for kk in range(observations_per_orbit_):
        fig = plt.figure(figsize=(20,10))
        grid = plt.GridSpec(observations_per_orbit_, 4, wspace=0., hspace=0.)

        plt.subplot(grid[kk,0])
        plt.imshow(star_cube_orbit_1[:,:,kk], cmap='hot', vmin=-1., vmax=1.)
        plt.axis('off')
        if kk == 0:
            plt.title('Visit ' + str(ii+1) + '; Orbit 1')

        plt.subplot(grid[kk,1])
        plt.imshow(star_cube_orbit_2[:,:,kk], cmap='hot', vmin=-1., vmax=1.)
        plt.axis('off')
        if kk == 0:
            plt.title('Visit ' + str(ii+1) + '; Orbit 2')

        plt.subplot(grid[kk,2])
        plt.imshow(star_cube_orbit_3[:,:,kk], cmap='hot', vmin=-1., vmax=1.)
        plt.axis('off')
        if kk == 0:
            plt.title('Visit ' + str(ii+1) + '; Orbit 3')

        plt.subplot(grid[kk,3])
        plt.imshow(star_cube_orbit_4[:,:,kk], cmap='hot', vmin=-1., vmax=1.)
        plt.axis('off')
        if kk == 0:
            plt.title('Visit ' + str(ii+1) + '; Orbit 4')

    return fig

def fig_full_lc_(phase_full_rotation, lc_full, phase_, lc_transit, nn, ii, save_fig):
    fig = plt.figure(figsize=(20,3))
    plt.plot(phase_full_rotation/360., lc_full, 'ko-')#, ms = 12)
    plt.plot(phase_/360., lc_transit, 'r-')#, ms = 10)
#                plt.xlim(-0.001, 0.004)
#                plt.ylim(0.995, 1.005)
    plt.title('Norm. Lightcurve, Visit ' + str(ii+1))
    plt.xlabel('Phase')
#            else:

    left, bottom, width, height = [0.65, 0.65, 0.15, 0.2]
    ax2 = fig.add_axes([left, bottom, width, height])
#                ax2.plot(phase_full_rotation/360., lc_full, 'k.')
    ax2.plot(phase_/360., lc_transit, 'ro-')#, ms = 10)
    plt.xlim(0.995*min(phase_/360.), 1.005*max(phase_/360.))
    plt.ylim(0.995*min(lc_transit), 1.005*max(lc_transit))

    return fig

def fig_plot_individual_visits(number_of_visits_, filter_IR_HST, mean_subtracted_diff_flux_arr_, \
                              bin_centers_, mean_subtracted_binned_spec_per_visit, nn, save_fig):
    fig = plt.figure(figsize=(20,15))

    for jj in range(number_of_visits_):

        plt.subplot(number_of_visits_, 1, jj+1)
        plt.plot(filter_IR[idx_HST], mean_subtracted_diff_flux_arr_[jj,:], 'k', alpha = 0.25)
        plt.plot(bin_centers_, mean_subtracted_binned_spec_per_visit[jj,:],'ro-',ms=10)
        plt.title('Diff spectrum, Visit ' + str(jj+1))

    return fig

def fig_plot_averaged_spec_all_visits_(bin_centers_, averaged_spec_all_visits_binned_, normalized_HST_depths_,\
                                       nn, save_fig):
    fig = plt.figure(figsize=(20,3))
    plt.plot(bin_centers_, averaged_spec_all_visits_binned_, 'rs-', ms = 7, lw = 15, alpha = 0.5)
    plt.errorbar(bin_centers_, normalized_HST_depths_, yerr = 1.*depth_err_, fmt = 'bs-', ms = 7, \
                    label = 'K2-18 HST', lw = 15, alpha = 0.5)

    plt.title('Averaged Difference Spectrum, (OOT-ITR)/ITR)')
#        plt.show()

    return fig_____no_output_____def spot_sizes(area_covered, ndc, aspect_ratio):

    Atot = np.round(4.*np.pi*rEarth**2)
    rad = np.random.random(ndc)
    ntot = np.sum((np.pi*(rad**2))/aspect_ratio)
    normalization = Atot*area_covered/ntot

    rad_real = np.around(rad*(normalization**0.5))

    return rad_real_____no_output_____def create_photosphere_map(spot_sizes_, faculae_size, lon, lat, photosphere_id, spots_id):

    fig = plt.figure(figsize=[5.,2.5], dpi = 72)# This makes a 360x180 map in Jupyter. Brute force, but it works...
    proj = ccrs.PlateCarree()
    ax = plt.axes(projection = proj, fc = 'r')

    canvas = FigureCanvas(fig)
    plt.gca().set_position([0, 0, 1, 1])

    ax.set_global()
    ax.outline_patch.set_linewidth(0.)
    ax.set_extent([-180, 180, -90, 90])

    for ii in range(len(spot_sizes_)):

#        add_faculae=ax.tissot(rad_km=faculae_size[ii], lons=lon[ii], lats=lat[ii], n_samples=500, fc='b', alpha=0.5)
        add_spots = ax.tissot(rad_km=spot_sizes_[ii], lons=lon[ii], lats=lat[ii], n_samples=500, fc='r', alpha=0.1)
#        XXXXXX
#        THIS IS WRONG!!!!

    canvas.draw()       # draw the canvas, cache the renderer
    s, (width, height) = canvas.print_to_buffer()
#    img_ = np.fromstring(s, np.uint8).reshape((height, width, 4))
    img_ = np.frombuffer(s, np.uint8).reshape((height, width, 4))

#    photosphere_id = 0.25
#    spots_id = 0.04
    photosphere = 1. + np.zeros((img_.shape[0],img_.shape[1]))
    photosphere[img_[:,:,3] != 0] = photosphere_id#0.05
    photosphere[img_[:,:,2] != 255] = spots_id#0.45

    plt.close()
#    plt.show()
    return photosphere_____no_output_____def rotate_star(star_map, lon, do_plot_, add_planet_, photosphere_id, spots_id):

    n_rot_phases = len(lon)
#    lon = np.linspace(0.,360, n_rot_phases)

    proj = ccrs.Orthographic(central_longitude=0., central_latitude=0.)

    tmp_arr = np.zeros((750,750,n_rot_phases))

    for ii in range(n_rot_phases):
        if ii != 180.:
            lon_min_, lon_max_ = -180+lon[ii], 180+lon[ii]
        else:
            lon_min_, lon_max_ = -180+lon[ii]+1e-1, 180+lon[ii]+1e-1

        plt.figure(figsize=(5,5))
        ax = plt.axes(projection = proj, fc = 'r')
        ax.outline_patch.set_linewidth(0.)

        aa = ax.imshow(star_map, origin='upper', transform=ccrs.PlateCarree(), extent=[lon_min_, lon_max_, -90, 90], \
                    cmap='hot', vmin=-1., vmax=1.)
        img_ = aa.get_array()
        photosphere = 1. + np.zeros((img_.shape[0],img_.shape[1]))
#        photosphere_id = 0.25
#        spots_id = 0.04
        photosphere[img_ == photosphere_id] = photosphere_id
        photosphere[img_ == spots_id] = spots_id

        tmp_arr[:,:,ii] = photosphere

#        rr_ = (Rp_Rs*star_cube.shape[0])
#        center_ = int(0.5*star_cube.shape[0])
#        print(rr_, center_)
#        star_cube[center_ - rr_:center_ + rr_, 300:360, ii] = planet_id

        if do_plot_ == 'y':
            plt.show()
        else:
            plt.close()

    return tmp_arr_____no_output_____def fraction_covered_(star_cube, number_of_rotation_phases, photosphere_id, spots_id, planet_id):

    photosphere = photosphere_id
    spots = spots_id
    planet = planet_id
    frac_photosphere = np.zeros(number_of_rotation_phases)
    frac_spots = np.zeros(number_of_rotation_phases)
    frac_planet = np.zeros(number_of_rotation_phases)

    for ii in range(number_of_rotation_phases):
        area_visible_disk = star_cube[:,:,ii].copy()
        area_visible_disk = star_cube[:,:,ii][star_cube[:,:,ii] != 1.].size

        frac_photosphere[ii] = float(star_cube[:,:,ii][star_cube[:,:,ii] == photosphere].size)
        frac_photosphere[ii] = frac_photosphere[ii]/area_visible_disk

        frac_spots[ii] = float(star_cube[:,:,ii][star_cube[:,:,ii] == spots].size)
        frac_spots[ii] = frac_spots[ii]/area_visible_disk

        frac_planet[ii] = float(star_cube[:,:,ii][star_cube[:,:,ii] == planet].size)
        frac_planet[ii] = frac_planet[ii]/area_visible_disk

#    print(frac_photosphere)
#    print(frac_spots)
#    print(frac_planet)
#    print(frac_photosphere + frac_spots + frac_planet)

    return frac_photosphere, frac_spots, frac_planet_____no_output_____def make_flux_cube(star_cube, number_of_rotation_phases, photosphere_id, spots_id, planet_id):
    frac_photosphere, frac_spots, frac_planet = \
    fraction_covered_(star_cube, number_of_rotation_phases, photosphere_id, spots_id, planet_id)
    frac_covered = np.vstack((frac_photosphere, frac_spots)).T

#    print(frac_photosphere)
#    print(frac_spots)
#    print(frac_planet)
#    print()

#    lc_IR = frac_covered[:,0]*np.sum(stellar_flux_model_interp_Teff_IR) + \
#    frac_covered[:,1]*np.sum(stellar_flux_model_interp_spots_IR)
#    phase_max_ = lc_IR.argmax()
#    phase_min_ = lc_IR.argmin()
#
    flux_cube = np.zeros((number_of_rotation_phases, len(stellar_flux_model_interp_Teff_IR)))

    for ii in range(number_of_rotation_phases):
        flux_cube[ii,:] = frac_covered[ii,0]*stellar_flux_model_interp_Teff_IR + \
        frac_covered[ii,1]*stellar_flux_model_interp_spots_IR
#
    rot_phase_ = np.linspace(0.,360, number_of_rotation_phases)/360.

    return flux_cube _____no_output_____def make_diff_flux_(ii, tdepth_,flux_cube_orbit_1, flux_cube_orbit_2, flux_cube_orbit_3, flux_cube_orbit_4, idx_HST):

    if (ii+1 != 4) & (ii+1 != 8):
        itr_flux_ = (1.-0.*tdepth_)*np.mean(np.vstack((flux_cube_orbit_2,flux_cube_orbit_3)), axis = 0)
        oot_flux_ = np.mean(np.vstack((flux_cube_orbit_1,flux_cube_orbit_4)), axis = 0)
    if ii+1 == 4:
        itr_flux_ = (1.-0.*tdepth_)*np.mean(flux_cube_orbit_3, axis = 0)
        oot_flux_ = np.mean(np.vstack((flux_cube_orbit_1,flux_cube_orbit_3,flux_cube_orbit_4)), axis = 0)
    if ii+1 == 8:
        itr_flux_ = (1.-0.*tdepth_)*np.mean(np.vstack((flux_cube_orbit_3,flux_cube_orbit_4)), axis = 0)
        oot_flux_ = np.mean(np.vstack((flux_cube_orbit_1,flux_cube_orbit_2)), axis = 0)
#
    diff_flux_ = 1e6*(oot_flux_[idx_HST] - itr_flux_[idx_HST])/oot_flux_[idx_HST]
#    diff_flux_arr_[ii,:] = 1e6*(oot_flux_[idx_HST] - itr_flux_[idx_HST])/itr_flux_[idx_HST]

    return diff_flux______no_output_____def add_two_planet_transits_(ii, star_cube_t1, star_cube_t2, observations_per_orbit_):

    area_visible_disk = star_cube_t1[:,:,0][star_cube_t1[:,:,0] != 1.].size
#    rr_ = int(Rp_Rs*0.5*star_cube.shape[0])
    rr_ = int(0.5*np.ceil(np.sqrt(area_visible_disk*Rp_Rs**2)))
    center_ = int(0.5*star_cube.shape[0])
    pixels_per_obs_ = int(pixels_per_HST_visit_round_of_10/(observations_per_orbit_-1))
#    pixels_per_obs_ = 50 # FIXED
#
    if ii+1 != 8:
        for oo in range(observations_per_orbit_):
#            obs_2_indiv_ = center_-125 + (oo-5)*pixels_per_obs_
#            obs_3_indiv_ = center_+125 + (oo-5)*pixels_per_obs_
            obs_2_indiv_ = center_-25 + (oo-observations_per_orbit_)*pixels_per_obs_
            obs_3_indiv_ = center_+(-25+425) + (oo-observations_per_orbit_)*pixels_per_obs_
#            print(pixels_per_obs_, obs_2_indiv_, obs_3_indiv_)

            star_cube_t1[center_-rr_:center_+rr_, obs_2_indiv_-rr_:obs_2_indiv_+rr_, oo] = planet_id
            star_cube_t2[center_-rr_:center_+rr_, obs_3_indiv_-rr_:obs_3_indiv_+rr_, oo] = planet_id
#            print(rr_)
#            print(star_cube_t1.shape)
#            area_visible_disk = star_cube_t1[:,:,oo][star_cube_t1[:,:,oo] != 1.].size
#            area_planet = star_cube_t1[:,:,oo][star_cube_t1[:,:,oo] == planet_id].size
#            print(area_visible_disk, area_planet, area_planet/area_visible_disk)
#            print(star_cube_t1[center_-rr_:center_+rr_, obs_2_indiv_-rr_:obs_2_indiv_+rr_, oo].shape)
#        xxx
    else:
        for oo in range(observations_per_orbit_):
#            obs_3_indiv_ = center_-125 + (oo-5)*pixels_per_obs_
#            obs_4_indiv_ = center_+125  + (oo-5)*pixels_per_obs_
            obs_3_indiv_ = center_-25 + (oo-observations_per_orbit_)*pixels_per_obs_
            obs_4_indiv_ = center_+(-25+425) + (oo-observations_per_orbit_)*pixels_per_obs_

            star_cube_t1[center_-rr_:center_+rr_, obs_3_indiv_-rr_:obs_3_indiv_+rr_, oo] = planet_id
            star_cube_t2[center_-rr_:center_+rr_, obs_4_indiv_-rr_:obs_4_indiv_+rr_, oo] = planet_id

#   for oo in range(observations_per_orbit_):
#       fig = plt.figure(figsize=(10,5))
#       grid = plt.GridSpec(observations_per_orbit_, 2, wspace=0.3, hspace=0.3)
#       plt.subplot(grid[oo,0])
#       plt.imshow(star_cube_orbit_2[:,:,oo])

#       plt.subplot(grid[oo,1])
#       plt.imshow(star_cube_orbit_3[:,:,oo])
#       plt.show()

    return star_cube_t1, star_cube_t2_____no_output_____def add_one_planet_transits_(ii, star_cube, observations_per_orbit_):

    area_visible_disk = star_cube[:,:,0][star_cube[:,:,0] != 1.].size
#    rr_ = int(Rp_Rs*0.5*star_cube.shape[0])
    rr_ = int(0.5*np.ceil(np.sqrt(area_visible_disk*Rp_Rs**2)))
    center_ = int(0.5*star_cube.shape[0])
    pixels_per_obs_ = int(pixels_per_HST_visit_round_of_10/(observations_per_orbit_-1))
#    pixels_per_obs_ = 50 # FIXED
#
    for oo in range(observations_per_orbit_):
        obs_3_indiv_ = center_ + (oo-observations_per_orbit_)*pixels_per_obs_
        star_cube[center_-rr_:center_+rr_, obs_3_indiv_-rr_:obs_3_indiv_+rr_, oo] = planet_id

    return star_cube_____no_output_____def many_visits_random_photosphere(number_of_spots, area_covered, add_planet, number_of_visits_, nn, show_full_lc_, \
                                  plot_individual_visits_, show_rotated_star_map_, observations_per_orbit_):

#    number_of_visits_ = 8
    diff_flux_arr_ = np.zeros((number_of_visits_, len(idx_HST)))
    mean_subtracted_diff_flux_arr_ = np.zeros((number_of_visits_, len(idx_HST)))
    mean_subtracted_binned_spec_per_visit = np.zeros((number_of_visits_, 17))
    per_bin_errors_ = np.random.uniform(-30.,30, size = (number_of_visits_,17))

    star_map_all_visits_ = np.zeros((180, 360, number_of_visits_))

    photosphere_id = 0.25
    spots_id = 0.04
    planet_id = -1.

    phase_transit_ = np.zeros(number_of_visits_)

    for ii in range(number_of_visits_):
#
        spot_lat_ = -60. + 120*np.random.random(number_of_spots)
        spot_lon_ = -180 + 360.*np.random.random(number_of_spots)
        spot_sizes_ = spot_sizes(area_covered, number_of_spots, aspect_ratio)
        faculae_size = 1.5*spot_sizes_
        star_map = create_photosphere_map(spot_sizes_, faculae_size, spot_lon_, spot_lat_, photosphere_id, spots_id)
        star_map_all_visits_[:,:,ii] = star_map
#
        phase_ = 2.*Degrees_stellar_rotation_per_HST_orbit_*np.linspace(0,3,4) + np.random.uniform(357.5)
        number_of_rotation_phases = len(phase_)
#        print(ii, phase_)

        phase_transit_[ii] = phase_[0]

        pp_tmp_ = Degrees_stellar_rotation_per_HST_orbit_/observations_per_orbit_
        phases_per_orbit_ = np.zeros((len(phase_),observations_per_orbit_))
        for pp_ in range(len(phase_)):
            phases_per_orbit_[pp_,:] = phase_[pp_] + (pp_tmp_)*np.linspace(-observations_per_orbit_/2,\
                                                                           observations_per_orbit_/2,\
                                                                           observations_per_orbit_)
#
        do_plot_ = 'n'
        star_cube_orbit_1 = rotate_star(star_map,phases_per_orbit_[0,:],do_plot_,add_planet,photosphere_id,spots_id)
        star_cube_orbit_2 = rotate_star(star_map,phases_per_orbit_[1,:],do_plot_,add_planet,photosphere_id,spots_id)
        star_cube_orbit_3 = rotate_star(star_map,phases_per_orbit_[2,:],do_plot_,add_planet,photosphere_id,spots_id)
        star_cube_orbit_4 = rotate_star(star_map,phases_per_orbit_[3,:],do_plot_,add_planet,photosphere_id,spots_id)

        if (ii+1 != 4) & (ii+1 != 8):
            star_cube_orbit_2, star_cube_orbit_3 = \
            add_two_planet_transits_(ii, star_cube_orbit_2, star_cube_orbit_3, observations_per_orbit_)
        if ii+1 == 4:
            star_cube_orbit_3 = \
            add_one_planet_transits_(ii, star_cube_orbit_3, observations_per_orbit_)
        if ii+1 == 8:
            star_cube_orbit_3, star_cube_orbit_4 = \
            add_two_planet_transits_(ii, star_cube_orbit_3, star_cube_orbit_4, observations_per_orbit_)
#
#
        pp_ = observations_per_orbit_
        flux_cube_orbit_1 = make_flux_cube(star_cube_orbit_1, pp_, photosphere_id, spots_id, planet_id)
        flux_cube_orbit_2 = make_flux_cube(star_cube_orbit_2, pp_, photosphere_id, spots_id, planet_id)
        flux_cube_orbit_3 = make_flux_cube(star_cube_orbit_3, pp_, photosphere_id, spots_id, planet_id)
        flux_cube_orbit_4 = make_flux_cube(star_cube_orbit_4, pp_, photosphere_id, spots_id, planet_id)

        diff_flux_arr_[ii,:] = \
        make_diff_flux_(ii, tdepth_,flux_cube_orbit_1, flux_cube_orbit_2, flux_cube_orbit_3, flux_cube_orbit_4, \
                        idx_HST)

        mean_subtracted_diff_flux_arr_[ii,:] = diff_flux_arr_[ii,:] - np.nanmean(diff_flux_arr_[ii,:])
#
        bin_means,bin_edges,binnumber = \
        stats.binned_statistic(filter_IR[idx_HST],mean_subtracted_diff_flux_arr_[ii,:],statistic='mean', bins=17)

        bin_width = (bin_edges[1] - bin_edges[0])
        bin_centers_ = bin_edges[1:] - bin_width/2

        mean_subtracted_binned_spec_per_visit[ii,:] =  bin_means + 0.*per_bin_errors_[ii,:]
#
        if show_rotated_star_map_ == 'y':
            save_fig = 'n'
            aa = fig_rotated_star_map_(phase_, \
                                       star_cube_orbit_1, star_cube_orbit_2, star_cube_orbit_3, star_cube_orbit_4, \
                                       nn, ii, observations_per_orbit_, save_fig)
#
        if show_full_lc_ == 'y':
            save_fig = 'n'
            lc_transit_ = np.hstack((np.sum(flux_cube_orbit_1, axis = 1),
                                    np.sum(flux_cube_orbit_2, axis = 1),
                                    np.sum(flux_cube_orbit_3, axis = 1),
                                    np.sum(flux_cube_orbit_4, axis = 1)))

            phase_full_rotation = np.linspace(0.,359., 20)
            do_plot_
            star_cube_full_rotation = \
            rotate_star(star_map, phase_full_rotation, do_plot_, add_planet_, photosphere_id, spots_id)
            flux_cube_full_rotation = \
            make_flux_cube(star_cube_full_rotation, len(phase_full_rotation), photosphere_id, spots_id, planet_id)

            lc_full = np.zeros((flux_cube_full_rotation.shape[0]))
            for kk in range(len(lc_full)):
                lc_full[kk] = np.sum(flux_cube_full_rotation[kk,:])

            aa = fig_full_lc_(phase_full_rotation, lc_full/lc_transit_[0], \
                              phases_per_orbit_.flatten(), lc_transit_/lc_transit_[0], nn, ii, save_fig)
#
#
    averaged_spec_all_visits_ = np.nanmean(mean_subtracted_diff_flux_arr_, axis = 0)
    averaged_spec_all_visits_binned_ = np.nanmean(mean_subtracted_binned_spec_per_visit, axis = 0)
#
    if plot_individual_visits_ == 'y':
        save_fig = 'y'
        aa = fig_plot_individual_visits(number_of_visits_, filter_IR[idx_HST], mean_subtracted_diff_flux_arr_, \
                              bin_centers_, mean_subtracted_binned_spec_per_visit, nn, save_fig)
#
    plot_averaged_spec_all_visits_ = 'y'
    if plot_averaged_spec_all_visits_ == 'y':
        save_fig = 'y'
        aa=fig_plot_averaged_spec_all_visits_(bin_centers_, averaged_spec_all_visits_binned_, \
                                              normalized_HST_depths_, nn, save_fig)

    return mean_subtracted_diff_flux_arr_,mean_subtracted_binned_spec_per_visit,bin_centers_,\
star_map_all_visits_,phase_transit______no_output_____def many_visits_same_photosphere(star_map,add_planet,number_of_visits_,nn,show_full_lc_,lc_full,phase_full_rotation,\
                                plot_individual_visits_, show_rotated_star_map_, load_phase_transit, phase_load, \
                                observations_per_orbit_, add_noise_):

    diff_flux_arr_ = np.zeros((number_of_visits_, len(idx_HST)))
    mean_subtracted_diff_flux_arr_ = np.zeros((number_of_visits_, len(idx_HST)))
    mean_subtracted_binned_spec_per_visit = np.zeros((number_of_visits_, 17))
    per_bin_errors_ = np.random.uniform(-30.,30, size = (number_of_visits_,17))

    photosphere_id = 0.25
    spots_id = 0.04
    planet_id = -1.

    if load_phase_transit == 'n':
        phase_transit_ = np.zeros(number_of_visits_)

    for ii in range(number_of_visits_):

        if load_phase_transit == 'n':
#            phase_ = 0.67*np.linspace(0,3,4) + np.random.uniform(357.5)
            phase_ = 2.*Degrees_stellar_rotation_per_HST_orbit_*np.linspace(0,3,4) + np.random.uniform(357.5)
            phase_transit_[ii] = phase_[0]
        else:
#            phase_ = 0.67*np.linspace(0,3,4) + phase_load[ii]
            phase_ = 2.*Degrees_stellar_rotation_per_HST_orbit_*np.linspace(0,3,4) + phase_load[ii]
            phase_transit_ = phase_load
#
#
#        observations_per_orbit_ = 2
        pp_tmp_ = Degrees_stellar_rotation_per_HST_orbit_/observations_per_orbit_

        phases_per_orbit_ = np.zeros((len(phase_),observations_per_orbit_))
        for pp_ in range(len(phase_)):
            phases_per_orbit_[pp_,:] = phase_[pp_] + (pp_tmp_)*np.linspace(-observations_per_orbit_/2,\
                                                                           observations_per_orbit_/2,\
                                                                           observations_per_orbit_)
#        xxxx

        do_plot_ = 'n'
        star_cube_orbit_1 = rotate_star(star_map,phases_per_orbit_[0,:],do_plot_,add_planet,photosphere_id,spots_id)
        star_cube_orbit_2 = rotate_star(star_map,phases_per_orbit_[1,:],do_plot_,add_planet,photosphere_id,spots_id)
        star_cube_orbit_3 = rotate_star(star_map,phases_per_orbit_[2,:],do_plot_,add_planet,photosphere_id,spots_id)
        star_cube_orbit_4 = rotate_star(star_map,phases_per_orbit_[3,:],do_plot_,add_planet,photosphere_id,spots_id)
#
#
        if add_planet == 'y':
            if (ii+1 != 4) & (ii+1 != 8):
                star_cube_orbit_2, star_cube_orbit_3 = \
                add_two_planet_transits_(ii, star_cube_orbit_2, star_cube_orbit_3, observations_per_orbit_)
            if ii+1 == 4:
                star_cube_orbit_3 = \
                add_one_planet_transits_(ii, star_cube_orbit_3, observations_per_orbit_)
            if ii+1 == 8:
                star_cube_orbit_3, star_cube_orbit_4 = \
                add_two_planet_transits_(ii, star_cube_orbit_3, star_cube_orbit_4, observations_per_orbit_)
#
#

#        plt.figure(figsize=(10,20))
#        plt.subplot(421)
#        plt.imshow(star_cube_orbit_2[:,:,0], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(422)
#        plt.imshow(star_cube_orbit_3[:,:,0], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(423)
#        plt.imshow(star_cube_orbit_2[:,:,1], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(424)
#        plt.imshow(star_cube_orbit_3[:,:,1], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(425)
#        plt.imshow(star_cube_orbit_2[:,:,2], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(426)
#        plt.imshow(star_cube_orbit_3[:,:,2], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(427)
#        plt.imshow(star_cube_orbit_2[:,:,3], cmap='hot', vmin=-1., vmax=1.)

#        plt.subplot(428)
#        plt.imshow(star_cube_orbit_3[:,:,3], cmap='hot', vmin=-1., vmax=1.)
#        xxxx
        pp_ = observations_per_orbit_
        flux_cube_orbit_1 = make_flux_cube(star_cube_orbit_1, pp_, photosphere_id, spots_id, planet_id)
        flux_cube_orbit_2 = make_flux_cube(star_cube_orbit_2, pp_, photosphere_id, spots_id, planet_id)
        flux_cube_orbit_3 = make_flux_cube(star_cube_orbit_3, pp_, photosphere_id, spots_id, planet_id)
        flux_cube_orbit_4 = make_flux_cube(star_cube_orbit_4, pp_, photosphere_id, spots_id, planet_id)
#
#
        diff_flux_arr_[ii,:] = make_diff_flux_(ii, tdepth_,flux_cube_orbit_1, flux_cube_orbit_2, \
                                               flux_cube_orbit_3, flux_cube_orbit_4, idx_HST)
#
        mean_subtracted_diff_flux_arr_[ii,:] = diff_flux_arr_[ii,:] - 0.*np.nanmean(diff_flux_arr_[ii,:])
#

#        plt.plot(diff_flux_arr_[ii,:])
#        xxxx

        bin_means,bin_edges,binnumber = \
        stats.binned_statistic(filter_IR[idx_HST],mean_subtracted_diff_flux_arr_[ii,:],statistic='mean', bins=17)

        bin_width = (bin_edges[1] - bin_edges[0])
        bin_centers_ = bin_edges[1:] - bin_width/2

        if add_noise_ == 'y':
            mean_subtracted_binned_spec_per_visit[ii,:] =  bin_means + 1.*per_bin_errors_[ii,:]
        else:
            mean_subtracted_binned_spec_per_visit[ii,:] =  bin_means + 0.*per_bin_errors_[ii,:]
#
        if show_rotated_star_map_ == 'y':
            save_fig = 'n'
            aa = fig_rotated_star_map_(phase_, \
                                       star_cube_orbit_1, star_cube_orbit_2, star_cube_orbit_3, star_cube_orbit_4, \
                                       nn, ii, observations_per_orbit_, save_fig)
#
        if show_full_lc_ == 'y':
            save_fig = 'n'
            lc_transit_ = np.hstack((np.sum(flux_cube_orbit_1, axis = 1),
                                    np.sum(flux_cube_orbit_2, axis = 1),
                                    np.sum(flux_cube_orbit_3, axis = 1),
                                    np.sum(flux_cube_orbit_4, axis = 1)))

            aa = fig_full_lc_(phase_full_rotation, lc_full/lc_transit_[0], \
                              phases_per_orbit_.flatten(), lc_transit_/lc_transit_[0], nn, ii, save_fig)
#
#
    averaged_spec_all_visits_ = np.nanmean(mean_subtracted_diff_flux_arr_, axis = 0)
    averaged_spec_all_visits_binned_ = np.nanmean(mean_subtracted_binned_spec_per_visit, axis = 0)
#
#
    if plot_individual_visits_ == 'y':
        save_fig = 'n'
        aa = fig_plot_individual_visits(number_of_visits_, filter_IR[idx_HST], mean_subtracted_diff_flux_arr_, \
                              bin_centers_, mean_subtracted_binned_spec_per_visit, nn, save_fig)
#
    plot_averaged_spec_all_visits_ = 'y'
    if plot_averaged_spec_all_visits_ == 'y':
        save_fig = 'y'
        aa=fig_plot_averaged_spec_all_visits_(bin_centers_, averaged_spec_all_visits_binned_, \
                                              normalized_HST_depths_, nn, save_fig)
#
    return mean_subtracted_diff_flux_arr_, mean_subtracted_binned_spec_per_visit, bin_centers_, phase_transit______no_output_____number_of_iterations_ = 1#25
number_of_visits_ = 1
observations_per_orbit_ = 10

star_cube = np.zeros((750,750,4,number_of_iterations_))
flux_cube = np.zeros((4, 2551, number_of_iterations_))
mean_subtracted_diff_flux_arr_all_iterations_ = np.zeros((number_of_iterations_,number_of_visits_,
                                                          len(filter_IR[idx_HST])))

star_map = np.zeros((180, 360, number_of_iterations_))

phase_transit_ = np.zeros((number_of_iterations_, number_of_visits_))

photosphere_ = 'random'
photosphere_ = 'same'
load_phase_transit, load_star_map = 'n', 'n'#'y', 'y'#
add_planet_ = 'y'#'y'
add_noise_ = 'n'#'y'

show_full_lc_ = 'y'#'n'
plot_individual_visits_ = 'y'
show_rotated_star_map_ = 'y'#'y'#

for ii in range(number_of_iterations_):

    if photosphere_ == 'random':

        mean_subtracted_diff_flux_arr_,mean_subtracted_binned_spec_per_visit,bin_centers_,\
        star_map_tmp,phase_transit_tmp=many_visits_random_photosphere(number_of_spots,area_covered,add_planet_,\
                                                                      number_of_visits_,ii,show_full_lc_, \
                                                                      plot_individual_visits_, \
                                                                      show_rotated_star_map_, \
                                                                      observations_per_orbit_)
        phase_transit_[ii, :] = phase_transit_tmp
        star_map[:,:,ii] = star_map_tmp

    if photosphere_ == 'same':

        spot_lat_ = -60. + 120*np.random.random(number_of_spots)
        spot_lon_ = -180 + 360.*np.random.random(number_of_spots)
        spot_sizes_ = spot_sizes(area_covered, number_of_spots, aspect_ratio)
        faculae_size = 1.5*spot_sizes_ # WIP

        photosphere_id = 0.25
        spots_id = 0.04
        planet_id = -1.

        do_plot_ = 'n'

        if (load_phase_transit == 'n') & (load_star_map == 'n'):
            star_map_same = \
            create_photosphere_map(spot_sizes_,faculae_size,spot_lon_,spot_lat_,photosphere_id,spots_id)
            phase_transit_load = -999.

        else:
            star_map_file = 'star_map_file_42820.npy'#.npy'#
            phase_file = 'phase_file_42820.npy'#.npy'#_041620.npy'
            star_map_load, phase_transit_load = np.load(star_map_file), np.load(phase_file)

            idx_ = -1# FOR WATER FEATURE IN "STAR_MAP_FILE.NPY"
            star_map_same, phase_transit_load = star_map_load[:,:,idx_], phase_transit_load[idx_,:]

        phase_full_rotation = np.linspace(0.,359., 10)

        do_plot_ = 'n'
        star_cube_full_rotation = \
        rotate_star(star_map_same, phase_full_rotation, do_plot_, add_planet_, photosphere_id, spots_id)
        flux_cube_full_rotation = \
        make_flux_cube(star_cube_full_rotation, len(phase_full_rotation), photosphere_id, spots_id, planet_id)

        lc_full = np.zeros((flux_cube_full_rotation.shape[0]))
        for kk in range(len(lc_full)):
            lc_full[kk] = np.sum(flux_cube_full_rotation[kk,:])

        mean_subtracted_diff_flux_arr_, mean_subtracted_binned_spec_per_visit, bin_centers_, phase_transit_tmp = \
        many_visits_same_photosphere(star_map_same, add_planet_, number_of_visits_, ii, show_full_lc_, lc_full, \
                                     phase_full_rotation, plot_individual_visits_, show_rotated_star_map_, \
                                     load_phase_transit, phase_transit_load, observations_per_orbit_,add_noise_)

        mean_subtracted_diff_flux_arr_all_iterations_[ii,:,:] = mean_subtracted_diff_flux_arr_

        phase_transit_[ii, :] = phase_transit_tmp

        star_map[:,:,ii] = star_map_same

    print('Iteration ', str(ii), '...done!')Iteration  0 ...done!
q = pd.read_csv(fn_model_Teff, header = 7, delim_whitespace=True, names = ['wavelength', 'flux'])_____no_output_____qq = q.rolling(window=70, ).mean()_____no_output_____qq.plot(x='wavelength', y='flux')
plt.xlim(1000, 20000)_____no_output_____np.interp()_____no_output_____
</code>


# Notebook from ksjhint43/ksjhint43.github.io
Path: bang_dream/bang_dream_complete.ipynb

import selenium
from selenium import webdriver
from selenium.webdriver import ActionChains

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.chrome.options import Options
import os

from bs4 import BeautifulSoup

import time

import pandas as pd

import urllib.request
import random

import sys
import io

import copy_____no_output_____

# Original_____no_output_____

# 크롬 켜기
from selenium import webdriver

options = webdriver.ChromeOptions()
# options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# chrome-extension://djflhoibgkdhkhhcedjiklpkjnoahfmg/options.html
options.add_argument("user-agent=Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36")

driver = webdriver.Chrome(executable_path='chromedriver',options=options)

URL = 'https://bandori.fandom.com/wiki/Category:Original_Songs'
driver.get(url=URL)
_____no_output_____

time.sleep(10)

info_all = pd.DataFrame(columns=['theme','kor_theme','artist','page_url','mp3_url','mp3_name_pre',"mp3_name","mp3_error"])

# 알파벳 순서대로 클릭

driver.implicitly_wait(20)

ksjs = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')

cat_i = 0

while True :

    driver.implicitly_wait(20)
    print("see1")
    ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')

    while True:
        try :
            print("again0")
            time.sleep(3)
            ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')
            print(ksjs_in[cat_i].find_element_by_tag_name('a').text)
            ksjs_in[cat_i].find_element_by_tag_name('a').click()
            break
        except :
            print("again11")
            move_scroll = random.uniform(-200, 200)
            driver.execute_script("window.scrollTo(0, "+str(move_scroll)+")")
            time.sleep(5)
            continue

    driver.implicitly_wait(20)

    if "#google_vignette" in driver.current_url :

        driver.refresh()
        element = WebDriverWait(driver, 30).until(
            EC.presence_of_element_located((By.CLASS_NAME, "category-alphabet-shortcuts__item"))
        )
        while True:
            try :
                print("again01")
                time.sleep(3)
                ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')
                print(ksjs_in[cat_i].find_element_by_tag_name('a').text)
                ksjs_in[cat_i].find_element_by_tag_name('a').click()
                break
            except :
                print("again12")

                move_scroll = random.uniform(-200, 200)
                driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                time.sleep(5)
                continue

    # 해당 알파벳에 존재하는 각 곡으로 들어가기
    driver.implicitly_wait(20)

    ksj2 = driver.find_elements_by_class_name('category-members-grouped__member')

    cat_i2 = 0

    while True :

        driver.implicitly_wait(20)
        time.sleep(3)
        #ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')

        while True:
            try :
                time.sleep(2)
                print("go1")
                ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')
                print(ksj2_in[cat_i2].find_element_by_tag_name('a').text)
                ksj2_in[cat_i2].find_element_by_tag_name('a').click()
                print("go2")
                break
            except :
                try :
                    print("go11")
                    move_scroll = random.uniform(-200, 200)
                    driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                    a_artist_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
                    print("go111")
                    break
                except :
                    pass

                print("again21")
                time.sleep(2)
                continue

        driver.implicitly_wait(10)
        if "#google_vignette" in driver.current_url :

            driver.refresh()
            driver.implicitly_wait(10)
            element = WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.CLASS_NAME, "category-members-grouped__member"))
            )
            while True:
                try :
                    time.sleep(2)
                    print("go2")
                    ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')
                    print(ksj2_in[cat_i2].find_element_by_tag_name('a').text)
                    ksj2_in[cat_i2].find_element_by_tag_name('a').click()
                    break
                except :
                    try :
                        print("go21")
                        move_scroll = random.uniform(-100, 100)
                        driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                        a_artist_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
                        print("go211")
                        break
                    except :
                        pass

                    print("again22")
                    time.sleep(2)
                    continue

        print("start")

        driver.implicitly_wait(10)
        sources = driver.find_elements_by_tag_name('audio')
        if sources ==[] :
            sources == ['']
        for source_i in sources :

            while True:
                try :

                    driver.implicitly_wait(10)
                    time.sleep(1)
                    theme_small = driver.find_element_by_class_name('wiki-page-header__title').text
                    break
                    print("again31")
                except :
                    time.sleep(2)
                    continue

            print("theme_small : ",theme_small)

            try :
                kortheme_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/section[1]/div[2]/div/span').text
            except :
                kortheme_small = ''

            try :
                driver.implicitly_wait(10)
                time.sleep(2)
                element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "pi-expand-button")))
                more_info_button = driver.find_elements_by_class_name('pi-expand-button')
                for button_i in more_info_button:
                    button_i.click()
                    print("buttons are clicked")
            except :
                pass

            try :
                artist_small_all = driver.find_elements_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/section[2]/div[2]/div/b/span')
                if artist_small_all == []:
                    raise Exception('artist_small is empty')

                artist_small = ''
                for num_i,i in enumerate(artist_small_all) :
                    print(i.find_element_by_xpath('./a').text)
                    artist_small_i = i.find_element_by_xpath('./a').text
                    if num_i == 0 :
                        artist_small = artist_small_i
                    else :
                        artist_small = artist_small + ' x '+ artist_small_i

            except :
                try :
                    artist_small_all = driver.find_elements_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/section[1]/div[2]/div/b')
                    if artist_small_all == []:
                        raise Exception('artist_small is empty')

                    artist_small = ''
                    for num_i,i in enumerate(artist_small_all) :
                        print(i.find_element_by_xpath('./a').text)
                        artist_small_i = i.find_element_by_xpath('./a').text
                        if num_i == 0 :
                            artist_small = artist_small_i
                        else :
                            artist_small = artist_small + ' x '+ artist_small_i

                except :
                    try :
                        artist_small_all = driver.find_elements_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/section[2]/div[2]/div/b')
                        if artist_small_all == []:
                            raise Exception('artist_small is empty')

                        artist_small = ''
                        for num_i,i in enumerate(artist_small_all) :
                            print(i.find_element_by_xpath('./a').text)
                            artist_small_i = i.find_element_by_xpath('./a').text
                            if num_i == 0 :
                                artist_small = artist_small_i
                            else :
                                artist_small = artist_small + ' x '+ artist_small_i

                    except :
                        try :
                            artist_small_all = driver.find_elements_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/section[1]/div[2]/div')
                            if artist_small_all == []:
                                raise Exception('artist_small is empty')

                            artist_small = ''
                            for num_i,i in enumerate(artist_small_all) :
                                print(i.find_element_by_xpath('./a').text)
                                artist_small_i = i.find_element_by_xpath('./a').text
                                if num_i == 0 :
                                    artist_small = artist_small_i
                                else :
                                    artist_small = artist_small + ' x '+ artist_small_i

                        except :
                            try :
                                artist_small_all = driver.find_elements_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/section[2]/div[2]/div')
                                if artist_small_all == []:
                                    raise Exception('artist_small is empty')

                                artist_small = ''
                                for num_i,i in enumerate(artist_small_all) :
                                    print(i.find_element_by_xpath('./a').text)
                                    artist_small_i = i.find_element_by_xpath('./a').text
                                    if num_i == 0 :
                                        artist_small = artist_small_i
                                    else :
                                        artist_small = artist_small + ' x '+ artist_small_i
                            except :
                                artist_small = ""
                                print("artist_small : ",artist_small)

            page_url = driver.current_url
            try :
                file_url = source_i.find_element_by_tag_name('source').get_attribute("src")
                file_names = source_i.get_attribute("data-mwtitle").replace('.ogg','')
            except :
                file_url = ''
                file_names = ''
            print(file_names)

            paths = './mp3/'

            change_text = {"\\":'_',
                   "/":';',
                   ":":';',
                   "*":'+',
                   "?":'',
                   '"':"'",
                   "<":'[',
                   ">":']',
                   "|":'_'}

            file_names2 = copy.deepcopy(file_names)
            for change_text_i in change_text :
                file_names2 = file_names2.replace(change_text_i,change_text[change_text_i])

            try:
                error_type = ''
                urllib.request.urlretrieve(file_url, filename = paths + file_names2+'.mp3')
            except Exception  as err  :
                error_type = str(err)

            my_dict = {"theme": [theme_small],
                    "kor_theme": [kortheme_small],
                    "artist": [artist_small],
                    "page_url": [page_url],
                    "mp3_url": [file_url],
                   "mp3_name_pre" : [file_names],
                    "mp3_name" : [file_names2],
                    "mp3_error" : [error_type]}

            info_small =  pd.DataFrame(my_dict)

            info_all = info_all.append(info_small).reset_index(drop=True)
            info_all.to_csv("./music_info_rev_rev2.csv",encoding='utf-8')

        c_url = driver.current_url

        time.sleep(2)
        driver.back()
        print("see2")
        driver.implicitly_wait(10)
        print("see3")
        print("cat_i2 : ",cat_i2)
        print("len(ksj2) : ",len(ksj2))

        cat_i2 = cat_i2 + 1
        if (cat_i2 >= len(ksj2))|((cat_i==0)&(cat_i2>=2)) :
            break

    print("cat_i : ",cat_i)
    print("len(ksjs) : ",len(ksjs))

    print("see4")
    c_url = driver.current_url

    cat_i = cat_i + 1

    if cat_i >= len(ksjs) :
        breaksee1
again0
S
go1
Sacred world
go2
go2
Sacred world
go21
again22
go2
Sacred world
go21
again22
go2
Sacred world
start
theme_small :  Sacred world
buttons are clicked
buttons are clicked
RAISE A SUILEN
Sacred_world
theme_small :  Sacred world
RAISE A SUILEN
Sacred_world_-instrumental-
theme_small :  Sacred world
RAISE A SUILEN
Sacred_world_-instrumental-
theme_small :  Sacred world
RAISE A SUILEN
Sacred_world_(Game_Version)
see2
see3
cat_i2 :  0
len(ksj2) :  27
go1
Safe and Sound
go11
again21
go1
Safe and Sound
go11
again21
go1
Safe and Sound
go2
start
theme_small :  Safe and Sound
buttons are clicked
buttons are clicked
Roselia
Safe_and_Sound
theme_small :  Safe and Sound
Roselia
Safe_and_Sound_-instrumental-
theme_small :  Safe and Sound
Roselia
Safe_and_Sound_(Game_Version)
see2
see3
cat_i2 :  1
len(ksj2) :  27
go1
Saikou (Saa Ikou!)
go11
again21
go1
Saikou (Saa Ikou!)
go11
again21
go1
Saikou (Saa Ikou!)
go2
start
theme_small :  Saikou (Saa Ikou!)
buttons are clicked
buttons are clicked
Poppin'Party
Saikou_(Saa_Ikou!)
theme_small :  Saikou (Saa Ikou!)
Poppin'Party
Saikou_(Saa_Ikou!)_-Instrumental-
theme_small :  Saikou (Saa Ikou!)
Poppin'Party
Saikou_(Saa_Ikou!)_(Game_Version)
theme_small :  Saikou (Saa Ikou!)
Poppin'Party
Excellent_(Hey,_Let's_Go!)_(Game_Version)
see2
see3
cat_i2 :  2
len(ksj2) :  27
go1
SAKURA MEMORIES
go2
start
theme_small :  SAKURA MEMORIES
buttons are clicked
buttons are clicked
Poppin'Party
SAKURA_MEMORIES
theme_small :  SAKURA MEMORIES
Poppin'Party
SAKURA_MEMORIES_-instrumental-
theme_small :  SAKURA MEMORIES
Poppin'Party
SAKURA_MEMORIES_(Game_Version)
see2
see3
cat_i2 :  3
len(ksj2) :  27
go1
Sakura Zensen
go11
again21
go1
Sakura Zensen
go11
again21
go1
Sakura Zensen
go2
start
see2
see3
cat_i2 :  4
len(ksj2) :  27
go1
Sanctuary
go2
start
theme_small :  Sanctuary
buttons are clicked
Roselia
Sanctuary
theme_small :  Sanctuary
Roselia
Sanctuary_-instrumental-
theme_small :  Sanctuary
buttons are clicked
Roselia
Sanctuary_(Game_Version)
see2
see3
cat_i2 :  5
len(ksj2) :  27
go1
Sasanqua
go11
again21
go1
Sasanqua
go11
again21
go1
Sasanqua
go2
start
theme_small :  Sasanqua
buttons are clicked
buttons are clicked
Afterglow
Sasanqua
theme_small :  Sasanqua
Afterglow
Sasanqua_-instrumental-
theme_small :  Sasanqua
Afterglow
Sasanqua_(Game_Version)
see2
see3
cat_i2 :  6
len(ksj2) :  27
go1
Scarlet Sky
go2
start
theme_small :  Scarlet Sky
buttons are clicked
buttons are clicked
Afterglow
Scarlet_Sky
theme_small :  Scarlet Sky
Afterglow
04._Scarlet_Sky_-Instrumental-
theme_small :  Scarlet Sky
Afterglow
Scarlet_Sky_(THE_THIRD_1st_Live)
theme_small :  Scarlet Sky
Afterglow
Scarlet_Sky_(Game_Version)
see2
see3
cat_i2 :  7
len(ksj2) :  27
go1
Secret Dawn
go2
start
theme_small :  Secret Dawn
Morƒonica
Secret_Dawn_(Game_Version)
see2
see3
cat_i2 :  8
len(ksj2) :  27
go1
Sekai Nobbinobi Treasure!
go2
start
theme_small :  Sekai Nobbinobi Treasure!
buttons are clicked
buttons are clicked
Hello, Happy World!
Sekai_Nobbinobi_Treasure!
theme_small :  Sekai Nobbinobi Treasure!
Hello, Happy World!
Sekai_Nobbinobi_Treasure!_-Instrumental-
theme_small :  Sekai Nobbinobi Treasure!
Hello, Happy World!
Sekai_Nobbinobi_Treasure!_(Game_Version)
see2
see3
cat_i2 :  9
len(ksj2) :  27
go1
Senkou
go2
start
theme_small :  Senkou
buttons are clicked
Roselia
Senkou_(Game_Version)
see2
see3
cat_i2 :  10
len(ksj2) :  27
go1
SENSENFUKOKU
go2
start
theme_small :  SENSENFUKOKU
buttons are clicked
buttons are clicked
Afterglow
SENSENFUKOKU
theme_small :  SENSENFUKOKU
Afterglow
SENSENFUKOKU_-instrumental-
theme_small :  SENSENFUKOKU
Afterglow
SENSENFUKOKU_(Game_Version)
see2
see3
cat_i2 :  11
len(ksj2) :  27
go1
Setsunai Sandglass
go2
start
theme_small :  Setsunai Sandglass
buttons are clicked
buttons are clicked
Poppin'Party
Setsunai_Sandglass
theme_small :  Setsunai Sandglass
Poppin'Party
Setsunai_Sunglass_-instrumental-
theme_small :  Setsunai Sandglass
Poppin'Party
Setsunai_Sandglass_(Game_version)
see2
see3
cat_i2 :  12
len(ksj2) :  27
go1
Shakunetsu Bonfire!
go11
again21
go1
Shakunetsu Bonfire!
go11
again21
go1
Shakunetsu Bonfire!
go2
start
theme_small :  Shakunetsu Bonfire!
buttons are clicked
RAISE A SUILEN
Shakunetsu_Bonfire!
theme_small :  Shakunetsu Bonfire!
buttons are clicked
RAISE A SUILEN
Shakunetsu_Bonfire!_-instrumental-
theme_small :  Shakunetsu Bonfire!
buttons are clicked
RAISE A SUILEN
Shakunetsu_Bonfire!_(Game_Version)
see2
see3
cat_i2 :  13
len(ksj2) :  27
go1
Shuwarin☆Drea~min
go2
start
theme_small :  Shuwarin☆Drea~min
buttons are clicked
buttons are clicked
Pastel✽Palettes
Shuwarin☆Drea~min
theme_small :  Shuwarin☆Drea~min
Pastel✽Palettes
Shuwarin☆Drea~min_-instrumental-
theme_small :  Shuwarin☆Drea~min
Pastel✽Palettes
Shuwarin☆Drea~min_(THE_THIRD_1st_Live)
theme_small :  Shuwarin☆Drea~min
Pastel✽Palettes
Shuwarin☆Drea~min_(Game_Version)
see2
see3
cat_i2 :  14
len(ksj2) :  27
go1
Sing Alive
go11
again21
go1
Sing Alive
go11
again21
go1
Sing Alive
go2
start
theme_small :  Sing Alive
buttons are clicked
Roselia
Sing_Alive
theme_small :  Sing Alive
Roselia
Sing_Alive_-instrumental-
theme_small :  Sing Alive
buttons are clicked
Roselia
Sing_Alive_(Game_Version)
see2
see3
cat_i2 :  15
len(ksj2) :  27
go1
Singing "OURS"
go2
start
theme_small :  Singing "OURS"
buttons are clicked
Roselia
Singing_"OURS"
theme_small :  Singing "OURS"
Roselia
Singing_"OURS"_-instrumental-
theme_small :  Singing "OURS"
buttons are clicked
Roselia
Singing_"OURS"_(Game_Version)
see2
see3
cat_i2 :  16
len(ksj2) :  27
go1
Smile Bouquet de Ta~maya~!
go2
start
theme_small :  Smile Bouquet de Ta~maya~!
buttons are clicked
buttons are clicked
Hello, Happy World!
Smile_Bouquet_de_Ta~maya~!
theme_small :  Smile Bouquet de Ta~maya~!
Hello, Happy World!
Smile_Bouquet_de_Ta~maya~!_-instrumental-
theme_small :  Smile Bouquet de Ta~maya~!
Hello, Happy World!
Smile_Bouquet_de_Ta~maya~!_(Game_Version)
see2
see3
cat_i2 :  17
len(ksj2) :  27
go1
Song I am.
go11
again21
go1
Song I am.
go11
again21
go1
Song I am.
go2
start
theme_small :  Song I am.
buttons are clicked
buttons are clicked
Roselia
Song_I_am
theme_small :  Song I am.
Roselia
Song_I_am._-instrumental-
theme_small :  Song I am.
Roselia
Song_I_am._(Game_Version)
see2
see3
cat_i2 :  18
len(ksj2) :  27
go1
Sonorous
go11
again21
go1
Sonorous
go11
again21
go1
Sonorous
go2
start
theme_small :  Sonorous
buttons are clicked
Morƒonica
Sonorous
theme_small :  Sonorous
Morƒonica
Sonorous_-instumental-
theme_small :  Sonorous
buttons are clicked
Morƒonica
Sonorous_(Game_Version)
see2
see3
cat_i2 :  19
len(ksj2) :  27
go1
SOUL SOLDIER
go2
start
theme_small :  SOUL SOLDIER
buttons are clicked
buttons are clicked
RAISE A SUILEN
SOUL_SOLDIER
theme_small :  SOUL SOLDIER
RAISE A SUILEN
SOUL_SOLDIER_-instrumental-
see2
see3
cat_i2 :  20
len(ksj2) :  27
go1
Sprechchor
go11
again21
go1
Sprechchor
go11
again21
go1
Sprechchor
go2
start
theme_small :  Sprechchor
buttons are clicked
buttons are clicked
Roselia
Sprechchor
theme_small :  Sprechchor
Roselia
Sprechchor_(Game_Version)
see2
see3
cat_i2 :  21
len(ksj2) :  27
go1
STAR BEAT! ~Hoshi no Kodou~
go11
again21
go1
STAR BEAT! ~Hoshi no Kodou~
go11
again21
go1
STAR BEAT! ~Hoshi no Kodou~
go2
start
theme_small :  STAR BEAT! ~Hoshi no Kodou~
buttons are clicked
buttons are clicked
Poppin'Party
STAR_BEAT!_~Hoshi_no_Kodou~
theme_small :  STAR BEAT! ~Hoshi no Kodou~
Poppin'Party
STAR_BEAT!_~Hoshi_no_Kodou~_-Instrumental-
theme_small :  STAR BEAT! ~Hoshi no Kodou~
Poppin'Party
STAR_BEAT_~Hoshi_no_Kodou~_(Game_Version)
theme_small :  STAR BEAT! ~Hoshi no Kodou~
Poppin'Party
STAR_BEAT!_~Hoshi_no_Kodou~_～Acoustic_Ver.～
theme_small :  STAR BEAT! ~Hoshi no Kodou~
Poppin'Party
STAR_BEAT!_~Hoshi_no_Kodou~_～Acoustic_Ver.～_-instrumental-
theme_small :  STAR BEAT! ~Hoshi no Kodou~
Poppin'Party
STAR_BEAT!_~Hoshi_no_Kodou~_~Popipa_Acoustic_Ver.~
see2
see3
cat_i2 :  22
len(ksj2) :  27
go1
Step×Step!
go11
again21
go1
Step×Step!
go11
again21
go1
Step×Step!
go2
start
theme_small :  Step×Step!
buttons are clicked
buttons are clicked
Poppin'Party
Step×Step!
theme_small :  Step×Step!
Poppin'Party
Step×Step!_-instrumental-
theme_small :  Step×Step!
Poppin'Party
Step×Step!_(Game_Version)
see2
see3
cat_i2 :  23
len(ksj2) :  27

# rev_____no_output_____

time.sleep(10)

info_all = pd.DataFrame(columns=['artist','theme','kor_theme','eng_theme','trans_theme',
                                 'page_url','mp3_url','mp3_name_pre',"mp3_name","mp3_error"])

# 알파벳 순서대로 클릭

driver.implicitly_wait(20)

ksjs = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')

cat_i = 0

while True :

    driver.implicitly_wait(20)
    print("see1")
    ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')

    while True:
        try :
            print("again0")
            time.sleep(3)
            ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')
            print(ksjs_in[cat_i].find_element_by_tag_name('a').text)
            ksjs_in[cat_i].find_element_by_tag_name('a').click()
            break
        except :
            print("again11")
            move_scroll = random.uniform(-200, 200)
            driver.execute_script("window.scrollTo(0, "+str(move_scroll)+")")
            time.sleep(5)
            continue

    driver.implicitly_wait(20)

    if "#google_vignette" in driver.current_url :

        driver.refresh()
        element = WebDriverWait(driver, 30).until(
            EC.presence_of_element_located((By.CLASS_NAME, "category-alphabet-shortcuts__item"))
        )
        while True:
            try :
                print("again01")
                time.sleep(3)
                ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')
                print(ksjs_in[cat_i].find_element_by_tag_name('a').text)
                ksjs_in[cat_i].find_element_by_tag_name('a').click()
                break
            except :
                print("again12")

                move_scroll = random.uniform(-200, 200)
                driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                time.sleep(5)
                continue

    # 해당 알파벳에 존재하는 각 곡으로 들어가기
    driver.implicitly_wait(20)

    ksj2 = driver.find_elements_by_class_name('category-members-grouped__member')

    cat_i2 = 0

    while True :

        driver.implicitly_wait(20)
        time.sleep(3)
        #ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')

        while True:
            try :
                time.sleep(2)
                print("go1")
                ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')
                print(ksj2_in[cat_i2].find_element_by_tag_name('a').text)
                ksj2_in[cat_i2].find_element_by_tag_name('a').click()
                print("go2")
                break
            except :
                try :
                    print("go11")
                    move_scroll = random.uniform(-200, 200)
                    driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                    a_artist_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
                    print("go111")
                    break
                except :
                    pass

                print("again21")
                time.sleep(2)
                continue

        driver.implicitly_wait(10)
        if "#google_vignette" in driver.current_url :

            driver.refresh()
            driver.implicitly_wait(10)
            element = WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.CLASS_NAME, "category-members-grouped__member"))
            )
            while True:
                try :
                    time.sleep(2)
                    print("go2")
                    ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')
                    print(ksj2_in[cat_i2].find_element_by_tag_name('a').text)
                    ksj2_in[cat_i2].find_element_by_tag_name('a').click()
                    break
                except :
                    try :
                        print("go21")
                        move_scroll = random.uniform(-200, 200)
                        driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                        a_artist_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
                        print("go211")
                        break
                    except :
                        pass

                    print("again22")
                    time.sleep(2)
                    continue

        print("start")

        driver.implicitly_wait(10)

        # 노래정보

        while True:
            try :

                driver.implicitly_wait(10)
                time.sleep(1)
                theme_small = driver.find_element_by_class_name('wiki-page-header__title').text
                break
                print("again31")
            except :
                time.sleep(2)
                continue

        print("theme_small : ",theme_small)

        try :
            driver.implicitly_wait(10)
            time.sleep(2)
            element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "pi-expand-button")))
            more_info_button = driver.find_elements_by_class_name('pi-expand-button')
            for button_i in more_info_button:
                button_i.click()
                print("buttons are clicked")
        except :
            pass

        driver.implicitly_wait(10)
        artist_small_all = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
        artist_small_all = artist_small_all.find_elements_by_xpath('./section')

        kortheme_small = ''
        engtheme_small = ''
        engtrantheme_small = ''
        artist_small = ''

        for xpath_artist_numi, xpath_artist_i in enumerate(artist_small_all) :
            try :
                if xpath_artist_i.find_element_by_xpath('./div[1]/h2').text == 'SONG INFORMATION' :
                    xpath_artist_section = xpath_artist_numi
            except :
                if xpath_artist_numi == 0 :
                    theme_xpath = xpath_artist_i.find_elements_by_xpath('./div')
                    for theme_xpath_i in theme_xpath :
                        try :
                            print(theme_xpath_i.find_element_by_xpath('./h3').text.upper())
                            if theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'KOREAN TITLE':
                                kortheme_small = theme_xpath_i.find_element_by_xpath('./div/span').text
                                print(kortheme_small)
                            elif theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'ENGLISH TITLE':
                                engtheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                                print(engtheme_small)
                            elif theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'TRANSLATED TITLE':
                                engtrantheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                                print(engtrantheme_small)
                        except :
                            pass

        try :
            artist_small = artist_small_all[xpath_artist_section].find_element_by_xpath('./div[2]/div').text
            print(artist_small)
            if artist_small == '':
                raise Exception('artist_small is empty')
        except :
            artist_small = artist_small_all[xpath_artist_section].find_element_by_xpath('./div[2]/div/b').text
            print(artist_small)

        page_url = driver.current_url

        # mp3 다운
        sources = driver.find_elements_by_tag_name('audio')
        if sources ==[] :
            sources == ['']

        for source_i in sources :

            try :
                file_url = source_i.find_element_by_tag_name('source').get_attribute("src")
                file_names = source_i.get_attribute("data-mwtitle").replace('.ogg','')
            except :
                file_url = ''
                file_names = ''
            print(file_names)

            paths = './mp3/'

            change_text = {"\\":'_',
                   "/":';',
                   ":":';',
                   "*":'+',
                   "?":'',
                   '"':"'",
                   "<":'[',
                   ">":']',
                   "|":'_'}

            file_names2 = copy.deepcopy(file_names)
            for change_text_i in change_text :
                file_names2 = file_names2.replace(change_text_i,change_text[change_text_i])

            try:
                error_type = 'normal'
                urllib.request.urlretrieve(file_url, filename = paths + file_names2+'.mp3')
            except Exception  as err  :
                error_type = str(err)

            my_dict = {"artist": [artist_small],
                       "theme": [theme_small],
                    "kor_theme": [kortheme_small],
                    "eng_theme" :[engtheme_small],
                       "trans_theme" :[engtrantheme_small],
                    "page_url": [page_url],
                    "mp3_url": [file_url],
                   "mp3_name_pre" : [file_names],
                    "mp3_name" : [file_names2],
                    "mp3_error" : [error_type]}

            info_small =  pd.DataFrame(my_dict)

            info_all = info_all.append(info_small).reset_index(drop=True)
            info_all.to_csv("./music_info_rev_rev2.csv",encoding='utf-8')

        c_url = driver.current_url

        time.sleep(2)
        driver.back()
        print("see2")
        driver.implicitly_wait(10)
        print("see3")
        print("cat_i2 : ",cat_i2)
        print("len(ksj2) : ",len(ksj2))

        cat_i2 = cat_i2 + 1
        if (cat_i2 >= len(ksj2))|((cat_i==0)&(cat_i2>=2)) :
            break

    print("cat_i : ",cat_i)
    print("len(ksjs) : ",len(ksjs))

    print("see4")
    c_url = driver.current_url

    cat_i = cat_i + 1

    if cat_i >= len(ksjs) :
        break_____no_output_____

# cover_____no_output_____

# 크롬 켜기
from selenium import webdriver

options = webdriver.ChromeOptions()
# options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# chrome-extension://djflhoibgkdhkhhcedjiklpkjnoahfmg/options.html
options.add_argument("user-agent=Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36")

driver = webdriver.Chrome(executable_path='chromedriver',options=options)

URL = 'https://bandori.fandom.com/wiki/Category:Cover_Songs'
driver.get(url=URL)
_____no_output_____

time.sleep(10)

info_all = pd.DataFrame(columns=['artist','theme','kor_theme','eng_theme','trans_theme',
                                 'page_url','mp3_url','mp3_name_pre',"mp3_name","mp3_error"])

# 알파벳 순서대로 클릭

driver.implicitly_wait(20)

ksjs = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')

cat_i = 0

pass_sign = False

while True :

    driver.implicitly_wait(20)
    print("see1")
    ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')

    while True:
        try :
            print("again0")
            time.sleep(3)
            ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')
            print(ksjs_in[cat_i].find_element_by_tag_name('a').text)
            ksjs_in[cat_i].find_element_by_tag_name('a').click()
            break
        except :
            print("again11")
            move_scroll = random.uniform(-200, 200)
            driver.execute_script("window.scrollTo(0, "+str(move_scroll)+")")
            time.sleep(5)
            continue

    driver.implicitly_wait(20)

    if "#google_vignette" in driver.current_url :

        driver.refresh()
        element = WebDriverWait(driver, 30).until(
            EC.presence_of_element_located((By.CLASS_NAME, "category-alphabet-shortcuts__item"))
        )
        while True:
            try :
                print("again01")
                time.sleep(3)
                ksjs_in = driver.find_elements_by_class_name('category-alphabet-shortcuts__item')
                print(ksjs_in[cat_i].find_element_by_tag_name('a').text)
                ksjs_in[cat_i].find_element_by_tag_name('a').click()
                break
            except :
                print("again12")

                move_scroll = random.uniform(-200, 200)
                driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                time.sleep(5)
                continue

    # 해당 알파벳에 존재하는 각 곡으로 들어가기
    driver.implicitly_wait(20)

    ksj2 = driver.find_elements_by_class_name('category-members-grouped__member')

    cat_i2 = 0

    while True :

        driver.implicitly_wait(20)
        time.sleep(3)
        #ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')

        while True:
            try :
                time.sleep(2)
                print("go1")
                ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')
                print(ksj2_in[cat_i2].find_element_by_tag_name('a').text)
                if "Category:" in ksj2_in[cat_i2].find_element_by_tag_name('a').text :
                    pass_sign = True
                    break
                ksj2_in[cat_i2].find_element_by_tag_name('a').click()
                print("go2")
                break
            except :
                try :
                    print("go11")
                    move_scroll = random.uniform(-200, 200)
                    driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                    a_artist_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
                    print("go111")
                    break
                except :
                    pass

                print("again21")
                time.sleep(2)
                continue

        driver.implicitly_wait(10)
        if "#google_vignette" in driver.current_url :

            driver.refresh()
            driver.implicitly_wait(10)
            element = WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.CLASS_NAME, "category-members-grouped__member"))
            )
            while True:
                try :
                    time.sleep(2)
                    print("go2")
                    ksj2_in = driver.find_elements_by_class_name('category-members-grouped__member')
                    print(ksj2_in[cat_i2].find_element_by_tag_name('a').text)
                    ksj2_in[cat_i2].find_element_by_tag_name('a').click()
                    break
                except :
                    try :
                        print("go21")
                        move_scroll = random.uniform(-200, 200)
                        driver.execute_script("window.scrollTo(0,  "+str(move_scroll)+")")
                        a_artist_small = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')
                        print("go211")
                        break
                    except :
                        pass

                    print("again22")
                    time.sleep(2)
                    continue

        print("start")

        driver.implicitly_wait(10)

        if pass_sign :
            pass_sign = False
            cat_i2 = cat_i2 + 1
            if (cat_i2 >= len(ksj2))|((cat_i==0)&(cat_i2>=2)) :
                break

            continue

        # 노래정보

        while True:

            try :

                driver.implicitly_wait(10)
                time.sleep(1)
                theme_small = driver.find_element_by_class_name('wiki-page-header__title').text
                break
                print("again31")
            except :
                time.sleep(2)
                continue

        print("theme_small : ",theme_small)

        try :
            driver.implicitly_wait(10)
            time.sleep(2)
            element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "pi-expand-button")))
            more_info_button = driver.find_elements_by_class_name('pi-expand-button')
            for button_i in more_info_button:
                button_i.click()
                print("buttons are clicked")
        except :
            pass

        driver.implicitly_wait(10)
        artist_small_all = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')

        artist_small = ''

        try :
            artist_small_all_info = artist_small_all.find_elements_by_xpath('./section')
            for xpath_artist_numi, xpath_artist_i in enumerate(artist_small_all_info) :

                if xpath_artist_i.find_element_by_xpath('./div[1]/h2').text == 'COVER INFORMATION' :
                    xpath_artist_section = xpath_artist_numi

                try :
                    artist_small = artist_small_all_info[xpath_artist_section].find_element_by_xpath('./div[2]/div').text
                    print(artist_small)
                    if artist_small == '':
                        raise Exception('artist_small is empty')
                except :
                    artist_small = artist_small_all_info[xpath_artist_section].find_element_by_xpath('./div[2]/div/b').text
                    print(artist_small)
        except :
            pass

        time.sleep(1)

        kortheme_small = ''
        engtheme_small = ''
        engtrantheme_small = ''

        artist_small_all_artist = artist_small_all.find_elements_by_xpath('./div')

        for theme_xpath_i in artist_small_all_artist :
            try :
                print(theme_xpath_i.find_element_by_xpath('./h3').text.upper())
                if theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'KOREAN TITLE':
                    kortheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                    print(kortheme_small)
                elif theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'ENGLISH TITLE':
                    engtheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                    print(engtheme_small)
                elif theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'TRANSLATED TITLE':
                    engtrantheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                    print(engtrantheme_small)
            except :
                pass

        page_url = driver.current_url

        time.sleep(2)
        # mp3 다운
        sources = driver.find_elements_by_tag_name('audio')
        if sources ==[] :
            sources == ['']

        for source_i in sources :

            try :
                file_url = source_i.find_element_by_tag_name('source').get_attribute("src")
                file_names = source_i.get_attribute("data-mwtitle").replace('.ogg','')
            except :
                file_url = ''
                file_names = ''
            print(file_names)

            paths = './mp3_cover/'

            change_text = {"\\":'_',
                   "/":';',
                   ":":';',
                   "*":'+',
                   "?":'',
                   '"':"'",
                   "<":'[',
                   ">":']',
                   "|":'_'}

            file_names2 = copy.deepcopy(file_names)
            for change_text_i in change_text :
                file_names2 = file_names2.replace(change_text_i,change_text[change_text_i])

            try:
                error_type = 'normal'
                urllib.request.urlretrieve(file_url, filename = paths + file_names2+'.mp3')
            except Exception  as err  :
                error_type = str(err)

            my_dict = {"artist": [artist_small],
                       "theme": [theme_small],
                    "kor_theme": [kortheme_small],
                    "eng_theme" :[engtheme_small],
                       "trans_theme" :[engtrantheme_small],
                    "page_url": [page_url],
                    "mp3_url": [file_url],
                   "mp3_name_pre" : [file_names],
                    "mp3_name" : [file_names2],
                    "mp3_error" : [error_type]}

            info_small =  pd.DataFrame(my_dict)

            info_all = info_all.append(info_small).reset_index(drop=True)
            info_all.to_csv("./music_info_cover.csv",encoding='utf-8')

            time.sleep(2)

        c_url = driver.current_url

        time.sleep(2)
        driver.back()
        print("see2")
        driver.implicitly_wait(10)
        print("see3")
        print("cat_i2 : ",cat_i2)
        print("len(ksj2) : ",len(ksj2))

        cat_i2 = cat_i2 + 1
        if (cat_i2 >= len(ksj2))|((cat_i==0)&(cat_i2>=2)) :
            break

    print("cat_i : ",cat_i)
    print("len(ksjs) : ",len(ksjs))

    print("see4")
    c_url = driver.current_url

    cat_i = cat_i + 1

    if cat_i >= len(ksjs) :
        break_____no_output_____/html/body/main/section[1]/article/div/div/div[1]/aside/div[2]/div_____no_output_____driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside/div[2]/div').text_____no_output_____        driver.implicitly_wait(10)
        artist_small_all = driver.find_element_by_xpath('/html/body/main/section[1]/article/div/div/div[1]/aside')

        artist_small = ''

        try :
            artist_small_all_info = artist_small_all.find_elements_by_xpath('./section')
            for xpath_artist_numi, xpath_artist_i in enumerate(artist_small_all_info) :

                if xpath_artist_i.find_element_by_xpath('./div[1]/h2').text == 'COVER INFORMATION' :
                    xpath_artist_section = xpath_artist_numi

                try :
                    artist_small = artist_small_all_info[xpath_artist_section].find_element_by_xpath('./div[2]/div').text
                    print(artist_small)
                    if artist_small == '':
                        raise Exception('artist_small is empty')
                except :
                    artist_small = artist_small_all_info[xpath_artist_section].find_element_by_xpath('./div[2]/div/b').text
                    print(artist_small)
        except :
            pass

        kortheme_small = ''
        engtheme_small = ''
        engtrantheme_small = ''

        artist_small_all_artist = artist_small_all.find_elements_by_xpath('./div')

        for theme_xpath_i in artist_small_all_artist :
            try :
                print(theme_xpath_i.find_element_by_xpath('./h3').text.upper())
                if theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'KOREAN TITLE':
                    kortheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                    print(kortheme_small)
                elif theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'ENGLISH TITLE':
                    engtheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                    print(engtheme_small)
                elif theme_xpath_i.find_element_by_xpath('./h3').text.upper() == 'TRANSLATED TITLE':
                    engtrantheme_small = theme_xpath_i.find_element_by_xpath('./div').text
                    print(engtrantheme_small)
            except :
                pass

        page_url = driver.current_url

        time.sleep(2)
        # mp3 다운
        sources = driver.find_elements_by_tag_name('audio')
        if sources ==[] :
            sources == ['']RAISE A SUILEN
RAISE A SUILEN
ROMANIZED TITLE
KOREAN TITLE
브리키의 댄스
TRANSLATED TITLE
Tinplate's Dance