2

Working on discord bot that works in conjunction with the music bot, Rythm. Basically it tells you how many times a song has been played before by keeping track with a csv. An added feature is that it keeps track of songs played within the same month also stored within a csv. Code is as follows:

async def add_month_song(m, name):
    df = pd.read_csv('monthly_songs.csv', index_col=0)
    month_year = str((m.created_at - datetime.timedelta(hours=8)).month) + '-' + str((m.created_at - datetime.timedelta(hours=8)).year)
    append = pd.DataFrame([[month_year, name, 1]],
                          columns=["Month", "Song Name", "Count"])
    df = pd.concat([df, append], ignore_index=True)
    df.to_csv(r'monthly_songs.csv')

This works fine on local and I can see the csv's update within pycharm. But after I uploaded my bot to Heroku, I'll try to check the top songs with this command:

@client.command(aliases=["top", "month"])
async def top_month(ctx):
    curr_month = str((ctx.message.created_at - datetime.timedelta(hours=8)).month) + '-' + str((ctx.message.created_at - datetime.timedelta(hours=8)).year)
    month_data = pd.read_csv('monthly_songs.csv', index_col=0)
    counted = month_data[month_data['Month'].str.contains(curr_month)].groupby('Song Name', as_index=False
                ).sum()[['Song Name', 'Count']].sort_values('Count', ascending=False)[:5].reset_index(drop=True)
    await ctx.message.channel.send(f'The top 5 songs of {calendar.month_name[(ctx.message.created_at - datetime.timedelta(hours=8)).month]} '
                                   f'{(ctx.message.created_at - datetime.timedelta(hours=8)).year}:')
    for row in np.arange(len(counted)):
        await ctx.message.channel.send(f'{row + 1}: {counted.loc[row][0]} - {str(counted.loc[row][1])}')

But it'll return info from the csv from when I first uploaded the bot, not the csv that should've been updated since its been run on Heroku. I've even tried to download the files from Heroku from the instructions of https://stackoverflow.com/a/10997885/14894231 but my files are exactly the same from when I first git pushed my code (no csv's updated).

However, the .top_month command will show the correct info if I run it right after I play a lot of new songs. But after a few hours the command will show the wrong info. I'm thinking its cause Heroku refreshes the csv's every time a certain amount of time has passed and that's why the csv's go back to the info I pushed.

First few lines of code for the monthly_songs.csv:

,Month,Song Name,Count
0,12-2020,YOASOBI - Racing Into The Night Jap_Rom_Eng,1
1,12-2020,Fukashigi no Carte FULL- 1 hour,1
2,12-2020,YOASOBI - Racing Into The Night Jap_Rom_Eng,1
3,12-2020,Kamisama Hajimemashita Opening-English Sub,1
4,12-2020,YOASOBI - Racing Into The Night Yoru ni Kakeru  THE HOME TAKE,1
5,12-2020,Bazzi - 3:15,1
6,12-2020,Bazzi - I.F.L.Y.,1

Main problems: code works on local. Not with Heroku. Csv's don't get updated. Is there a more efficient way of updating the info in the csv? I've tried txt files but its not easy to read data. Does Heroku not allow files to change once they're in the git repo? Is there another way of downloading code that should show updated csv files? Is there a better hosting service I should use instead?

Any help is appreciated and thank you in advance!

Elton
  • 21
  • 2

0 Answers0