Browse Source

Use tumblr instead of twitter

master
Dylan Baker 2 years ago
parent
commit
f6ffc0626d
4 changed files with 57 additions and 55 deletions
  1. 7
    0
      .env.example
  2. 3
    0
      .gitignore
  3. 1
    1
      Gemfile
  4. 46
    54
      cc.rb

+ 7
- 0
.env.example View File

@@ -0,0 +1,7 @@
1
+TUMBLR_BLOG_URL=
2
+TUMBLR_API_KEY=
3
+TUMBLR_API_SECRET=
4
+TUMBLR_ACCESS_TOKEN=
5
+TUMBLR_ACCESS_TOKEN_SECRET=
6
+
7
+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"

+ 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