tsv.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import csv
  2. import logging
  3. from datetime import datetime
  4. import pytz
  5. from music.models import Album, Artist, Track
  6. from scrobbles.models import Scrobble
  7. logger = logging.getLogger(__name__)
  8. def process_audioscrobbler_tsv_file(file_path):
  9. """Takes a path to a file of TSV data and imports it as past scrobbles"""
  10. new_scrobbles = []
  11. with open(file_path) as infile:
  12. source = 'Audioscrobbler File'
  13. rows = csv.reader(infile, delimiter="\t")
  14. source_id = ""
  15. for row_num, row in enumerate(rows):
  16. if row_num in [0, 1, 2]:
  17. source_id += row[0] + "\n"
  18. continue
  19. if len(row) > 8:
  20. logger.warning(
  21. 'Improper row length during Audioscrobbler import',
  22. extra={'row': row},
  23. )
  24. continue
  25. artist, artist_created = Artist.objects.get_or_create(name=row[0])
  26. if artist_created:
  27. logger.debug(f"Created artist {artist}")
  28. else:
  29. logger.debug(f"Found artist {artist}")
  30. album = None
  31. album_created = False
  32. albums = Album.objects.filter(name=row[1])
  33. if albums.count() == 1:
  34. album = albums.first()
  35. else:
  36. for potential_album in albums:
  37. if artist in album.artist_set.all():
  38. album = potential_album
  39. if not album:
  40. album_created = True
  41. album = Album.objects.create(name=row[1])
  42. album.save()
  43. album.artists.add(artist)
  44. if album_created:
  45. logger.debug(f"Created album {album}")
  46. else:
  47. logger.debug(f"Found album {album}")
  48. track, track_created = Track.objects.get_or_create(
  49. title=row[2],
  50. artist=artist,
  51. album=album,
  52. )
  53. if track_created:
  54. logger.debug(f"Created track {track}")
  55. else:
  56. logger.debug(f"Found track {track}")
  57. if track_created:
  58. track.musicbrainz_id = row[7]
  59. track.save()
  60. timestamp = datetime.utcfromtimestamp(int(row[6])).replace(
  61. tzinfo=pytz.utc
  62. )
  63. source = 'Audioscrobbler File'
  64. new_scrobble = Scrobble(
  65. timestamp=timestamp,
  66. source=source,
  67. source_id=source_id,
  68. track=track,
  69. played_to_completion=True,
  70. in_progress=False,
  71. )
  72. existing = Scrobble.objects.filter(
  73. timestamp=timestamp, track=track
  74. ).first()
  75. if existing:
  76. logger.debug(f"Skipping existing scrobble {new_scrobble}")
  77. continue
  78. logger.debug(f"Queued scrobble {new_scrobble} for creation")
  79. new_scrobbles.append(new_scrobble)
  80. created = Scrobble.objects.bulk_create(new_scrobbles)
  81. logger.info(
  82. f"Created {len(created)} scrobbles",
  83. extra={'created_scrobbles': created},
  84. )