Browse Source

Remove Google API gem

master
Dylan Baker 4 years ago
parent
commit
7723ebf2fb
3 changed files with 74 additions and 115 deletions
  1. 1
    3
      Gemfile
  2. 33
    37
      Gemfile.lock
  3. 40
    75
      cc.rb

+ 1
- 3
Gemfile View File

@@ -4,8 +4,6 @@ source "https://rubygems.org"
4 4
 
5 5
 git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6 6
 
7
-# gem "rails"
8
-
9 7
 gem "dotenv", "~> 2.7"
10 8
 
11
-gem "google-api-client", "~> 0.30.8"
9
+gem "twitter", "~> 6.2"

+ 33
- 37
Gemfile.lock View File

@@ -3,52 +3,48 @@ GEM
3 3
   specs:
4 4
     addressable (2.6.0)
5 5
       public_suffix (>= 2.0.2, < 4.0)
6
-    declarative (0.0.10)
7
-    declarative-option (0.1.0)
6
+    buftok (0.2.0)
7
+    domain_name (0.5.20190701)
8
+      unf (>= 0.0.5, < 1.0.0)
8 9
     dotenv (2.7.5)
9
-    faraday (0.15.4)
10
-      multipart-post (>= 1.2, < 3)
11
-    google-api-client (0.30.8)
12
-      addressable (~> 2.5, >= 2.5.1)
13
-      googleauth (>= 0.5, < 0.10.0)
14
-      httpclient (>= 2.8.1, < 3.0)
15
-      mini_mime (~> 1.0)
16
-      representable (~> 3.0)
17
-      retriable (>= 2.0, < 4.0)
18
-      signet (~> 0.10)
19
-    googleauth (0.9.0)
20
-      faraday (~> 0.12)
21
-      jwt (>= 1.4, < 3.0)
22
-      memoist (~> 0.16)
23
-      multi_json (~> 1.11)
24
-      os (>= 0.9, < 2.0)
25
-      signet (~> 0.7)
26
-    httpclient (2.8.3)
27
-    jwt (2.2.1)
28
-    memoist (0.16.0)
29
-    mini_mime (1.0.2)
30
-    multi_json (1.13.1)
10
+    equalizer (0.0.11)
11
+    http (3.3.0)
12
+      addressable (~> 2.3)
13
+      http-cookie (~> 1.0)
14
+      http-form_data (~> 2.0)
15
+      http_parser.rb (~> 0.6.0)
16
+    http-cookie (1.0.3)
17
+      domain_name (~> 0.5)
18
+    http-form_data (2.1.1)
19
+    http_parser.rb (0.6.0)
20
+    memoizable (0.4.2)
21
+      thread_safe (~> 0.3, >= 0.3.1)
31 22
     multipart-post (2.1.1)
32
-    os (1.0.1)
23
+    naught (1.1.0)
33 24
     public_suffix (3.1.1)
34
-    representable (3.0.4)
35
-      declarative (< 0.1.0)
36
-      declarative-option (< 0.2.0)
37
-      uber (< 0.2.0)
38
-    retriable (3.1.2)
39
-    signet (0.11.0)
25
+    simple_oauth (0.3.1)
26
+    thread_safe (0.3.6)
27
+    twitter (6.2.0)
40 28
       addressable (~> 2.3)
41
-      faraday (~> 0.9)
42
-      jwt (>= 1.5, < 3.0)
43
-      multi_json (~> 1.10)
44
-    uber (0.1.0)
29
+      buftok (~> 0.2.0)
30
+      equalizer (~> 0.0.11)
31
+      http (~> 3.0)
32
+      http-form_data (~> 2.0)
33
+      http_parser.rb (~> 0.6.0)
34
+      memoizable (~> 0.4.0)
35
+      multipart-post (~> 2.0)
36
+      naught (~> 1.0)
37
+      simple_oauth (~> 0.3.0)
38
+    unf (0.1.4)
39
+      unf_ext
40
+    unf_ext (0.0.7.6)
45 41
 
46 42
 PLATFORMS
47 43
   ruby
48 44
 
49 45
 DEPENDENCIES
50 46
   dotenv (~> 2.7)
51
-  google-api-client (~> 0.30.8)
47
+  twitter (~> 6.2)
52 48
 
53 49
 BUNDLED WITH
54
-   2.0.1
50
+   2.0.2

+ 40
- 75
cc.rb View File

@@ -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

Loading…
Cancel
Save