Debian Codegraph @

plugin/aspectframe/aspectframe.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require 'fileutils'
3
4 =begin
5 このファイルを見た人へ
6 このことを絶対誰にも言わないでください
7
8 どうしても言いたい人は、毎年4/1(JST)の間だけはいいですよ
9
10 としぁ
11 =end
12
13 module Plugin::AspectFrame
14 def self.now
15 Time.new end
16
17 Sequence = Data.define(:slug, :range) do
18 def ===(other)
19 case other
20 when Sequence
21 self == other
22 when Symbol, String
23 slug == other.to_sym
24 when Time
25 range.include? other end end end
26 THE_DAY = Time.new(now.year, 4, 1)..Time.new(now.year, 4, 2)
27 SequenceTrueMikutter = Sequence.new(:true_mikutter, THE_DAY.first..Time.new(now.year, 4, 1, 12))
28 SequenceGermanyBird = Sequence.new(:germany_bird, Time.new(now.year, 4, 1, 12)..THE_DAY.last)
29 SCHEDULE = if Cairo::VERSION <= [1, 17, 6]
30 [SequenceTrueMikutter, SequenceGermanyBird]
31 else
32 [SequenceTrueMikutter]
33 end
34 PREFETCH = Time.new(now.year, 3, 1)..THE_DAY.first
35
36 CACHE_DIR = File.expand_path(File.join(Environment::CACHE, "af"))
37 end
38
39 Plugin.create :aspectframe do
40
41 def now
42 Plugin::AspectFrame.now
43 end
44
45 if Plugin::AspectFrame::PREFETCH.cover?(now)
46 FileUtils.mkdir_p(Plugin::AspectFrame::CACHE_DIR)
47 # prefetch
48 on_gui_timeline_add_messages do |_tl, messages|
49 if Plugin::AspectFrame::PREFETCH.cover?(now)
50 messages.deach do |message|
51 if rand(1000) < now.day**2
52 Plugin.call(:image_file_cache_photo, transform(message.user.icon))
53 end
54 end
55 end
56 end
57 end
58 if FileTest.exist?(Plugin::AspectFrame::CACHE_DIR) and not (Plugin::AspectFrame::PREFETCH.first..Plugin::AspectFrame::THE_DAY.last).cover?(now)
59 notice 'destroy all prefetch data'
60 FileUtils.rm_rf(Plugin::AspectFrame::CACHE_DIR) end
61
62 # return fetched data
63 filter_image_cache do |url, image, &stop|
64 if /https:\/\/mikutter\.hachune\.net\/img\/api\/([0-9A-F]{2})\.png/ =~ url
65 local = localfile_hash($1)
66 if FileTest.exist? local
67 raw = file_get_contents(local)
68 if raw and not raw.empty?
69 stop.call([url, raw]) end end end
70 [url, image] end
71
72 filter_miracle_icon_filter do |photo|
73 if enable_sequence? :true_mikutter
74 [transform(photo)]
75 else
76 [photo] end end
77
78 filter_main_icon_form do |form|
79 if enable_sequence? :germany_bird
80 [:aspectframe]
81 else
82 [form] end end
83
84 def transform(icon)
85 if icon.perma_link
86 Plugin.collect(:photo_filter, "https://mikutter.hachune.net/img/api/#{Digest::MD5.hexdigest(icon.perma_link.to_s)[0,2].upcase}.png", Pluggaloid::COLLECT).first
87 else
88 icon
89 end
90 end
91
92 def localfile_hash(hash)
93 File.expand_path(File.join(Plugin::AspectFrame::CACHE_DIR, "#{hash.upcase}_5.png")) end
94
95 def the_day?
96 Plugin::AspectFrame::THE_DAY.cover?(now) end
97
98 def enable_sequence
99 Plugin::AspectFrame::SCHEDULE.lazy.select{|s| s.range.cover? now} end
100
101 def enable_sequence?(seq)
102 enable_sequence.map(&:slug).include? seq end
103
104 end