2018-11-24 18:29:24 +03:00
import sys , os , datetime , re , cleanup
2018-11-28 19:45:52 +03:00
from cleanup import *
2018-11-29 18:05:44 +03:00
from utilities import *
2018-11-24 18:29:24 +03:00
2018-11-29 18:05:44 +03:00
log = open ( sys . argv [ 1 ] )
2018-11-29 15:43:45 +03:00
outputlog = open ( sys . argv [ 2 ] , " w " )
2018-12-20 20:46:55 +03:00
checksumfile = open ( sys . argv [ 2 ] + " .rulestate " , " w " ) #this file stores an identifier for all rules that were in place when the corresponding file was created
2018-11-24 18:29:24 +03:00
2018-11-29 18:05:44 +03:00
2018-11-28 19:45:52 +03:00
c = CleanerAgent ( )
2018-11-29 18:05:44 +03:00
stamps = [ 99999999999999 ]
2018-11-28 19:45:52 +03:00
2018-11-24 18:29:24 +03:00
for l in log :
l = l . replace ( " \n " , " " )
data = l . split ( " , " )
artist = data [ 0 ]
album = data [ 1 ]
title = data [ 2 ]
time = data [ 3 ]
2018-11-28 19:45:52 +03:00
( artists , title ) = c . fullclean ( artist , title )
2018-11-24 18:29:24 +03:00
2018-11-26 14:55:17 +03:00
artistsstr = " ␟ " . join ( artists )
2018-11-24 18:29:24 +03:00
timeparts = time . split ( " " )
( h , m ) = timeparts [ 3 ] . split ( " : " )
months = { " Jan " : 1 , " Feb " : 2 , " Mar " : 3 , " Apr " : 4 , " May " : 5 , " Jun " : 6 , " Jul " : 7 , " Aug " : 8 , " Sep " : 9 , " Oct " : 10 , " Nov " : 11 , " Dec " : 12 }
timestamp = int ( datetime . datetime ( int ( timeparts [ 2 ] ) , months [ timeparts [ 1 ] ] , int ( timeparts [ 0 ] ) , int ( h ) , int ( m ) ) . timestamp ( ) )
2018-11-29 18:05:44 +03:00
## We prevent double timestamps in the database creation, so we technically don't need them in the files
2018-12-20 20:46:55 +03:00
## however since the conversion from lastfm to maloja is a one-time thing, we should take any effort to make the file as good as possible
2018-11-29 18:05:44 +03:00
if ( timestamp < stamps [ - 1 ] ) :
pass
elif ( timestamp == stamps [ - 1 ] ) :
timestamp - = 1
else :
while ( timestamp in stamps ) :
timestamp - = 1
if ( timestamp < stamps [ - 1 ] ) :
stamps . append ( timestamp )
else :
stamps . insert ( 0 , timestamp )
2018-11-26 14:55:17 +03:00
entry = " \t " . join ( [ str ( timestamp ) , artistsstr , title , album ] )
2019-03-11 16:49:29 +03:00
entry = entry . replace ( " # " , r " \ num " )
2018-11-24 18:29:24 +03:00
outputlog . write ( entry )
outputlog . write ( " \n " )
2018-12-20 20:46:55 +03:00
checksumfile . write ( c . checksums )
log . close ( )
outputlog . close ( )
checksumfile . close ( )
2018-11-24 18:29:24 +03:00