Skip to content

Commit

Permalink
Merge pull request #11 from rakeshpatra/feature/tag-media-search
Browse files Browse the repository at this point in the history
Add feature for searching media by hash-tag
  • Loading branch information
rakeshpatra authored Nov 11, 2020
2 parents 8d59ae4 + 349df33 commit ad7b4bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ client.discover_user(USERNAME, fields)

#discover user media, fields can be "caption,media_url,media_type,like_count,comments_count,id"
client.discover_user_media(USERNAME, fields)

#Search for a tag,
client.tag_media(page_token, tag_name)
#fields can be "media_type,comments_count,like_count,media_url,permalink"
fields="media_url,permalink"
client.tag_media(page_token, tag_name, fields)
#custom edge can be provided, default is 'top_media'
tag_media = client.tag_media(page_token, tag_name, edge: "recent_media")
# Note: We can run `tag_media.next_page` to fetch next set of results
# additional arguent options can also be passed for page params i.e before/after/limit etc
```

## Development
Expand Down
2 changes: 1 addition & 1 deletion lib/instagram_graph_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Client < Koala::Facebook::API
include InstagramGraphApi::Client::Users
include InstagramGraphApi::Client::Media
include InstagramGraphApi::Client::Discovery

include InstagramGraphApi::Client::Tag
end
end
21 changes: 21 additions & 0 deletions lib/instagram_graph_api/client/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module InstagramGraphApi
class Client
module Tag
def tag_media(page_token, tag_name, fields = nil, edge: "top_media", options: {})
user_id = get_ig_id_from_token(page_token)
tag_info = graph_call("ig_hashtag_search?user_id=#{user_id}&q=#{tag_name}")[0]
fields ||= "id,media_type,comments_count,like_count,media_url,permalink"
query = "#{edge}?user_id=#{user_id}&fields=#{fields}"
query += options.reduce(""){|s, (k, v)| s+= "&#{k}=#{v}" }
get_connections(tag_info["id"], query)
end

private

def get_ig_id_from_token(token)
account_info = get_connections('me', "?fields=connected_instagram_account")
account_info.dig("connected_instagram_account", "id")
end
end
end
end
2 changes: 1 addition & 1 deletion lib/instagram_graph_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module InstagramGraphApi
VERSION = "0.0.11"
VERSION = "0.0.12"
end

0 comments on commit ad7b4bc

Please sign in to comment.