Class: UpdateBlogJob

Inherits:
ApplicationJob show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/jobs/update_blog_job.rb

Direct Known Subclasses

UpdatePodcastJob

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blogObject (readonly)

Returns the value of attribute blog


8
9
10
# File 'app/jobs/update_blog_job.rb', line 8

def blog
  @blog
end

Instance Method Details

#caps?(text) ⇒ Boolean

Returns:

  • (Boolean)

113
114
115
# File 'app/jobs/update_blog_job.rb', line 113

def caps?(text)
  text.strip.present? && !text.match(/[a-zа-я]+/)
end

#entry_to_post(entry) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/jobs/update_blog_job.rb', line 117

def entry_to_post(entry)
  entry_url = url_after_redirects(entry.url)

  # иначе youtube ленты содержат дубликаты
  entry_url.sub('&feature=youtube_gdata', '')

  post_body = entry.content.presence || entry.summary || ''

  title = entry.title

  pub_date = if entry.published.blank?
               Time.now
             elsif entry.published < (blog.checked_at || 1.hour.ago)
               (blog.checked_at || 1.hour.ago)
             else
               [entry.published.to_time, Time.now].min
             end

  title = title.mb_chars.capitalize.to_s if caps? title

  post = Post.new(body: HtmlCleanup.new(blog.cleanup_html(post_body)).cleanup,
                  blog_id: blog.id,
                  created_at: pub_date,
                  source_url: entry_url,
                  title: Typogruby.improve(strip_tags(title)),
                  stream: blog.default_stream)

  post
end

#need_check?(blog) ⇒ Boolean

Returns:

  • (Boolean)

89
90
91
92
# File 'app/jobs/update_blog_job.rb', line 89

def need_check?(blog)
  posts_per_hour = [blog.posts_per_hour || 1, 1].max
  blog.checked_at.blank? || blog.checked_at < 1.hour.ago || blog.checked_at < (1.hour / (posts_per_hour || 1)).seconds.ago
end

#perform(blog = nil, force = false) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/jobs/update_blog_job.rb', line 10

def perform(blog = nil, force = false)
  if blog.nil?
    BlogBase.with_feed.find_each do |blog|
      case blog
      when Blog
        UpdateBlogJob.perform_later blog
      when Podcast
        UpdatePodcastJob.perform_later blog
      end
    end

    return
  end

  @blog = blog

  return unless need_check?(blog) || force

  logger.info "Updating blog #{blog.id} feed '#{blog.feed_url}'"

  result = Timeout.timeout(30) do
    Feedjira::Feed.fetch_and_parse(blog.feed_url)
  end

  EventTracker.track 'Blogs', 'Feed update', blog.title

  if result.is_a? Numeric
    raise "Updating blog #{blog.id} faild, feedzirra status code '#{result}'"
  end

  feed = result

  not_older_than = blog.posts.maximum(:created_at)

  # сначала старые
  feed.entries.reverse.each do |entry|
    if not_older_than.present? && entry.published.present? && entry.published < not_older_than
      logger.warn "Entry #{entry.url} is old"
      next
    end

    if entry.url.blank?
      logger.error 'Entry has blank url'
      next
    end

    if entry.title.blank?
      logger.error "Entry #{entry.url} has no title!"
      next
    end

    post = entry_to_post entry

    if blog.posts.exists?(source_url: post.source_url)      # TODO: обновлять статью

      logger.warn "Entry #{entry.url} is already added"
      next
    end

    if post.valid?
      post.save!
      EventTracker.track 'Blogs', 'Created post via Feed', blog.title
      TaggerJob.perform_later post
      ImageFinderJob.perform_later post

      logger.info "Post ##{post.id} created!"
    else
      EventTracker.track 'Blogs', 'Failed to create post via Feed', blog.title
    end
  end

  blog.checked!
rescue Feedjira::FetchFailure, Timeout::Error => ex
  blog.failed_to_check!(ex)
rescue StandardError => ex
  blog.failed_to_check!(ex)
  raise
end

#url_after_redirects(source_url) ⇒ Object

получаем полный url - после редиректов


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/jobs/update_blog_job.rb', line 95

def url_after_redirects(source_url)
  follow_url = ->(url) { HTTPClient.new.head(url).header['Location'][0] }

  target_url = source_url

  loop do
    next_url = follow_url.call(target_url)

    break if next_url.blank?

    target_url = next_url
  end

  target_url
rescue
  source_url
end