<commit_before># coding: utf-8
# frozen_string_literal: true

class SpotifyMixPlaylistUpdater < ApplicationJob
  queue_as :default

  def perform(*args)
    logger.info("SpotifyMixPlaylistUpdater start")
    email    = args[0]
    topic_id = args[1]
    name     = args[2]
    user     = User.find_by(email: email)
    topic    = Topic.find(topic_id)
    playlist = SpotifyMixPlaylistUpdater.update_playlist(user, topic, name)
    logger.info("Update playlist #{playlist.name}")
    logger.info("SpotifyMixPlaylistUpdater end")
    playlist
  end

  def self.update_playlist(user, topic, name)
    authentication = user.spotify_authentication
    spotify_user   = authentication.spotify_user
    playlist_name  = name
    playlist       = find_or_create_spotify_playlist(spotify_user, playlist_name)
    Rails.logger.info("#{playlist_name} is created")
    tracks = chart_tracks(topic).select do |track|
      track.provider == "Spotify"
    end
    clear_playlist(playlist)
    add_tracks_to_spotify_playlist(playlist, tracks)
    authentication.update(credentials: spotify_user.credentials.to_json)
    playlist
  end

  def self.chart_tracks(topic)
    today            = Time.zone.now.beginning_of_day
    week_ago         = today - 7.days
    entries_per_feed = Setting.latest_entries_per_feed
    query = Mix::Query.new(week_ago..today, :engaging,
                           locale: "ja",
                           entries_per_feed: entries_per_feed)
    tracks = topic.mix_enclosures(Track, page: 1, per_page: 100, query: query)
    Track.set_contents(tracks)
    tracks
  end

  def self.clear_playlist(playlist)
    tracks = playlist.tracks(offset: 0, limit: 100)
    playlist.remove_tracks!(tracks)
  end

  def self.add_tracks_to_spotify_playlist(playlist, tracks)
    tracks.map { |t| t.content["identifier"] }.in_groups_of(50, false) do |ids|
      spotify_tracks = RSpotify::Track.find(ids)
      playlist.add_tracks!(spotify_tracks)
    end
  end

  def self.find_or_create_spotify_playlist(user, name)
    playlist = user.playlists(offset: 0, limit: 50).find { |v| v.name == name }
    return playlist if playlist.present?
    user.create_playlist!(name)
  end
end
<commit_msg>Don't specify locale for bot<commit_after># coding: utf-8
# frozen_string_literal: true

class SpotifyMixPlaylistUpdater < ApplicationJob
  queue_as :default

  def perform(*args)
    logger.info("SpotifyMixPlaylistUpdater start")
    email    = args[0]
    topic_id = args[1]
    name     = args[2]
    user     = User.find_by(email: email)
    topic    = Topic.find(topic_id)
    playlist = SpotifyMixPlaylistUpdater.update_playlist(user, topic, name)
    logger.info("Update playlist #{playlist.name}")
    logger.info("SpotifyMixPlaylistUpdater end")
    playlist
  end

  def self.update_playlist(user, topic, name)
    authentication = user.spotify_authentication
    spotify_user   = authentication.spotify_user
    playlist_name  = name
    playlist       = find_or_create_spotify_playlist(spotify_user, playlist_name)
    Rails.logger.info("#{playlist_name} is created")
    tracks = chart_tracks(topic).select do |track|
      track.provider == "Spotify"
    end
    clear_playlist(playlist)
    add_tracks_to_spotify_playlist(playlist, tracks)
    authentication.update(credentials: spotify_user.credentials.to_json)
    playlist
  end

  def self.chart_tracks(topic)
    today            = Time.zone.now.beginning_of_day
    week_ago         = today - 7.days
    entries_per_feed = Setting.latest_entries_per_feed
    query = Mix::Query.new(week_ago..today, :engaging, entries_per_feed: entries_per_feed)
    tracks = topic.mix_enclosures(Track, page: 1, per_page: 100, query: query)
    Track.set_contents(tracks)
    tracks
  end

  def self.clear_playlist(playlist)
    tracks = playlist.tracks(offset: 0, limit: 100)
    playlist.remove_tracks!(tracks)
  end

  def self.add_tracks_to_spotify_playlist(playlist, tracks)
    tracks.map { |t| t.content["identifier"] }.in_groups_of(50, false) do |ids|
      spotify_tracks = RSpotify::Track.find(ids)
      playlist.add_tracks!(spotify_tracks)
    end
  end

  def self.find_or_create_spotify_playlist(user, name)
    playlist = user.playlists(offset: 0, limit: 50).find { |v| v.name == name }
    return playlist if playlist.present?
    user.create_playlist!(name)
  end
end

{'fraction_non_alphanumeric': 0.09665513264129182, 'fraction_numerical': 0.008304498269896194, 'mean_word_length': 3.6275346851654215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3940242', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1448, 'n_words': 272}