Class: Tuenti

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/tuenti.rb

Constant Summary collapse

LOGIN_URL =
'https://www.tuenti.com/?m=Login&func=do_login'
HOME_URL =
'http://www.tuenti.com/?m=home&func=view_home'
UPLOAD_URL =
'http://fotos.tuenti.com/?upload=1&iframe=1'
ALBUMS_URL =
'http://www.tuenti.com/?m=Search&func=get_user_custom_albums_for_data_source&ajax=1'
EDIT_PHOTO_URL =
'http://www.tuenti.com/?m=Photoedit&func=process_edit_photo&ajax=1'
CSFR_REGEXP =
/csfr(")?[:=](")?([0-9a-zA-Z]+)\b/
CSFR_REGEXP_INDEX =
3

Instance Method Summary collapse

Constructor Details

#initialize(user, password, timezone = Time.now.utc_offset/3600) ⇒ Tuenti

Returns a new instance of Tuenti.

Raises:

  • (Exception)


18
19
20
21
22
# File 'lib/tuenti.rb', line 18

def initialize(user, password, timezone = Time.now.utc_offset/3600)
  @agent = WWW::Mechanize.new
  raise Exception if @agent.post(LOGIN_URL, :email => user, :input_password => password, :timezone => timezone).uri.to_s == LOGIN_URL
  @csfr = @agent.get(HOME_URL).body.match(CSFR_REGEXP)[CSFR_REGEXP_INDEX]
end

Instance Method Details

#album_id(album) ⇒ Object



54
55
56
# File 'lib/tuenti.rb', line 54

def album_id(album)
  albums[album] || new_album_id(album) 
end

#albumsObject



47
48
49
50
51
# File 'lib/tuenti.rb', line 47

def albums
  JSON.parse(@agent.get(ALBUMS_URL).search('#request_data').text)["results"].inject({}) do |albums, result|
    albums.update(result["string"] => result["id"])
  end
end

#set_album(*photos) ⇒ Object



58
59
# File 'lib/tuenti.rb', line 58

def set_album(*photos)
end

#upload_photo(file, attributes = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tuenti.rb', line 24

def upload_photo(file, attributes = nil)
  qid = @agent.submit(upload_photo_form(file)).search('#request_data').text
  puts "QID: #{qid}" if DEBUG
  id = nil
  # el formato del id es album_id-user_id-photo_id-user_id
  while id.nil? || id =~ /-0-/ do # hasta que no se procesa photo_id es 0
    sleep(0.5)
    id = @agent.post(UPLOAD_URL, :func => 'checkq', :qid => qid).search('#request_data').text
  end
  puts "PHOTO ID: #{id}" if DEBUG
  if attributes
    options = {:csfr => @csfr, :'item_ids[]' => id, :from_collection_key => id, :photo_title => attributes[:title] || ''}
    if attributes[:album]
      options[:'add_albums_collection_keys[]'] = album_id(attributes[:album])
      #@new_albums_count = 0
      puts options[:'add_albums_collection_keys[]'] if DEBUG
    end
    @agent.post EDIT_PHOTO_URL + "&collection_key=#{id}", options
  end
  id
end