Skip to content

Commit

Permalink
Added uploading of screenshots and images
Browse files Browse the repository at this point in the history
Added auto-clearing of existing screenshots
Added automatic creation of screenshots on setup
Added downloading of existing images
  • Loading branch information
KrauseFx committed Sep 22, 2015
1 parent 8b5fc67 commit 700401a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions lib/supply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 38 additions & 1 deletion lib/supply/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

Expand Down
43 changes: 26 additions & 17 deletions lib/supply/setup.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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
60 changes: 46 additions & 14 deletions lib/supply/uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 700401a

Please sign in to comment.