|
@@ -1,13 +1,10 @@
|
1
|
1
|
require 'dotenv/load'
|
2
|
|
-require 'google/apis/youtube_v3'
|
3
|
2
|
require 'json'
|
4
|
3
|
require 'open-uri'
|
5
|
4
|
require 'twitter'
|
6
|
5
|
|
7
|
6
|
Dotenv.load
|
8
|
7
|
|
9
|
|
-YOUTUBE_API_KEY = ENV["YOUTUBE_API_KEY"]
|
10
|
|
-
|
11
|
8
|
TWITTER_CONSUMER_KEY = ENV["TWITTER_CONSUMER_KEY"]
|
12
|
9
|
TWITTER_CONSUMER_SECRET = ENV["TWITTER_CONSUMER_SECRET"]
|
13
|
10
|
TWITTER_ACCESS_TOKEN = ENV["TWITTER_ACCESS_TOKEN"]
|
|
@@ -17,101 +14,63 @@ YOUTUBE_DL_PATH = `which youtube-dl`.chomp
|
17
|
14
|
FFMPEG_PATH = `which ffmpeg`.chomp
|
18
|
15
|
FFPROBE_PATH = `which ffprobe`.chomp
|
19
|
16
|
|
20
|
|
-throw "YOUTUBE_API_KEY is required" unless YOUTUBE_API_KEY
|
|
17
|
+VIDEO_PATH = "/tmp/video.mp4"
|
|
18
|
+FRAME_PATH = "/tmp/frame.png"
|
|
19
|
+
|
|
20
|
+PLAYLIST_ID = ENV["PLAYLIST_ID"]
|
|
21
|
+
|
21
|
22
|
throw "TWITTER_CONSUMER_KEY is required" unless TWITTER_CONSUMER_KEY
|
22
|
23
|
throw "TWITTER_CONSUMER_SECRET is required" unless TWITTER_CONSUMER_SECRET
|
23
|
24
|
throw "TWITTER_ACCESS_TOKEN is required" unless TWITTER_ACCESS_TOKEN
|
24
|
25
|
throw "TWITTER_ACCESS_TOKEN_SECRET is required" unless TWITTER_ACCESS_TOKEN_SECRET
|
25
|
26
|
|
|
27
|
+throw "PLAYLIST_ID is required" unless PLAYLIST_ID
|
|
28
|
+
|
26
|
29
|
throw "Can't find youtube-dl" if YOUTUBE_DL_PATH.empty?
|
27
|
30
|
throw "Can't find ffprobe" if FFPROBE_PATH.empty?
|
28
|
31
|
throw "Can't find ffmpeg" if FFMPEG_PATH.empty?
|
29
|
32
|
|
30
|
33
|
class YouTubeService
|
31
|
|
- @@RESULTS_PER_PAGE = 50.0
|
32
|
|
- @@PLAYLIST_OPTIONS = {
|
33
|
|
- fields: 'items/snippet/resourceId/videoId,nextPageToken,page_info',
|
34
|
|
- max_results: @@RESULTS_PER_PAGE,
|
35
|
|
- }
|
36
|
|
-
|
37
|
|
- def initialize
|
38
|
|
- @youtube = Google::Apis::YoutubeV3::YouTubeService.new
|
39
|
|
- @youtube.key = YOUTUBE_API_KEY
|
|
34
|
+ def initialize(playlist_id)
|
|
35
|
+ @playlist_id = playlist_id
|
40
|
36
|
end
|
41
|
37
|
|
42
|
|
- def fetch_random_frame_from_random_video
|
43
|
|
- video_id = fetch_random_video_from_channel('ComputerChroniclesYT')
|
44
|
|
- video_path = download_video(video_id)
|
45
|
|
- frame_path = extract_frame(video_path)
|
46
|
|
- delete_file(video_path)
|
47
|
|
- frame_path
|
|
38
|
+ def fetch_random_frame_of_random_video
|
|
39
|
+ download_random_video
|
|
40
|
+ extract_frame
|
|
41
|
+ delete_video
|
48
|
42
|
end
|
49
|
43
|
|
50
|
|
-
|
51
|
|
- def fetch_random_video_from_channel(channel_name)
|
52
|
|
- uploads_playlist_id = fetch_channel_uploads_playlist(channel_name)
|
53
|
|
- response = fetch_playlist_videos(uploads_playlist_id)
|
54
|
|
- extract_random_video(uploads_playlist_id, response)
|
55
|
|
- end
|
56
|
|
-
|
57
|
|
- def fetch_channel_uploads_playlist(channel_name)
|
58
|
|
- channel = @youtube.list_channels('contentDetails', for_username: channel_name)
|
59
|
|
- throw "No channel found" unless channel.items.first
|
60
|
|
- channel.items.first.content_details.related_playlists.uploads
|
|
44
|
+ def download_random_video
|
|
45
|
+ `#{YOUTUBE_DL_PATH} --playlist-random --max-downloads 1 -o #{VIDEO_PATH} -f mp4 #{@playlist_id}`
|
61
|
46
|
end
|
62
|
47
|
|
63
|
|
- def fetch_playlist_videos(playlist_id)
|
64
|
|
- @youtube.list_playlist_items('snippet', @@PLAYLIST_OPTIONS.merge(playlist_id: playlist_id))
|
65
|
|
- end
|
66
|
|
-
|
67
|
|
- def extract_random_video(playlist_id, response)
|
68
|
|
- number_of_videos = response.page_info.total_results
|
69
|
|
- number_of_pages = (number_of_videos / @@RESULTS_PER_PAGE).ceil
|
70
|
|
- random_page_number = rand(1..number_of_pages)
|
71
|
|
-
|
72
|
|
- (1..random_page_number).each do
|
73
|
|
- response = @youtube.list_playlist_items(
|
74
|
|
- 'snippet',
|
75
|
|
- @@PLAYLIST_OPTIONS.merge(
|
76
|
|
- page_token: response.next_page_token,
|
77
|
|
- playlist_id: playlist_id
|
78
|
|
- )
|
79
|
|
- )
|
80
|
|
- end
|
81
|
|
-
|
82
|
|
- response.items.sample.snippet.resource_id.video_id
|
83
|
|
- end
|
84
|
|
-
|
85
|
|
- def download_video(video_id)
|
86
|
|
- output_path = "/tmp/video.mp4"
|
87
|
|
- `#{YOUTUBE_DL_PATH} -f mp4 -o /tmp/video.mp4 #{video_id}`
|
88
|
|
- output_path
|
|
48
|
+ def extract_frame
|
|
49
|
+ length = get_video_length
|
|
50
|
+ random_second = rand(0..length)
|
|
51
|
+ `#{FFMPEG_PATH} -v error -ss #{random_second} -i #{VIDEO_PATH} -frames 1 -y #{FRAME_PATH}`
|
89
|
52
|
end
|
90
|
53
|
|
91
|
|
- def extract_frame(video_path)
|
92
|
|
- output_path = "/tmp/frame.png"
|
93
|
|
- length = get_video_length(video_path)
|
94
|
|
- random_second = rand(0..length)
|
95
|
|
- `#{FFMPEG_PATH} -v error -ss #{random_second} -i #{video_path} -frames 1 -y #{output_path}`
|
96
|
|
- output_path
|
|
54
|
+ def get_video_length
|
|
55
|
+ `#{FFPROBE_PATH} -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 #{VIDEO_PATH}`.chomp.to_i
|
97
|
56
|
end
|
98
|
57
|
|
99
|
|
- def get_video_length(video_path)
|
100
|
|
- `#{FFPROBE_PATH} -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 #{video_path}`.chomp.to_i
|
|
58
|
+ def delete_video
|
|
59
|
+ `rm #{VIDEO_PATH}`
|
101
|
60
|
end
|
102
|
61
|
|
103
|
|
- def delete_file(path)
|
104
|
|
- `rm #{path}`
|
|
62
|
+ def delete_frame
|
|
63
|
+ `rm #{FRAME_PATH}`
|
105
|
64
|
end
|
106
|
65
|
end
|
107
|
66
|
|
108
|
67
|
class TwitterService
|
109
|
|
- def initialize
|
|
68
|
+ def initialize(key, secret, token, token_secret)
|
110
|
69
|
@twitter = Twitter::REST::Client.new do |config|
|
111
|
|
- config.consumer_key = TWITTER_CONSUMER_KEY
|
112
|
|
- config.consumer_secret = TWITTER_CONSUMER_SECRET
|
113
|
|
- config.access_token = TWITTER_ACCESS_TOKEN
|
114
|
|
- config.access_token_secret = TWITTER_ACCESS_TOKEN_SECRET
|
|
70
|
+ config.consumer_key = key
|
|
71
|
+ config.consumer_secret = secret
|
|
72
|
+ config.access_token = token
|
|
73
|
+ config.access_token_secret = token_secret
|
115
|
74
|
end
|
116
|
75
|
end
|
117
|
76
|
|
|
@@ -120,7 +79,13 @@ class TwitterService
|
120
|
79
|
end
|
121
|
80
|
end
|
122
|
81
|
|
123
|
|
-youtube = YouTubeService.new
|
124
|
|
-filepath = youtube.fetch_random_frame_from_random_video
|
125
|
|
-TwitterService.new.tweet_image(filepath)
|
126
|
|
-youtube.delete_file(filepath)
|
|
82
|
+youtube = YouTubeService.new(PLAYLIST_ID)
|
|
83
|
+youtube.fetch_random_frame_of_random_video
|
|
84
|
+twitter = TwitterService.new(
|
|
85
|
+ TWITTER_CONSUMER_KEY,
|
|
86
|
+ TWITTER_CONSUMER_SECRET,
|
|
87
|
+ TWITTER_ACCESS_TOKEN,
|
|
88
|
+ TWITTER_ACCESS_TOKEN_SECRET,
|
|
89
|
+)
|
|
90
|
+twitter.tweet_image(FRAME_PATH)
|
|
91
|
+youtube.delete_frame
|