Module: CommonsUpload

Defined in:
lib/commons_upload.rb,
lib/commons_upload/version.rb

Overview

Iterate over all images, create license, upload image by image

Constant Summary collapse

VERSION =
'1.2.1'.freeze

Class Method Summary collapse

Class Method Details

.edit(file_name, client) ⇒ Object



5
6
7
8
# File 'lib/commons_upload.rb', line 5

def self.edit(file_name, client)
  page = "File:#{file_name}"
  client.edit(title: page, text: license(file_name), summary: 'Update page text')
end

.image(file_path, client) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/commons_upload.rb', line 38

def self.image(file_path, client)
  file_name = File.basename(file_path, '')
  file_license = license(file_name)

  begin
    client.upload_image(
      file_name, file_path, 'Upload new version of the file', true, file_license
    )
    return 'OK'
  rescue MediawikiApi::ApiError => mwerr
    return 'exists: fileexists-no-change' if mwerr.code == 'fileexists-no-change'
    return 'exists: fileexists-shared-forbidden' if mwerr.code == 'fileexists-shared-forbidden'
    return "error: #{mwerr}"
  ensure
    edit(file_name, client) # update page content
    sleep 5 # Restriction in bot speed: https://commons.wikimedia.org/wiki/Commons:Bots#Bot_speed
  end
end

.imagesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/commons_upload.rb', line 57

def self.images
  screenshot_directory = ENV['LANGUAGE_SCREENSHOT_PATH'] || './screenshots'
  require 'mediawiki_api'
  client = MediawikiApi::Client.new ENV['MEDIAWIKI_API_UPLOAD_URL']
  client. ENV['MEDIAWIKI_USER'], ENV['MEDIAWIKI_PASSWORD']
  Dir["#{screenshot_directory}/*.png"].each do |file_path|
    print "Uploading #{file_path} ... "
    STDOUT.flush
    begin
      message = image file_path, client
      puts message
    rescue StandardError => e
      puts 'FAILED'
      raise e
    end
    STDOUT.flush
  end
end

.license(file_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/commons_upload.rb', line 10

def self.license(file_name)
  require 'date'
  date = Date.today.to_s

  # file_name example: VisualEditor_category_item-en.png
  # language_code is the portion of the string between dash (-) and dot (.)
  # in this case it is: en
  language_code = file_name.split('-')[1].split('.')[0]

  "    |=={{int:filedesc}}==\n    |{{Information\n    ||description={{en|1=\#{file_name}}}\n    ||date=\#{date}\n    ||source=[[User:LanguageScreenshotBot|Automatically created by LanguageScreenshotBot]]\n    ||author=[[User:LanguageScreenshotBot|Automatically created by LanguageScreenshotBot]]\n    ||permission=\n    ||other_versions=\n    ||other_fields=\n    |}}\n    |\n    |=={{int:license-header}}==\n    |{{Wikimedia-screenshot}}\n    |\n    |[[Category:VisualEditor-\#{language_code}]]\n".gsub(/^\s+\|/, '')
end