Class: Viddler::Sitemap

Inherits:
Object
  • Object
show all
Defined in:
lib/viddler/sitemap.rb

Constant Summary collapse

API_PER_PAGE =
100

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, username, password) ⇒ Sitemap

Setup a new instance of Viddler Sitemap



9
10
11
12
# File 'lib/viddler/sitemap.rb', line 9

def initialize(api_key, username, password)
  @api_key, @username, @password = api_key, username, password
  setup_client
end

Class Method Details

.max_videos_per_fileInteger

The maximum number of videos to be incuded in each sitemap file.



60
61
62
# File 'lib/viddler/sitemap.rb', line 60

def self.max_videos_per_file
  @@max_videos_per_file ||= 50000
end

.max_videos_per_file=(value) ⇒ Object

Set the maximum number of videos that should be included in one sitemap file. Defaults to 50000 which is Google's maximum. You may want to lower this on systems with limited resources



68
69
70
# File 'lib/viddler/sitemap.rb', line 68

def self.max_videos_per_file=(value)
  @@max_videos_per_file = value
end

Instance Method Details

#each_video(&block) ⇒ Object

Yields each video that will be included in the sitemap



47
48
49
50
51
52
53
54
55
56
# File 'lib/viddler/sitemap.rb', line 47

def each_video(&block)
  page = 1
  loop do
    videos = videos_for_page(page)
    videos.each { |v| yield v }

    break if videos.length < API_PER_PAGE
    page += 1
  end
end

#filesArray



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/viddler/sitemap.rb', line 30

def files
  [].tap do |array|
    array << SitemapFile.new
    counter = 0
    each_video do |video|
      counter += 1

      # Add the video to the current file
      array.last.add_video(video)

      # Reached 50k in this file, start a new file
      array << SitemapFile.new if counter % Sitemap.max_videos_per_file == 0
    end
  end
end

#generate!(folder) ⇒ Integer

Generate the sitemap files(s) for your videos



18
19
20
21
22
23
24
25
# File 'lib/viddler/sitemap.rb', line 18

def generate!(folder)
  counter = 0
  files.each_with_index do |file, index|
    counter += file.videos.length
    file.write!(folder, index+1)
  end
  return counter
end