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

Parameters:

  • api_key (String)

    Viddler API key

  • username (String)

    Viddler username

  • password (String)

    Viddler password



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.

Returns:

  • (Integer)

    the maximum number. Defaults to 50000



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

Parameters:

  • value (Integer)

    The maximum number



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

Returns An array of SitemapFile objects each representing 1 sitemap file. There will normally be only 1 of these but could be more if there are lots of videos. There are a maxiumum of 5000 (default) videos per file.

Returns:

  • (Array)

    An array of SitemapFile objects each representing 1 sitemap file. There will normally be only 1 of these but could be more if there are lots of videos. There are a maxiumum of 5000 (default) videos per file.



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

Parameters:

  • folder (String)

    The path to the folder where the sitemap files will be written to

Returns:

  • (Integer)

    the number of videos that were included in the index



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