Class: Photocase

Inherits:
GenericSite show all
Defined in:
lib/multistockphoto/site_photocase.rb

Constant Summary collapse

SITENAME =
'photocase'
@@errors =
0

Constants inherited from GenericSite

GenericSite::MAX_ERRORS, GenericSite::SENDLIST

Instance Attribute Summary collapse

Attributes inherited from GenericSite

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GenericSite

#already_sent_site?, #heute_schon_gesendet, #photos_fuer_heute_uebrig?

Constructor Details

#initialize(name) ⇒ Photocase

Returns a new instance of Photocase.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/multistockphoto/site_photocase.rb', line 13

def initialize(name)
  super
  if ENV['MSP_CONFIG']
    @config = YAML.load_file(ENV['MSP_CONFIG'])
  else
    @config = YAML.load_file("config.yaml")
  end
  @user = @config[:photocase][:user]
  @password = @config[:photocase][:password]
  @total_per_day = @config[:photocase][:total_per_day]
end

Instance Attribute Details

#max_errorsObject

Returns the value of attribute max_errors.



11
12
13
# File 'lib/multistockphoto/site_photocase.rb', line 11

def max_errors
  @max_errors
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/multistockphoto/site_photocase.rb', line 11

def password
  @password
end

#total_per_dayObject (readonly)

Returns the value of attribute total_per_day.



10
11
12
# File 'lib/multistockphoto/site_photocase.rb', line 10

def total_per_day
  @total_per_day
end

#userObject

Returns the value of attribute user.



11
12
13
# File 'lib/multistockphoto/site_photocase.rb', line 11

def user
  @user
end

Class Method Details

.can_handle_orientation?Boolean

can site handle orientation?

Returns:

  • (Boolean)


137
138
139
# File 'lib/multistockphoto/site_photocase.rb', line 137

def self.can_handle_orientation?
  false
end

Instance Method Details

#already_sent?(photo) ⇒ Boolean

wurde dieses Photo schon gesendet?

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/multistockphoto/site_photocase.rb', line 123

def already_sent?(photo)
  already_sent_site?(photo,SITENAME)
  #
  #    unless photo.class == Photo
  #      raise 'not a Photo object'
  #    end
  #    unless File.exist?(photo.filename)
  #      raise "file #{photo.filename} does not exist"
  #    end
  #    g = grep(SENDLIST, /photocase\t#{photo.filename}/)
  #    return g.size > 0
end

#sent_todayObject

Anzahl heute schon gesendeter Photos



117
118
119
120
# File 'lib/multistockphoto/site_photocase.rb', line 117

def sent_today
  site = SITENAME
  return sent_today_site(site)
end

#transfer(photo, dont_send = false, dont_log = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/multistockphoto/site_photocase.rb', line 25

def transfer(photo,dont_send=false,dont_log=false)
  sav_photo_filename = photo.filename.dup
  # falls nicht in config Datei eingetragen dann nicht senden
  if @user == nil or @password == nil
    return
  end
  
  unless photo.class == Photo
    raise 'not a Photo object'
  end
  unless File.exist?(photo.filename)
    raise "file #{photo.filename} does not exist"
  end
  print "#{SITENAME}:#{photo.filename} ... "
  $stdout.flush
  g = grep(SENDLIST, /#{SITENAME}\t#{photo.filename}/)
  if g.size > 0
    puts 'already sent'
    return :duplicate
  end
  if photo.portrait? and ! Photocase.can_handle_orientation?
    photo.drehen
  end
  if site_can_handle_keywords?
    photo.set_keywords
  end

  if ! dont_send
    agent = WWW::Mechanize.new
    agent.user_agent_alias = 'Linux Mozilla'
    page = agent.get 'http://www.photocase.de/en/login.asp'
    if !page.body.include?('Log in')
      raise 'not at the login page'
    end
    form = page.forms.first #TODO: besser nach Name
    #    form['loginForm[Username]']  = @config[:zoonar][:user]
    #    form['loginForm[Password]']  = @config[:zoonar][:password] 
    form['UserName']  = @config[:photocase][:user]
    form['UserPassword']  = @config[:photocase][:password] 
    #UserName
    #UserPassword
    page = agent.submit form
    raise "login error" if ! page.body.include?("Login successful")
    page = agent.click page.links.text('Upload')
    raise "no Upload a photo" if ! page.body.include?('Upload a photo')
    form = page.form('Form')
    if photo.portrait?
      form.file_uploads.first.file_name = photo.rotated_filename
      #        b = File.basename(photo.filename)
      #        newfilename = photo.filename.sub(b,'')+'rot_'+b
      #        #      form.file_uploads.first.file_name = 'r' + photo.filename
      #        form.file_uploads.first.file_name = newfilename
    else
      form.file_uploads.first.file_name = photo.filename
    end
    #TODO:
    # 2 Checkboxen
    #UploadDisclaimerAccepted
    #HasCopyright
    form.checkboxes.name('UploadDisclaimerAccepted').check
    form.checkboxes.name('HasCopyright').check
    page = agent.submit form
    if !page.body.include?('Your photo was uploaded successfully.')
      raise "not successfully uploaded"
    end
    if page.body.include?('Your photo was uploaded successfully.')
      @@errors = 0
      puts "OK"
    else
      #Fehler aufgetreten
      @@errors += 1
      if @@errors > 3
        raise "too many errors"
      end
    end
  else
    puts "OK"
  end
  #'Your photo was uploaded successfully.'
  if sav_photo_filename != photo.filename
    raise "photo.filenames has been changed!"
  end
  
  unless dont_log
    File.open(SENDLIST,'a') {|f|
      f.puts "#{SITENAME}\t#{photo.filename}\t#{Time.now}"
    }
  end
  true
end