-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathodnoklassniki_client.rb
170 lines (137 loc) · 3.65 KB
/
odnoklassniki_client.rb
1
2
3
4
5
6
7
8
9
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
#encoding: UTF-8
require 'watir-webdriver'
require 'open-uri'
require 'watir-webdriver/wait'
class OdnoklassnikiClient
@browser
@siteUrl
def initialize(siteUrl = 'http://ok.ru')
@siteUrl = siteUrl
open_browser
end
def open_browser
@browser = Watir::Browser.new :chrome # :chrome or :firefox
@browser.goto @siteUrl
end
def login(user_name, password)
@browser.goto @siteUrl
@usr_field = @browser.text_field(:id, 'field_email')
@pwd_field = @browser.text_field(:id, 'field_password')
@usr_field.when_present.set(user_name)
@pwd_field.set(password)
@browser.input(:type, 'submit').click
@browser.span(:id, 'portal-headline_login').wait_until_present
return self
end
def profile(profile_id)
profile_url = get_url(profile_id)
return OdnoklassnikiProfilePage.new(@browser, profile_url)
end
def close
@browser.close
end
def get_url(action)
return @siteUrl + '/' + action;
end
end
class OdnoklassnikiProfilePage
def initialize(browser, profileUrl)
@browser = browser
@profileUrl = profileUrl
end
def open
@browser.goto @profileUrl
return self
end
def albums
@browser.goto get_url('albums')
@albums = Array.new
@browser.divs(:class, 'photo-sc_grid_i_alb-t').each { |albumDiv|
title = albumDiv.a().title()
url = albumDiv.a().href()
@albums.push(OdnoklassnikiAlbumPage.new(@browser, url, title))
}
return @albums
end
def all_photos
return OdnoklassnikiAlbumPage.new(@browser, get_url('photos'), 'Все фото')
end
def get_url(action)
return @profileUrl + '/' + action;
end
end
class OdnoklassnikiAlbumPage
def initialize(browser, albumUrl, title)
@browser = browser
@albumUrl = albumUrl
@title = title
end
def open
@browser.goto @albumUrl
return self
end
def show_first_photo
first_image_in_album = @browser.img(:class, 'photo-sc_i_cnt_a_img va_target')
first_image_in_album.when_present.click
end
def get_photo(i = 0)
main_image = @browser.img(:class, 'plp_photo')
main_image.wait_until_present
photo_description = @browser.span(:id => 'plp_descrCntText', :class => 'plp_descrCntText').text
return PhotoSource.new(@title, photo_description, i, main_image.src)
end
def move_to_next_photo
sleep 0.2
@browser.div(:id, 'plp_slide_r').hover
@browser.div(:id, 'plp_slide_r').when_present.click #clicking next button
end
def save_to(workspace)
show_first_photo
first_photo = get_photo
i=1
loop do
photo = get_photo(i)
workspace.save_photo(photo)
move_to_next_photo
i = i + 1
next_photo = get_photo
break if next_photo.url === first_photo.url
end
end
end
class PhotoSource
attr_accessor :album, :url, :number
def initialize(album, description, number, url)
@album = album
@description = description
@number = number
@url = url
end
def name
formatNumber = @number.to_s.rjust(3, '0')
return "#{formatNumber} #{@description}"
end
end
class Workspace
def initialize(rootPath)
@rootPath = rootPath
end
def prepare_album(albumName)
albumPath = @rootPath + '\\Photos\\' + normalize(albumName)
FileUtils.mkdir_p albumPath
return albumPath
end
def normalize(path)
return path.gsub(/[<>?:"\/\\?*\.]/, '')
.gsub("\n", '')
.to_s[0..255]
end
def save_photo(source)
albumPath = prepare_album(source.album)
#photoName = normalize(source.name)
photoName = source.number.to_s.rjust(3, '0')
File.open(albumPath + "\\" + photoName + '.jpg', 'wb') do |file|
file.write open(source.url).read
end
end
end