class Article
Constants
- DEFAULT_OPTS
- RASTERIZE_PATH
Attributes
bot_name[R]
Public Class Methods
connect_to_database(bot_name)
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 15 def connect_to_database(bot_name) @bot_name = bot_name ActiveRecord::Base.logger = Logger.new('debug.log') ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: "#{bot_name}.sqlite3", encoding: 'utf8' ) end
fetch_at_random(opts)
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 43 def self.fetch_at_random(opts) options = DEFAULT_OPTS.merge opts articles = FindArticles.at_random(count: options[:count]) puts "#{articles.count} mainspace articles found" if options[:discard_redirects] articles = DiscardRedirects.from(articles) puts "#{articles.count} are not redirects" end if options[:min_views].positive? articles = HighPageviews.from_among(articles, min_views: options[:min_views]) puts "#{articles.count} of those have high page views" end if options[:max_wp10] articles = Ores.discard_high_revision_scores(articles, max_wp10: options[:max_wp10]) puts "#{articles.count} of those have low revision scores" end if options[:discard_dabs] articles = CategoryFilter.discard_disambiguation_pages(articles) puts "#{articles.count} of those are not disambiguation pages" end if articles.count > 0 puts "#{articles.count} tweetable prospect(s) found!" else puts 'no tweetable articles found' end articles end
first_tweetable()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 82 def self.first_tweetable tweetable.first end
import_at_random(opts)
click to toggle source
Class methods #
# File lib/wikipedia_twitterbot/article.rb, line 31 def self.import_at_random(opts) import fetch_at_random(opts) end
last_tweetable()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 78 def self.last_tweetable tweetable.last end
tweetable()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 86 def self.tweetable where(tweeted: nil, failed_tweet_at: nil) end
Public Instance Methods
bot_name()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 157 def bot_name self.class.bot_name end
commons_link(image)
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 110 def commons_link(image) "https://commons.wikimedia.org/wiki/#{CGI.escape(image.tr(' ', '_'))}" end
dirp()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 141 def dirp pp RASTERIZE_PATH end
edit_url()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 135 def edit_url # Includes the summary preload #FixmeBot, so that edits can be tracked: # http://tools.wmflabs.org/hashtags/search/wikiphotofight "https://en.wikipedia.org/wiki/#{escaped_title}?veaction=edit&summary=%23#{bot_name}" end
escaped_title()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 114 def escaped_title # CGI.escape will convert spaces to '+' which will break the URL CGI.escape(title.tr(' ', '_')) end
hashtag()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 153 def hashtag TwitterClient.new.top_hashtag(title) end
make_screenshot()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 146 def make_screenshot # Use rasterize script to make a screenshot %x[phantomjs #{RASTERIZE_PATH} #{mobile_url} #{screenshot_path} 1000px*1000px] # Trim any extra blank space, which may or may not be present. %x[convert #{screenshot_path} -trim #{screenshot_path}] end
mobile_url()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 131 def mobile_url "https://en.m.wikipedia.org/wiki/#{escaped_title}" end
page_text()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 169 def page_text @page_text ||= Wiki.get_page_content title end
plaintext()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 173 def plaintext @plaintext = ArticleTextCleaner.convert(page_text) end
quality()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 123 def quality wp10.to_i end
screenshot_path()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 105 def screenshot_path FileUtils.mkdir_p('screenshots') unless File.directory?('screenshots') "screenshots/#{escaped_title}.png" end
sentence_with(text)
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 177 def sentence_with(text) # TODO: Remove the plaintext footnote remnants plaintext[/[^.?!\n]*#{Regexp.quote text}[^.?!]*[.?!]/i] end
tweet(tweet_text, opts = {})
click to toggle source
Instance methods #
# File lib/wikipedia_twitterbot/article.rb, line 93 def tweet(tweet_text, opts = {}) @tweet_result = Tweet.new(tweet_text, opts).result self.tweeted = true save pp 'tweeted' @tweet_result rescue StandardError => e self.failed_tweet_at = Time.now save raise e end
url()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 127 def url "https://en.wikipedia.org/wiki/#{escaped_title}" end
views()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 119 def views average_views.to_i end
wikilinks()
click to toggle source
# File lib/wikipedia_twitterbot/article.rb, line 161 def wikilinks return @links if @links.present? query = { prop: 'links', titles: title, plnamespace: '0', pllimit: 500 } response = Wiki.query query @links = response.data['pages'].values.first['links'].map { |link| link['title'] } @links end