2 Commits

Author SHA1 Message Date
  Dylan Baker e163e99577 Use tumblr instead of twitter 2 years ago
  Dylan Baker 40ed033686 Remove Gemfile.lock 2 years ago
5 changed files with 62 additions and 105 deletions
  1. 12
    0
      .env.example
  2. 3
    0
      .gitignore
  3. 1
    1
      Gemfile
  4. 0
    50
      Gemfile.lock
  5. 46
    54
      cc.rb

+ 12
- 0
.env.example View File

@@ -0,0 +1,12 @@
1
+TWITTER_CONSUMER_KEY=
2
+TWITTER_CONSUMER_SECRET=
3
+TWITTER_ACCESS_TOKEN=
4
+TWITTER_ACCESS_TOKEN_SECRET=
5
+
6
+TUMBLR_BLOG_URL=
7
+TUMBLR_API_KEY=
8
+TUMBLR_API_SECRET=
9
+TUMBLR_ACCESS_TOKEN=
10
+TUMBLR_ACCESS_TOKEN_SECRET=
11
+
12
+PLAYLIST_ID=

+ 3
- 0
.gitignore View File

@@ -1 +1,4 @@
1 1
 .env
2
+vendor/
3
+Gemfile.lock
4
+.bundle

+ 1
- 1
Gemfile View File

@@ -5,5 +5,5 @@ source "https://rubygems.org"
5 5
 git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6 6
 
7 7
 gem "dotenv", "~> 2.7"
8
-
9 8
 gem "twitter", "~> 6.2"
9
+gem "tumblr_client", "~> 0.8.5"

+ 0
- 50
Gemfile.lock View File

@@ -1,50 +0,0 @@
1
-GEM
2
-  remote: https://rubygems.org/
3
-  specs:
4
-    addressable (2.6.0)
5
-      public_suffix (>= 2.0.2, < 4.0)
6
-    buftok (0.2.0)
7
-    domain_name (0.5.20190701)
8
-      unf (>= 0.0.5, < 1.0.0)
9
-    dotenv (2.7.5)
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)
22
-    multipart-post (2.1.1)
23
-    naught (1.1.0)
24
-    public_suffix (3.1.1)
25
-    simple_oauth (0.3.1)
26
-    thread_safe (0.3.6)
27
-    twitter (6.2.0)
28
-      addressable (~> 2.3)
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)
41
-
42
-PLATFORMS
43
-  ruby
44
-
45
-DEPENDENCIES
46
-  dotenv (~> 2.7)
47
-  twitter (~> 6.2)
48
-
49
-BUNDLED WITH
50
-   2.0.2

+ 46
- 54
cc.rb View File

@@ -1,53 +1,65 @@
1
-require 'dotenv/load'
1
+require 'dotenv'
2 2
 require 'json'
3 3
 require 'open-uri'
4
-require 'twitter'
5 4
 
6 5
 Dotenv.load
7 6
 
8
-TWITTER_CONSUMER_KEY = ENV["TWITTER_CONSUMER_KEY"]
9
-TWITTER_CONSUMER_SECRET = ENV["TWITTER_CONSUMER_SECRET"]
10
-TWITTER_ACCESS_TOKEN = ENV["TWITTER_ACCESS_TOKEN"]
11
-TWITTER_ACCESS_TOKEN_SECRET = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
12
-
13
-YOUTUBE_DL_PATH = `which youtube-dl`.chomp
14
-FFMPEG_PATH = `which ffmpeg`.chomp
15
-FFPROBE_PATH = `which ffprobe`.chomp
16
-
17
-VIDEO_PATH = "/tmp/video.mp4"
18
-FRAME_PATH = "/tmp/frame.png"
19
-
20
-PLAYLIST_ID = ENV["PLAYLIST_ID"]
21
-
22
-throw "TWITTER_CONSUMER_KEY is required" unless TWITTER_CONSUMER_KEY
23
-throw "TWITTER_CONSUMER_SECRET is required" unless TWITTER_CONSUMER_SECRET
24
-throw "TWITTER_ACCESS_TOKEN is required" unless TWITTER_ACCESS_TOKEN
25
-throw "TWITTER_ACCESS_TOKEN_SECRET is required" unless TWITTER_ACCESS_TOKEN_SECRET
26
-
27
-throw "PLAYLIST_ID is required" unless PLAYLIST_ID
7
+class TumblrService
8
+  TUMBLR_API_KEY = ENV["TUMBLR_API_KEY"]
9
+  TUMBLR_API_SECRET = ENV["TUMBLR_API_SECRET"]
10
+  TUMBLR_ACCESS_TOKEN = ENV["TUMBLR_ACCESS_TOKEN"]
11
+  TUMBLR_ACCESS_TOKEN_SECRET = ENV["TUMBLR_ACCESS_TOKEN_SECRET"]
12
+  TUMBLR_BLOG_URL = ENV["TUMBLR_BLOG_URL"]
13
+
14
+  def initialize
15
+    raise "TUMBLR_API_KEY is required" unless TUMBLR_API_KEY
16
+    raise "TUMBLR_API_SECRET is required" unless TUMBLR_API_SECRET
17
+    raise "TUMBLR_ACCESS_TOKEN is required" unless TUMBLR_ACCESS_TOKEN
18
+    raise "TUMBLR_ACCESS_TOKEN_SECRET is required" unless TUMBLR_ACCESS_TOKEN_SECRET
19
+    raise "TUMBLR_BLOG_URL is required" unless TUMBLR_BLOG_URL
20
+
21
+    @tumblr = Tumblr::Client.new(
22
+      :consumer_key => TUMBLR_API_KEY,
23
+      :consumer_secret => TUMBLR_API_SECRET,
24
+      :oauth_token => TUMBLR_ACCESS_TOKEN,
25
+      :oauth_token_secret => TUMBLR_ACCESS_TOKEN_SECRET,
26
+    )
27
+  end
28 28
 
29
-throw "Can't find youtube-dl" if YOUTUBE_DL_PATH.empty?
30
-throw "Can't find ffprobe" if FFPROBE_PATH.empty?
31
-throw "Can't find ffmpeg" if FFMPEG_PATH.empty?
29
+  def post_image(image_path)
30
+    @tumblr.photo(TUMBLR_BLOG_URL, {:data => [image_path]})
31
+  end
32
+end
32 33
 
33 34
 class YouTubeService
34
-  def initialize(playlist_id)
35
-    @playlist_id = playlist_id
35
+  YTDLP_PATH = `which yt-dlp`.chomp
36
+  FFMPEG_PATH = `which ffmpeg`.chomp
37
+  FFPROBE_PATH = `which ffprobe`.chomp
38
+  VIDEO_PATH = "/tmp/video.mp4"
39
+  FRAME_PATH = "/tmp/frame.png"
40
+  PLAYLIST_ID = ENV["PLAYLIST_ID"]
41
+
42
+  def initialize
43
+    raise "PLAYLIST_ID is required" unless PLAYLIST_ID
44
+    raise "Can't find youtube-dl" if YTDLP_PATH.empty?
45
+    raise "Can't find ffprobe" if FFPROBE_PATH.empty?
46
+    raise "Can't find ffmpeg" if FFMPEG_PATH.empty?
36 47
   end
37 48
 
38 49
   def fetch_random_frame_of_random_video
39 50
     download_random_video
40 51
     extract_frame
41 52
     delete_video
53
+
54
+    FRAME_PATH
42 55
   end
43 56
 
44 57
   def download_random_video
45
-    `#{YOUTUBE_DL_PATH} --playlist-random --max-downloads 1 -o #{VIDEO_PATH} -f mp4 #{@playlist_id}`
58
+    `#{YTDLP_PATH} --playlist-random --max-downloads 1 -o #{VIDEO_PATH} -f mp4 #{PLAYLIST_ID}`
46 59
   end
47 60
 
48 61
   def extract_frame
49
-    length = get_video_length
50
-    random_second = rand(0..length)
62
+    random_second = rand(0..get_video_length)
51 63
     `#{FFMPEG_PATH} -v error -ss #{random_second} -i #{VIDEO_PATH} -frames 1 -y #{FRAME_PATH}`
52 64
   end
53 65
 
@@ -64,28 +76,8 @@ class YouTubeService
64 76
   end
65 77
 end
66 78
 
67
-class TwitterService
68
-  def initialize(key, secret, token, token_secret)
69
-    @twitter = Twitter::REST::Client.new do |config|
70
-      config.consumer_key = key
71
-      config.consumer_secret = secret
72
-      config.access_token = token
73
-      config.access_token_secret = token_secret
74
-    end
75
-  end
76
-
77
-  def tweet_image(filepath)
78
-    @twitter.update_with_media(String.new, File.new(filepath))
79
-  end
80
-end
81
-
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)
79
+youtube = YouTubeService.new
80
+image_path = youtube.fetch_random_frame_of_random_video
81
+tumblr = TumblrService.new
82
+tumblr.post_image(image_path)
91 83
 youtube.delete_frame

Loading…
Cancel
Save