Class: Photocase

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

Overview

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?, #photosize_valid?

Constructor Details

#initialize(name) ⇒ Photocase



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

def initialize(name)
  super
  #    if ENV['MSP_CONFIG']
  #      @config = YAML.load_file(ENV['MSP_CONFIG'])
  #    else
  #      @config = YAML.load_file("config.yaml")
  #    end
  @config = load_config
  begin
    @user = @config[:photocase][:user]
    @password = @config[:photocase][:password]
    @total_per_day = @config[:photocase][:total_per_day]
  rescue
    puts 'cannot read configuration entries of '+SITENAME
  end
end

Instance Attribute Details

#max_errorsObject

Returns the value of attribute max_errors.



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

def max_errors
  @max_errors
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#total_per_dayObject (readonly)

Returns the value of attribute total_per_day.



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

def total_per_day
  @total_per_day
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.accept?(ext) ⇒ Boolean

Site akzeptiert diese Extension



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/multistockphoto/site_photocase.rb', line 176

def self.accept?(ext)
  ext = ext.downcase
  if ext[0,1] == '.'
    ext = ext[1..-1]
  end
  case ext
  when 'jpg','jpeg' #TODO: pruefen
    return true
  else
    return false
  end
end

.can_handle_orientation?Boolean

can site handle orientation?



171
172
173
# File 'lib/multistockphoto/site_photocase.rb', line 171

def self.can_handle_orientation?
  false
end

Instance Method Details

#accept?(ext) ⇒ Boolean



189
190
191
# File 'lib/multistockphoto/site_photocase.rb', line 189

def accept?(ext)
  Photocase.accept?(ext)
end

#already_sent?(photo) ⇒ Boolean

wurde dieses Photo schon gesendet?



166
167
168
# File 'lib/multistockphoto/site_photocase.rb', line 166

def already_sent?(photo)
  already_sent_site?(photo,SITENAME)
end

#sent_todayObject

Anzahl heute schon gesendeter Photos



160
161
162
163
# File 'lib/multistockphoto/site_photocase.rb', line 160

def sent_today
  site = SITENAME
  return sent_today_site(site)
end

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



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/multistockphoto/site_photocase.rb', line 29

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 "checking #{SITENAME}:#{photo.filename} ... "
    $stdout.flush
    if already_sent_site?(photo,SITENAME)
      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
      begin
        agent = WWW::Mechanize.new
        agent.user_agent_alias = 'Linux Mozilla'
        begin
          page = agent.get 'http://www.photocase.de/en/login.asp'
        rescue ArgumentError
          warn "ArgumentError error in mechanize-gem"
          warn "TODO: mechanize should fix this problem"
          warn "skipping file"
          raise UploadException
        end
        if !page.body.include?('Log in')
          raise UploadException, 'not at the login page'
        end
      end
      # form = page.forms.first #TODO: besser nach Name
      # form = page.forms[1] #TODO: es ist jetzt das zweite Formular
      form = page.forms[2] #TODO: es ist jetzt das dritte Formular
      #    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")
      if (! page.body.include?("Login successful")) and
                           (! page.body.include?("Einloggen erfolgreich"))
                        #2009-07-08 Fehlermeldung kommt jetzt in deutsch
                          
      # obiges kommt anscheinend seit 1.8.2008 nicht mehr auf der Seite.
      # mit folgendem ersetzt
      # 2009-06-26: scheint doch wieder zu kommen
      # if ! page.body.include?('<div id="loggedIn">')
        raise UploadException, "login error"
      end
      # old mechanize version: page = agent.click page.links.text('Upload')
      page = agent.click page.link_with(:text => 'Upload')
      if page.body.include?('Upload a photo') or
          page.body.include?('Foto hochladen')
        true # alles OK
      else
        raise UploadException, "no 'Upload a photo' on page"
      end
      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
      # old syntax of mechanize gem:       form.checkboxes.name('UploadDisclaimerAccepted').check
      form.checkbox_with(:name => 'UploadDisclaimerAccepted').check
      # old syntax of mechanize gem: form.checkboxes.name('HasCopyright').check
      form.checkbox_with(:name => 'HasCopyright').check
      page = agent.submit form
      #        if !page.body.include?('Your photo was uploaded successfully.') and
      #           !page.body.include?('Dein Foto wurde erfolgreich hochgeladen')
      #          raise "not successfully uploaded"
      #        end
      if page.body.include?('Your photo was uploaded successfully.') or
          page.body.include?('Dein Foto wurde erfolgreich hochgeladen')
        @@errors = 0
        puts "OK"
      else
        #Fehler aufgetreten
        @@errors += 1
        if @@errors > 3
          raise "too many errors"
        end
      end
#   rescue  Net::HTTPInternalServerError, WWW::Mechanize::ResponseCodeError
#     #         Net::HTTPInternalServerError (WWW::Mechanize::ResponseCodeError)
#     raise UploadException
#   rescue Iconv::IllegalSequence
#     warn "Iconv::IllegalSequence error in mechanize-gem"
#     warn "TODO: mechanize should fix this problem"
#     warn "skipping file"
#     raise UploadException
#   end # rescue
  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