tomosia_icon8_crawl 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/VERSION +1 -0
- data/lib/tomosia_icon8_crawl.rb +138 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ff6c8e1adf3d370c5c91f360d44c8b58e699e65d9b0f017d5d29dfea9f801e7a
|
4
|
+
data.tar.gz: d41ffa7c2bf60dca8936d7ec6259dce7a641c3b168c0f3b0bc0f67156e98ef58
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6afef9f8fb017509db4ee57797fc55f68c0bd3b6e0d7e87a5e78e5b28c3077f54c97b5ad4deb75b3053d282cc770379186e41639262a5b69de75494f8806535b
|
7
|
+
data.tar.gz: afc87f64e8407d106bfb6378762a0ac98d8b5a4a4a6df8dbfda441e8ac3db80cd4823cfaa0c3046ec46c0754fcfdcb84ab2a35674e390880288f94b5b968c89a
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module TomosiaIcon8Crawl
|
2
|
+
require 'open-uri'
|
3
|
+
require "HTTParty"
|
4
|
+
require 'pry'
|
5
|
+
require 'WriteExcel'
|
6
|
+
class CrawlIcon8
|
7
|
+
|
8
|
+
# get data from website
|
9
|
+
def self.json(key, max)
|
10
|
+
if key == nil
|
11
|
+
p "No data!"
|
12
|
+
else
|
13
|
+
if max == nil
|
14
|
+
url = "https://search.icons8.com/api/iconsets/v5/search?term=#{key}"
|
15
|
+
else
|
16
|
+
url = "https://search.icons8.com/api/iconsets/v5/search?term=#{key}&amount=#{max}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
page_data = HTTParty.get(url)
|
21
|
+
@responses = page_data.parsed_response
|
22
|
+
end
|
23
|
+
|
24
|
+
# save file to txt
|
25
|
+
def self.save_file_txt(index, name, url, size, extension)
|
26
|
+
File.open("log_image.txt", "a+") do |f|
|
27
|
+
f.write("#{index}. name: #{name} | url: #{url} | size: #{size}Kb | extension: #{extension} \n")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# save file to excel
|
32
|
+
def self.save_file_excel( path, data = {})
|
33
|
+
begin
|
34
|
+
des = path + '/export.xls'
|
35
|
+
workbook = WriteExcel.new(des)
|
36
|
+
format = workbook.add_format
|
37
|
+
format.set_bold()
|
38
|
+
format.set_align('center')
|
39
|
+
|
40
|
+
data_col = workbook.add_format
|
41
|
+
data_col.set_align('center')
|
42
|
+
format_url = workbook.add_format
|
43
|
+
format_url.set_color('blue')
|
44
|
+
format_url.set_align('center')
|
45
|
+
|
46
|
+
worksheet = workbook.add_worksheet
|
47
|
+
|
48
|
+
worksheet.write_string(0, 0, 'STT', format)
|
49
|
+
worksheet.write_string(0, 1, 'NAME', format)
|
50
|
+
worksheet.write_string(0, 2, 'URL', format)
|
51
|
+
worksheet.write_string(0, 3, 'SIZE', format)
|
52
|
+
worksheet.write_string(0, 4, 'EXTENSION', format)
|
53
|
+
|
54
|
+
data.each_with_index do |row, index|
|
55
|
+
i = index + 1
|
56
|
+
# p i
|
57
|
+
# p row
|
58
|
+
row.each do |key, value|
|
59
|
+
# p key
|
60
|
+
|
61
|
+
worksheet.write_string(i, 0, row['index'], data_col)
|
62
|
+
worksheet.write_string(i, 1, row['name'], data_col)
|
63
|
+
worksheet.write_url(i, 2, row['url'], format_url)
|
64
|
+
worksheet.write_string(i, 3, row['size'], data_col)
|
65
|
+
worksheet.write_string(i, 4, row['extension'], data_col)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
workbook.close
|
70
|
+
rescue Exception => e
|
71
|
+
p "Can't saved file"
|
72
|
+
p e
|
73
|
+
# break
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# download image
|
78
|
+
def self.download_image(path, img)
|
79
|
+
File.open(path, 'wb') do |file|
|
80
|
+
file.write open(img).read
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# multi download image
|
85
|
+
def self.multi_download_image(path, imgs)
|
86
|
+
threads = []
|
87
|
+
|
88
|
+
imgs.each_with_index do |img, index|
|
89
|
+
# p index
|
90
|
+
title = File.basename(img, '.png')
|
91
|
+
des = path. + "/" + index.to_s + "-" + title + ".png"
|
92
|
+
threads << Thread.new{download_image(des, img)}
|
93
|
+
threads.each{ |t| t.join }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# main
|
98
|
+
def self.crawl(keyword = nil, path = ".", max = nil)
|
99
|
+
begin
|
100
|
+
data = []
|
101
|
+
images = []
|
102
|
+
des = ""
|
103
|
+
json(keyword, max)
|
104
|
+
@responses['icons'].each_with_index do |item, index|
|
105
|
+
title = item['commonName']
|
106
|
+
|
107
|
+
|
108
|
+
src = "https://img.icons8.com/#{item['platform']}/2x/#{item['commonName']}.png"
|
109
|
+
# Extension image
|
110
|
+
ext = File.extname(src)
|
111
|
+
|
112
|
+
# Name image
|
113
|
+
img_name = File.basename(src, ext)
|
114
|
+
|
115
|
+
# add image
|
116
|
+
images.push(src)
|
117
|
+
|
118
|
+
# Size image
|
119
|
+
size = src.size.to_s + 'px'
|
120
|
+
|
121
|
+
# push data
|
122
|
+
row = {"index" => index, "name" => img_name, "url" => src, "size" => size, "extension" => ext}
|
123
|
+
data.push(row)
|
124
|
+
end
|
125
|
+
# p data
|
126
|
+
|
127
|
+
save_file_excel(path, data)
|
128
|
+
multi_download_image(path, images)
|
129
|
+
rescue Exception => e
|
130
|
+
p "--Runtime error--"
|
131
|
+
p e
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# TomosiaIcon8Crawl::CrawlIcon8.crawl("coronavirus")
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tomosia_icon8_crawl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ninh Tomosia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- VERSION
|
20
|
+
- lib/tomosia_icon8_crawl.rb
|
21
|
+
homepage:
|
22
|
+
licenses: []
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.1.2
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: tomosia_icon8_crawl demo viblo
|
43
|
+
test_files: []
|