yandex_xml 0.0.1 → 1.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/yandex_xml.rb +88 -13
  3. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 748231943ffb461959ebb4e682283c749a6c8b335b19f9931918a16b69b9b390
4
- data.tar.gz: 34ef480451509f9c03ed3d7fb8dcf24ee6475d0cc8a741cf7eb2fdc605846698
3
+ metadata.gz: d3238a11e12c46cf6f59299e385f84a8cb2a62a468f1621582bcd0caa3f32fd7
4
+ data.tar.gz: 39dd8d86aac3b0898b087ec7c6fe46a44b8c0bf9613f475481f4db51a436c42a
5
5
  SHA512:
6
- metadata.gz: 36973c51c195acb148341427845c5e67cf2ae5911ea7a7affbafdf995839bd03bffc353e4ee6f105a7c583d805ff7918574ebc6a693ade0788ea925e420e23eb
7
- data.tar.gz: 540f102c72d968a60e8bef98855d2417e05adebc9a422f2e5bf82dc343a29fe439d7b1e1f0c92ff090ea6ed09e76bee8e58b2ce66d8447ba6792bac2a0018fac
6
+ metadata.gz: 229614f1f3268de67dc965c34304e8d3844def5c6ffe047bbdd96380577be15e9c3e423c7928460ebe2517fd5d272a45f9de68928ed639a965f86220fd297f84
7
+ data.tar.gz: '0990c6aa7dbb849aed11567219310299a39321b5add8aeb54f8650e12e08d1ab3fff4cba3608c99c847f7722b6700815fde5abd094fc054d7e9ef74e46a49164'
@@ -1,33 +1,108 @@
1
- # Yandex.XML parser
1
+ # Yandex.XML parser - version 1.0.1 (gem yandex_xml)
2
2
  # Author: https://github.com/krdprog/ - Alexey Tsaplin-Kupaysinov
3
3
  # License: MIT
4
4
  require 'net/http'
5
+ require 'json'
6
+ require 'nori'
5
7
 
6
8
  # Get data from Yandex.XML service by XML - https://xml.yandex.ru/settings/
9
+ # Yandex.XML Doc: https://yandex.ru/dev/xml/doc/dg/concepts/response-docpage/
10
+ # Yandex Regions: https://yandex.ru/dev/xml/doc/dg/reference/regions-docpage/
7
11
  class YandexXml
8
- attr_accessor :keyword, :region
12
+ def initialize(args)
13
+ @user = args[:user]
14
+ @key = args[:key]
15
+ @region = args.fetch(:region, '213') # Moscow
16
+ end
17
+
18
+ # Get data from Yandex.XML and create Hash with result
19
+ def get(keyword)
20
+ # Create URL to Yandex.Xml
21
+ xml_url = create_url(keyword)
22
+ # Get data XML
23
+ xml = Net::HTTP.get_response(URI.parse(xml_url)).body
24
+ # Convert XML to Hash
25
+ converter = Nori.new(:convert_dashes_to_underscores => true)
26
+ hash = converter.parse(xml)
27
+ # Convert Hash to OpenStruct
28
+ data = JSON.parse(hash.to_json, object_class: OpenStruct)
29
+ # Create short route to data
30
+ groups = data.yandexsearch.response.results.grouping.group
31
+
32
+ result = {}
33
+
34
+ result[:meta] = {
35
+ keyword: data.yandexsearch.request.query,
36
+ timestamp: Time.now
37
+ }
38
+
39
+ data_array = []
40
+
41
+ groups.each_with_index do |i, index|
42
+ result_hash = {
43
+ position: index + 1,
44
+ url: i.doc.url,
45
+ domain: i.doc.domain,
46
+ title: i.doc.title,
47
+ modtime: i.doc.modtime,
48
+ size: i.doc.size,
49
+ charset: i.doc.charset,
50
+ # passage: i.doc.passages.passage, # TODO: not work
51
+ passages_type: i.doc.properties._PassagesType,
52
+ mime_type: i.doc.mime_type,
53
+ saved_copy_url: i.doc.saved_copy_url,
54
+ headline: i.doc.headline,
55
+ turbo_cgi_url: i.doc.properties.TurboCgiUrl,
56
+ turbo_fallback: i.doc.properties.TurboFallback,
57
+ turbo_link: i.doc.properties.TurboLink
58
+ }
59
+
60
+ data_array << result_hash
61
+ end
9
62
 
10
- def initialize(attributes = {})
11
- @user = attributes[:user]
12
- @key = attributes[:key]
13
- @region = attributes[:region]
63
+ result[:data] = data_array
64
+ result
14
65
  end
15
66
 
16
- # Get data from Yandex.XML service to XML format
17
- def to_xml
18
- create_xml_url
67
+ # Get 100 urls (Yandex Top-100) from result
68
+ def get_top100_urls(keyword)
69
+ hash = get(keyword)
70
+ struct = JSON.parse(hash.to_json, object_class: OpenStruct)
71
+
72
+ urls = []
73
+ struct.data.each { |i| urls << i.url }
74
+ urls
75
+ end
76
+
77
+ # Get position in Yandex Top-100 for site by keyword
78
+ def get_position(keyword, my_domain)
79
+ hash = get(keyword)
80
+ struct = JSON.parse(hash.to_json, object_class: OpenStruct)
81
+
82
+ domains = []
83
+ struct.data.each { |i| domains << i.domain.downcase }
84
+
85
+ position = 0
86
+ domains.each_with_index do |domain, i|
87
+ position = i + 1 if domain == my_domain
88
+ end
89
+
90
+ position
91
+ end
19
92
 
20
- @yandex_xml_to_xml = Net::HTTP.get_response(URI.parse(@xml_url)).body
93
+ # TODO: Get information about limits in Yandex.XML service.
94
+ def get_limits
95
+ # code
21
96
  end
22
97
 
23
98
  private
24
99
 
25
100
  # Create URL for query from Yandex.XML by XML
26
- def create_xml_url
101
+ def create_url(keyword)
27
102
  base_url = 'https://yandex.ru/search/xml?'
28
103
  tail_url = '&groupby=attr%3Dd.mode%3Ddeep.groups-on-page%3D100.docs-in-group%3D1'
29
- query_url = URI.encode_www_form([['user', @user], ['key', @key], ['query', @keyword], ['lr', @region]])
104
+ query_url = URI.encode_www_form([['user', @user], ['key', @key], ['query', keyword], ['lr', @region]])
30
105
 
31
- @xml_url = base_url + query_url + tail_url
106
+ base_url + query_url + tail_url
32
107
  end
33
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex_xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Tsaplin-Kupaysinov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-01 00:00:00.000000000 Z
11
+ date: 2019-10-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Get data from Yandex.XML service by XML
14
14
  email: info@krdprog.ru
@@ -20,8 +20,10 @@ files:
20
20
  homepage: https://github.com/krdprog/yandex_xml
21
21
  licenses:
22
22
  - MIT
23
- metadata: {}
24
- post_install_message:
23
+ metadata:
24
+ documentation_uri: https://rubydoc.info/github/krdprog/yandex_xml/
25
+ homepage_uri: https://github.com/krdprog/yandex_xml
26
+ post_install_message: 'Thanks for installing! Author: @krdprog'
25
27
  rdoc_options: []
26
28
  require_paths:
27
29
  - lib
@@ -36,8 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
38
  - !ruby/object:Gem::Version
37
39
  version: '0'
38
40
  requirements: []
39
- rubyforge_project:
40
- rubygems_version: 2.7.8
41
+ rubygems_version: 3.0.3
41
42
  signing_key:
42
43
  specification_version: 4
43
44
  summary: Ruby application for Yandex.XML service