-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fetching of most positive/negative/replied/retweeted tweets…
… per slug
- Loading branch information
1 parent
1934339
commit b306cd2
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule Sanbase.SocialData.Tweet do | ||
require Mockery.Macro | ||
@tweet_types [:most_positive, :most_negative, :most_retweets, :most_replies] | ||
@tweet_type_mapping %{ | ||
most_positive: :sentiment_pos, | ||
most_negative: :sentiment_neg, | ||
most_retweets: :retweet, | ||
most_replies: :reply | ||
} | ||
@recv_timeout 25_000 | ||
def get_most_tweets(%{} = selector, type, from, to, interval) do | ||
slugs = (Map.get(selector, :slug) || Map.get(selector, :slugs)) |> List.wrap() | ||
|
||
tweets_request(slugs, type, from, to, interval) | ||
|> handle_tweets_response() | ||
end | ||
|
||
defp handle_tweets_response({:ok, %HTTPoison.Response{status_code: 200, body: json_body}}) do | ||
case Jason.decode(json_body) do | ||
{:ok, %{"data" => data}} -> {:ok, decode_tweets_data(data)} | ||
_ -> {:error, "Malformed response fetching tweets"} | ||
end | ||
end | ||
|
||
defp handle_tweets_response({:ok, %HTTPoison.Response{status_code: status}}) do | ||
{:error, "Error status #{status} fetching tweets"} | ||
end | ||
|
||
defp decode_tweets_data(data_map) when is_map(data_map) do | ||
data_map | ||
|> Enum.map(fn {slug, json_list} -> | ||
list = Jason.decode!(json_list) | ||
|
||
tweets = | ||
Enum.map(list, fn map -> | ||
%{ | ||
text: Map.fetch!(map, "text"), | ||
screen_name: Map.fetch!(map, "screen_name"), | ||
datetime: | ||
Map.fetch!(map, "timestamp") | ||
|> NaiveDateTime.from_iso8601!() | ||
|> DateTime.from_naive!("Etc/UTC"), | ||
# TODO: Replace with fetch! after metricshub is updated | ||
replies_count: Map.get(map, "reply"), | ||
sentiment_pos: Map.get(map, "sentiment_pos"), | ||
sentiment_neg: Map.get(map, "sentiment_neg"), | ||
retweets_count: Map.get(map, "retweet") | ||
} | ||
end) | ||
|
||
%{slug: slug, tweets: tweets} | ||
end) | ||
end | ||
|
||
defp tweets_request(slugs, type, from, to, interval) | ||
when type in @tweet_types and is_list(slugs) do | ||
url = Path.join([metrics_hub_url(), "fetch_documents"]) | ||
|
||
options = [ | ||
recv_timeout: @recv_timeout, | ||
params: [ | ||
{"slugs", slugs |> List.wrap() |> Enum.join(",")}, | ||
{"from_timestamp", from |> DateTime.truncate(:second) |> DateTime.to_iso8601()}, | ||
{"to_timestamp", to |> DateTime.truncate(:second) |> DateTime.to_iso8601()}, | ||
{"interval", interval}, | ||
{"source", "twitter"}, | ||
{"most_type", Map.fetch!(@tweet_type_mapping, type)} | ||
] | ||
] | ||
|
||
http_client().get(url, [], options) | ||
end | ||
|
||
defp http_client, do: Mockery.Macro.mockable(HTTPoison) | ||
|
||
defp metrics_hub_url() do | ||
Sanbase.Utils.Config.module_get(Sanbase.SocialData, :metricshub_url) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters