Skip to content

Commit

Permalink
Merge pull request #3 from akurashev/feature/extract-http-client
Browse files Browse the repository at this point in the history
Extract HTTP client
  • Loading branch information
akurashev authored Nov 10, 2019
2 parents bbf7db7 + 9b12880 commit ec86dc8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-11-06 22:43:25 +0300 using RuboCop version 0.76.0.
# on 2019-11-10 19:31:50 +0300 using RuboCop version 0.76.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
Metrics/AbcSize:
Max: 26
Max: 27

# Offense count: 3
# Offense count: 4
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 23
Expand Down
2 changes: 1 addition & 1 deletion instagram_tags_searcher.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Gem::Specification.new do |s|
s.name = 'instagram_tags_searcher'
s.summary = 'Searches for Instagram tags related to the given tag'
s.version = '0.0.2'
s.version = '0.0.3'
s.authors = ['Andrey Kurashev']
s.files = ['lib/instagram_tags_searcher.rb']

Expand Down
27 changes: 9 additions & 18 deletions lib/instagram_tags_searcher.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'open-uri'
require 'json'
require 'cgi'

require 'instagram_tags_searcher/http_client'

module InstagramTagsSearcher
def self.search(tag)
new_tags, codes = from_top(tag)
Expand Down Expand Up @@ -40,7 +40,9 @@ def self.search(tag)

def self.from_top(tag)
tag = CGI.escape(tag)
data = fetch_data("https://www.instagram.com/explore/tags/#{tag}/?__a=1")
data = HttpClient.new.read_hash(
"https://www.instagram.com/explore/tags/#{tag}/?__a=1"
)

moretags = []
codes = []
Expand All @@ -66,7 +68,9 @@ def self.from_top(tag)
end

def self.from_first_comment(code)
data = fetch_data("https://www.instagram.com/p/#{code}/?__a=1")
data = HttpClient.new.read_hash(
"https://www.instagram.com/p/#{code}/?__a=1"
)
comments = data['graphql']['shortcode_media']['edge_media_to_parent_comment']

comments_count = comments['count']
Expand Down Expand Up @@ -107,7 +111,7 @@ def self.sort_by_frequency(tags)
def self.posts_count(tag)
tag_name = CGI.escape(tag[1..-1])
url = "https://www.instagram.com/explore/tags/#{tag_name}/?__a=1"
data = fetch_data(url)
data = HttpClient.new.read_hash(url)

posts = data['graphql']['hashtag']['edge_hashtag_to_media']
posts['count'].to_i
Expand All @@ -118,17 +122,4 @@ def self.search_tags(words)
word.start_with?('#') && word.count('#') == 1 && word.length > 2
end
end

def self.fetch_data(url)
header = {
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3357.400 ' \
'QQBrowser/9.6.11858.400'
}

response = URI.parse(url).open(header).read

JSON.parse(response)
end
end
30 changes: 30 additions & 0 deletions lib/instagram_tags_searcher/http_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'json'
require 'open-uri'

module InstagramTagsSearcher
# This is a simple HTTP client which relies on Ruby StdLib OpenURI module:
# https://ruby-doc.org/stdlib-2.6.3/libdoc/open-uri/rdoc/OpenURI.html
#
# This wrapper supports custom User-Agent header values.
# It expects JSON in response.
class HttpClient
HEADER = {
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3357.400 ' \
'QQBrowser/9.6.11858.400'
}.freeze

def read_hash(url)
JSON.parse(make_request(url))
end

private

def make_request(url)
URI.parse(url).open(HEADER).read
end
end
end

0 comments on commit ec86dc8

Please sign in to comment.