From 700401ab03b18ab8bcf1009aeee42823a27217a0 Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Mon, 21 Sep 2015 18:15:13 -0700 Subject: [PATCH] Added uploading of screenshots and images Added auto-clearing of existing screenshots Added automatic creation of screenshots on setup Added downloading of existing images --- README.md | 2 +- lib/supply.rb | 5 ++++ lib/supply/client.rb | 39 ++++++++++++++++++++++++++- lib/supply/setup.rb | 43 ++++++++++++++++++------------ lib/supply/uploader.rb | 60 ++++++++++++++++++++++++++++++++---------- 5 files changed, 116 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index f4035df..2a93ece 100755 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ Note that these will replace the current images and screenshots on the play stor - [`spaceship`](https://github.com/fastlane/spaceship): Ruby library to access the Apple Dev Center and iTunes Connect - [`pilot`](https://github.com/fastlane/pilot): The best way to manage your TestFlight testers and builds from your terminal - [`boarding`](https://github.com/fastlane/boarding): The easiest way to invite your TestFlight beta testers - +- [`gym`](https://github.com/fastlane/gym): Building your iOS apps has never been easier ##### [Like this tool? Be the first to know about updates and new fastlane tools](https://tinyletter.com/krausefx) diff --git a/lib/supply.rb b/lib/supply.rb index f232a3b..685d6c9 100755 --- a/lib/supply.rb +++ b/lib/supply.rb @@ -14,6 +14,11 @@ class << self end AVAILABLE_METADATA_FIELDS = %w(title short_description full_description video) + IMAGES_TYPES = %w(featureGraphic icon promoGraphic tvBanner) + SCREENSHOT_TYPES = %w(phoneScreenshots sevenInchScreenshots tenInchScreenshots tvScreenshots wearScreenshots) + + IMAGES_FOLDER_NAME = "images" + IMAGE_FILE_EXTENSIONS = "{png,jpg,jpeg}" Helper = FastlaneCore::Helper # you gotta love Ruby: Helper.* should use the Helper class contained in FastlaneCore end diff --git a/lib/supply/client.rb b/lib/supply/client.rb index ec2996f..f3c798b 100644 --- a/lib/supply/client.rb +++ b/lib/supply/client.rb @@ -231,7 +231,7 @@ def upload_apk_to_track(path_to_apk, track) # @!group Screenshots ##################################################### - def fetch_screenshots(image_type: nil, language: nil) + def fetch_images(image_type: nil, language: nil) ensure_active_edit! result = api_client.execute( @@ -250,6 +250,43 @@ def fetch_screenshots(image_type: nil, language: nil) result.data.images.collect(&:url) end + # @param image_type (e.g. phoneScreenshots, sevenInchScreenshots, ...) + def upload_image(image_path: nil, image_type: nil, language: nil) + ensure_active_edit! + + image = Google::APIClient::UploadIO.new(image_path, 'image/*') + result = api_client.execute( + api_method: android_publisher.edits.images.upload, + parameters: { + 'editId' => current_edit.data.id, + 'packageName' => current_package_name, + 'language' => language, + 'imageType' => image_type, + 'uploadType' => 'media' + }, + media: image, + authorization: auth_client + ) + + raise result.error_message.red if result.error? + end + + def clear_screenshots(image_type: nil, language: nil) + ensure_active_edit! + + result = @api_client.execute( + api_method: @android_publisher.edits.images.deleteall, + parameters: { + 'editId' => current_edit.data.id, + 'packageName' => current_package_name, + 'language' => language, + 'imageType' => image_type + }, + authorization: auth_client + ) + + raise result.error_message if result.error? + end private diff --git a/lib/supply/setup.rb b/lib/supply/setup.rb index d64a7d1..eb235c3 100755 --- a/lib/supply/setup.rb +++ b/lib/supply/setup.rb @@ -1,12 +1,17 @@ module Supply class Setup def perform_download + if File.exist?(metadata_path) + Helper.log.info "Metadata already exists at path '#{metadata_path}'".yellow + return + end + client.begin_edit(package_name: Supply.config[:package_name]) client.listings.each do |listing| store_metadata(listing) - - store_screenshots(listing.language) + create_screenshots_folder(listing) + download_images(listing) end client.abort_current_edit @@ -25,23 +30,27 @@ def store_metadata(listing) end end - def store_screenshots(language) - Supply::SCREENSHOT_TYPES.each do |image_type| - urls = client.fetch_screenshots(image_type: image_type, - language: language) + def download_images(listing) + # We cannot download existing screenshots as they are compressed + # But we can at least download the images + require 'net/http' - urls.each_with_index do |url, index| - if image_type.include?("Screenshots") - FileUtils.mkdir_p(File.join(metadata_path, language, image_type)) - path = File.join(metadata_path, language, image_type, "#{index}.png") - else - path = File.join(metadata_path, language, "#{image_type}.png") - end + IMAGES_TYPES.each do |image_type| + url = client.fetch_images(image_type: image_type, language: listing.language).last + next unless url - Helper.log.info "Writing to #{path}..." + Helper.log.info "Downloading #{image_type} for #{listing.language}..." + path = File.join(metadata_path, listing.language, IMAGES_FOLDER_NAME, "#{image_type}.png") + File.write(path, Net::HTTP.get(URI.parse(url))) + end + end + + def create_screenshots_folder(listing) + containing = File.join(metadata_path, listing.language) - File.write(path, Net::HTTP.get(URI(url))) - end + FileUtils.mkdir_p(File.join(containing, IMAGES_FOLDER_NAME)) + Supply::SCREENSHOT_TYPES.each do |screenshot_type| + FileUtils.mkdir_p(File.join(containing, IMAGES_FOLDER_NAME, screenshot_type)) end end @@ -55,7 +64,7 @@ def metadata_path def client @client ||= Client.new(path_to_key: Supply.config[:key], - issuer: Supply.config[:issuer]) + issuer: Supply.config[:issuer]) end end end diff --git a/lib/supply/uploader.rb b/lib/supply/uploader.rb index a302681..3224fd9 100644 --- a/lib/supply/uploader.rb +++ b/lib/supply/uploader.rb @@ -3,28 +3,60 @@ class Uploader def perform_upload client.begin_edit(package_name: Supply.config[:package_name]) - # Metadata - load_local_metadata + Dir.foreach(metadata_path) do |language| + next if language.start_with?('.') # e.g. . or .. or hidden folders - # pkg - upload_binary + listing = client.listing_for_language(language) + + upload_metadata(language, listing) + upload_images(language) + upload_screenshots(language) + end - Helper.log.info "Uploading changes to Google Play..." + # upload_binary + + Helper.log.info "Uploading all changes to Google Play..." client.commit_current_edit! Helper.log.info "Successfully finished the upload to Google Play".green end - def load_local_metadata - Dir.foreach(metadata_path) do |language| - next if language.start_with?('.') # e.g. . or .. or hidden folders - Helper.log.info "Loading metadata for language '#{language}'..." + def upload_metadata(language, listing) + Helper.log.info "Loading metadata for language '#{language}'..." - listing = client.listing_for_language(language) - Supply::AVAILABLE_METADATA_FIELDS.each do |key| - path = File.join(metadata_path, language, "#{key}.txt") - listing.send("#{key}=".to_sym, File.read(path)) if File.exist?(path) + Supply::AVAILABLE_METADATA_FIELDS.each do |key| + path = File.join(metadata_path, language, "#{key}.txt") + listing.send("#{key}=".to_sym, File.read(path)) if File.exist?(path) + end + listing.save + end + + def upload_images(language) + Supply::IMAGES_TYPES.each do |image_type| + search = File.join(metadata_path, language, Supply::IMAGES_FOLDER_NAME, image_type) + ".#{IMAGE_FILE_EXTENSIONS}" + path = Dir.glob(search, File::FNM_CASEFOLD).last + next unless path + + Helper.log.info "Uploading image file #{path}..." + client.upload_image(image_path: File.expand_path(path), + image_type: image_type, + language: language) + end + end + + def upload_screenshots(language) + Supply::SCREENSHOT_TYPES.each do |screenshot_type| + search = File.join(metadata_path, language, Supply::IMAGES_FOLDER_NAME, screenshot_type, "*.#{IMAGE_FILE_EXTENSIONS}") + paths = Dir.glob(search, File::FNM_CASEFOLD) + next unless paths.count > 0 + + client.clear_screenshots(image_type: screenshot_type, language: language) + + paths.each do |path| + Helper.log.info "Uploading screenshot #{path}..." + client.upload_image(image_path: File.expand_path(path), + image_type: screenshot_type, + language: language) end - listing.save end end