-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from realstorypro/twitter-send
Adding ability to send tweets
- Loading branch information
Showing
10 changed files
with
151 additions
and
80 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,34 @@ | ||
module Discussions | ||
class PublishJob < ApplicationJob | ||
queue_as :default | ||
MAX_STORIES_PUBLISHED_AT_A_TIME = 1 | ||
|
||
def perform(*args) | ||
ready_discussions = Discussion.ready_to_upload | ||
|
||
todays_uploaded_discussions_by_subtopic = Discussion.published_today_and_uploaded | ||
.group('sub_topics.id') | ||
.select('sub_topics.*, COUNT(discussions.id) AS discussions_count') | ||
.map do |sub_topic| | ||
{ | ||
sub_topic: sub_topic, | ||
discussions_count: sub_topic.discussions_count | ||
} | ||
end | ||
|
||
subtopics_with_max_discussions = todays_uploaded_discussions_by_subtopic.select do |hash| | ||
hash[:discussions_count] >= hash[:sub_topic].max_stories_per_day | ||
end | ||
|
||
# Extract subtopic_ids from subtopics_with_max_discussions | ||
subtopic_ids_with_max_discussions = subtopics_with_max_discussions.map { |hash| hash[:sub_topic].id } | ||
|
||
# Exclude discussions that have a sub_topic_id in subtopic_ids_with_max_discussions | ||
ready_discussions = ready_discussions.where.not(stories: { sub_topic_id: subtopic_ids_with_max_discussions }) | ||
|
||
ready_discussions.sample(MAX_STORIES_PUBLISHED_AT_A_TIME).each do |discussion| | ||
StoryPro::CreateDiscussionJob.perform_now(discussion:) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,32 +1,7 @@ | ||
class PublishJob < ApplicationJob | ||
queue_as :default | ||
MAX_STORIES_PUBLISHED_AT_A_TIME = 1 | ||
|
||
def perform(*args) | ||
ready_discussions = Discussion.ready_to_upload | ||
|
||
todays_uploaded_discussions_by_subtopic = Discussion.published_today_and_uploaded | ||
.group('sub_topics.id') | ||
.select('sub_topics.*, COUNT(discussions.id) AS discussions_count') | ||
.map do |sub_topic| | ||
{ | ||
sub_topic: sub_topic, | ||
discussions_count: sub_topic.discussions_count | ||
} | ||
end | ||
|
||
subtopics_with_max_discussions = todays_uploaded_discussions_by_subtopic.select do |hash| | ||
hash[:discussions_count] >= hash[:sub_topic].max_stories_per_day | ||
end | ||
|
||
# Extract subtopic_ids from subtopics_with_max_discussions | ||
subtopic_ids_with_max_discussions = subtopics_with_max_discussions.map { |hash| hash[:sub_topic].id } | ||
|
||
# Exclude discussions that have a sub_topic_id in subtopic_ids_with_max_discussions | ||
ready_discussions = ready_discussions.where.not(stories: { sub_topic_id: subtopic_ids_with_max_discussions }) | ||
|
||
ready_discussions.sample(MAX_STORIES_PUBLISHED_AT_A_TIME).each do |discussion| | ||
StoryPro::CreateDiscussionJob.perform_now(discussion:) | ||
end | ||
def perform(*_args) | ||
Discussions::PublishJob.perform_now | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Tweets::PublishJob < ApplicationJob | ||
queue_as :default | ||
MAX_TWEETS_PUBLISHED_AT_A_TIME = 1 | ||
|
||
def perform(*args) | ||
Discussion.ready_to_upload_tweets.sample(MAX_TWEETS_PUBLISHED_AT_A_TIME).each do |discussion| | ||
Tweets::PublishTweetJob.perform_now(discussion:) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Tweets::PublishTweetJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(discussion:) | ||
return if discussion.tweet.uploaded | ||
|
||
story = discussion.story | ||
tweet = discussion.tweet | ||
|
||
tweet_text = JSON.parse(tweet.stem)['tweet'] | ||
card_image = tweet.discussion.story.imaginations.where(aspect_ratio: :card).sample(1) | ||
card_image_url = "https://ucarecdn.com/#{card_image.last.uploadcare.last['uuid']}/" | ||
|
||
|
||
story_pro_discussion = StoryPro.get_discussion(discussion.story_pro_id) | ||
discussion_slug = story_pro_discussion["entry"]["slug"] | ||
category_slug = get_story_pro_category_slug(story) | ||
|
||
discussion_url = "#{ENV['STORYPRO_URL']}/discussions/#{category_slug}/#{discussion_slug}" | ||
|
||
full_tweet = "#{tweet_text} #{discussion_url}" | ||
Ayrshare.post_message(post: full_tweet, platforms: ['twitter'], media_urls: [card_image_url]) | ||
|
||
tweet.update(uploaded: true) | ||
end | ||
|
||
def get_story_pro_category_slug(story) | ||
story_pro_categories = StoryPro.get_categories | ||
found_story_pro_category = story_pro_categories.find { |c| c["id"] == story.sub_topic.storypro_category_id } | ||
found_story_pro_category['slug'] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'httparty' | ||
require 'json' | ||
|
||
module Ayrshare | ||
class Configuration | ||
attr_accessor :api_key | ||
|
||
def initialize | ||
@api_key = nil | ||
end | ||
end | ||
|
||
class << self | ||
attr_accessor :configuration | ||
end | ||
|
||
def self.configure | ||
self.configuration ||= Configuration.new | ||
yield(configuration) | ||
end | ||
|
||
def self.post_message(post:, platforms:, media_urls: []) | ||
url = "https://app.ayrshare.com/api/post" | ||
headers = { 'Authorization' => "Bearer #{configuration.api_key}" } | ||
body = { post:, platforms:, mediaUrls: [media_urls] } | ||
|
||
response = HTTParty.post(url, headers:, body:) | ||
|
||
raise "Error: #{response.code} - #{response.body}" unless response.code == 200 | ||
|
||
JSON.parse(response.body) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'ayrshare' | ||
|
||
Ayrshare.configure do |config| | ||
config.api_key = ENV['AYRSHARE_API_KEY'] | ||
end |