<commit_before><commit_msg>Starting photoalbum class for mediatopic<commit_after>require 'net/http'
require 'net/http/post/multipart'
require 'http_uploader'

require_relative '../odnoklassniki'

module Social
  module Content
    class Odnoklassniki
      class Photoalbum

        GET_ALBUMS_METHOD = 'photos.getAlbums'.freeze
        CREATE_ALBUM_METHOD = 'photos.createAlbum'.freeze
        GET_ALBUM_UPLOAD_URL_METHOD = 'photosV2.getUploadUrl'.freeze
        COMMIT_PHOTO_METHOD = 'photosV2.commit'.freeze

        ALBUM_NAME = 'amplifr'.freeze
        ALBUM_CREATION_OPTIONS = {
          title: ALBUM_NAME,
          description: 'Album for uploads from amplifr app'.freeze,
          type: 'public'.freeze
        }.freeze

        def initialize(account)
          @account = account
          @api = API::Odnoklassniki.new(account)
        end

        def upload(photo)
          upload_photoalbum_photo(photo)
        end

        private

        Error = Class.new(StandardError)
        FindingError = Class.new(Error)
        CreationError = Class.new(Error)
        UploadingError = Class.new(Error)

        def upload_photoalbum_photo(photo)
          upload_response = ::HttpUploader.new(photoalbum_upload_url)
            .upload(photo.to_io,
                    photo.basename,
                    query_param: :pic1, content_type: photo.content_type)

          unless Net::HTTPSuccess === upload_response
            raise UploadingError, "Failed to upload file. Reason: #{upload_response.body}"
          end

          photo_id, photo_attributes = JSON.parse(upload_response.body)
            .try(:[], 'photos').try(:flatten)

          if photo_id.blank? || photo_attributes.blank?
            raise UploadingError, "Broken upload response. Response: #{upload_response.body}"
          end

          if @account.personal?
            commit_uploaded_photo(photo_id, photo_attributes['token'])
          else
            photo_attributes['token']
          end
        end

        def photoalbum
          @photoalbum ||= begin
            params = {method: GET_ALBUMS_METHOD}
            params.merge!(gid: @account.group_id) if @account.group?

            @api.api(:get, params).try(:[], 'albums').to_a
              .find { |album| album['title'] == ALBUM_NAME }
          rescue API::Error
            raise FindingError
          end
        end

        def create_photoalbum
          return photoalbum['aid'] if photoalbum.present?

          params = {method: CREATE_ALBUM_METHOD}.merge!(ALBUM_CREATION_OPTIONS)
          params.merge!(gid: @account.group_id) if @account.group?

          @api.api(:get, params)
        rescue API::Error
          raise CreationError
        end

        def photoalbum_upload_url
          params = {method: GET_ALBUM_UPLOAD_URL_METHOD}

          if @account.group?
            params.merge!(gid: @account.group_id)
          else
            params.merge!(aid: create_photoalbum)
          end

          @api.api(:get, params) { |json| URI.parse json['upload_url'] }
        end

        def commit_uploaded_photo(photo_id, photo_token)
          params = {method: COMMIT_PHOTO_METHOD, photo_id: photo_id, token: photo_token}
          @api.api(:get, params) do |commit_response|
            commit_response['photos'][0]['assigned_photo_id']
          end
        end

      end # class Photoalbum
    end # class Odnoklassniki
  end # module Content
end # module Social

{'fraction_non_alphanumeric': 0.09251740139211137, 'fraction_numerical': 0.001160092807424594, 'mean_word_length': 2.2053903345724906, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5758053', 'n_tokens_mistral': 1030, 'n_tokens_neox': 993, 'n_words': 201}