Skip to content

Commit

Permalink
Merge pull request #4 from akurashev/feature/extract-instagram-client
Browse files Browse the repository at this point in the history
Extract Instagram client
  • Loading branch information
akurashev authored Nov 10, 2019
2 parents ec86dc8 + de15e5e commit f79f05b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 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-10 19:31:50 +0300 using RuboCop version 0.76.0.
# on 2019-11-10 21:16:27 +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: 27
Max: 25

# Offense count: 4
# Offense count: 3
# 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.3'
s.version = '0.0.4'
s.authors = ['Andrey Kurashev']
s.files = ['lib/instagram_tags_searcher.rb']

Expand Down
17 changes: 4 additions & 13 deletions lib/instagram_tags_searcher.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require 'cgi'

require 'instagram_tags_searcher/http_client'
require 'instagram_tags_searcher/instagram_client'

module InstagramTagsSearcher
def self.search(tag)
Expand Down Expand Up @@ -39,10 +37,7 @@ def self.search(tag)
end

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

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

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

comments_count = comments['count']
Expand Down Expand Up @@ -109,9 +102,7 @@ def self.sort_by_frequency(tags)
end

def self.posts_count(tag)
tag_name = CGI.escape(tag[1..-1])
url = "https://www.instagram.com/explore/tags/#{tag_name}/?__a=1"
data = HttpClient.new.read_hash(url)
data = InstagramClient.new.top(tag)

posts = data['graphql']['hashtag']['edge_hashtag_to_media']
posts['count'].to_i
Expand Down
47 changes: 47 additions & 0 deletions lib/instagram_tags_searcher/instagram_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'cgi'

require 'instagram_tags_searcher/http_client'

module InstagramTagsSearcher
# This class is responsible for making requests to Instagram
# It knows which URL should be used and which params should be passed
class InstagramClient
URL_TOP = 'https://www.instagram.com/explore/tags/%<param>s/?__a=1'
URL_POST = 'https://www.instagram.com/p/%<param>s/?__a=1'

def initialize(http_client = HttpClient.new)
@http_client = http_client
end

def top(tag)
@param = sanitize_tag(tag)
@url_template = URL_TOP
make_request
end

def post(code)
@param = code
@url_template = URL_POST
make_request
end

private

attr_reader :http_client, :url_template, :param

def make_request
http_client.read_hash(url)
end

def url
format(url_template, param: param)
end

def sanitize_tag(tag)
tag = tag[1..-1] if tag.start_with?('#')
CGI.escape(tag)
end
end
end

0 comments on commit f79f05b

Please sign in to comment.