This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathvimeo_uservideos.rb
85 lines (76 loc) · 2.6 KB
/
vimeo_uservideos.rb
1
2
3
4
5
6
7
8
9
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
#!/usr/bin/env ruby
require 'net/http'
require 'json'
# Job capturing user video info
#
# This job will capture the data from all videos of a user using the vimeo
# api: https://developer.vimeo.com/apis/simple#user-request
# This can be then be used to display a top-5 in a "List" widget
#
# This job will send three lists containing likes, plays and comments counts:
# `vimeo_uservideos_likes`, `vimeo_uservideos_plays` and
# `vimeo_uservideos_comments`
#
# There’s also a `vimeo_uservideos_
# Config
# ------
vimeo_username = 'spread'
# set the number of videos to display
vimeo_max_length = 5
# set to false if videos should not be ordered by value
vimeo_ordered = true
SCHEDULER.every '5m', :first_in => 0 do |job|
http = Net::HTTP.new("vimeo.com")
response = http.request(Net::HTTP::Get.new("/api/v2/#{vimeo_username}/videos.json"))
if response.code != "200"
puts "vimeo api error (status-code: #{response.code})\n#{response.body}"
else
videosData = JSON.parse(response.body)
videos_likes = Array.new
videos_plays = Array.new
videos_comments = Array.new
videos_sums = Array.new
# generate lists
videosData.each do |video|
title = video['title']
if title.length > 12
title = title[0..10] + ".."
end
videos_likes.push({
label: title,
value: video['stats_number_of_likes']
})
videos_plays.push({
label: title,
value: video['stats_number_of_plays']
})
videos_comments.push({
label: title,
value: video['stats_number_of_comments']
})
videos_sums.push({
label: title,
value: (
video['stats_number_of_plays'] +
10 * video['stats_number_of_likes'] +
50 * video['stats_number_of_comments']
)
})
end
# order lists by values?
if vimeo_ordered
videos_likes = videos_likes.sort_by { |obj| -obj[:value] }
videos_plays = videos_plays.sort_by { |obj| -obj[:value] }
videos_comments = videos_comments.sort_by { |obj| -obj[:value] }
videos_sums = videos_sums.sort_by { |obj| -obj[:value] }
end
if defined?(send_event)
send_event('vimeo_uservideos_likes', { items: videos_likes.slice(0, vimeo_max_length) })
send_event('vimeo_uservideos_plays', { items: videos_plays.slice(0, vimeo_max_length) })
send_event('vimeo_uservideos_comments', { items: videos_comments.slice(0, vimeo_max_length) })
send_event('vimeo_uservideos_sums', { items: videos_sums.slice(0, vimeo_max_length) })
else
print videos_likes, videos_plays, videos_comments, videos_sums
end
end
end