Class: Descartes::Pigro

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/descartes/modules/pigro.rb

Instance Method Summary collapse

Instance Method Details

#by_show(m, keyword) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/descartes/modules/pigro.rb', line 61

def by_show(m, keyword)
  s       = keyword.split
  n_ep    = (s.last == 'last' || s.last.numeric?) ? s.pop : nil
  keyword = s.join ' '

  host     = get_host
  shows    = Assonnato.new(host).show
  episodes = Assonnato.new(host).episode

  series = shows.search keyword

  if series.empty?
    m.reply 'Series not found.'
  else
    series.take(5).each { |s|
      show = shows.get(s.name).first

      if n_ep.nil?
        a = show.status.downcase.start_with?(?o) ?  'an' : ?a
        m.reply "[#{show.fansub.colorize}] #{show.name.colorize} is #{a} #{show.status} series of #{show.tot_episodes.colorize} episodes."
        m.reply ''.tap { |staff|
          {
            :Translator => show.translator,
            :Editor     => show.editor,
            :Checker    => show.checker,
            :Timer      => show.timer,
            :Typesetter => show.typesetter,
            :Encoder    => show.encoder,
            :QC         => show.qchecker
          }.each_pair { |key, val|
            staff << "#{key.to_s.colorize}: #{val} / " unless val.empty?
          }
        }[0..-4]
      elsif n_ep == 'last'
        episode = episodes.last(:ongoing).select { |ep| ep.show_name == s.name }.last

        if episode
          m.reply ("#{show.name.colorize} ##{episode.episode}".colorize + ' - ').tap { |staff|
            activities = {
              :Translation => episode.translation,
              :Editing     => episode.editing,
              :Check       => episode.checking,
              :Timing      => episode.timing,
              :Typesetting => episode.typesetting,
              :Encoding    => episode.encoding,
              :QC          => episode.qchecking
            }

            if activities.select { |k, v| v != :done }.any?
              staff << ''.tap { |s|
                activities.each_pair { |key, val|
                  s << "#{key.to_s.colorize}: #{val} / "
                }
              }[0..-3]
            else
              staff << 'Already released. '
            end
            staff << "#{'Download'.colorize}: #{episode.download}" unless episode.download.strip.empty?
          }
        else
          m.reply "#{show.name.colorize} has no episodes yet."
        end
      else
        episodes = episodes.get show.name, n_ep.to_i

        if episodes.any?
          episodes.each { |ep|
            m.reply ("#{show.name.colorize} ##{ep.episode}".colorize + ' - ').tap { |staff|
              activities = {
                :Translation => ep.translation,
                :Editing     => ep.editing,
                :Check       => ep.checking,
                :Timing      => ep.timing,
                :Typesetting => ep.typesetting,
                :Encoding    => ep.encoding,
                :QC          => ep.qchecking
              }

              if activities.select { |k, v| v != :done }.any?
                staff << ''.tap { |s|
                  activities.each_pair { |key, val|
                    s << "#{key.to_s.colorize}: #{val} / "
                  }
                }[0..-3]
              else
                staff << 'Already released. '
              end
              staff << "#{'Download'.colorize}: #{ep.download}" unless ep.download.strip.empty?
            }
          }
        elsif n_ep.to_i > show.tot_episodes
          m.reply "#{m.user.nick.colorize} pls go and http://just-believe.in."
        else
          m.reply "The episode #{n_ep.colorize} has not been added yet to #{show.name.colorize}."
        end
      end
    }
  end
end

#by_staff(m, user) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/descartes/modules/pigro.rb', line 23

def by_staff(m, user)
  user = user.split ' '
  role = user.last.to_sym.downcase

  if [ :translator, :editor, :checker, :timer, :typesetter, :encoder, :qchecker ].include? role
    user.pop
    options = { user: user.join(' '), role: role }
  else
    options = { user: user.join(' ') }
  end

  host  = get_host
  shows = Assonnato.new(host).show

  series = shows.all(:ongoing, options) + shows.all(:dropped, options) + shows.all(:finished, options)

  if series.empty?
    if options.has_key? :role
      m.reply "#{options[:user].colorize} hasn't worked to any series as #{options[:role].colorize}."
    else
      m.reply "#{options[:user].colorize} hasn't worked to any series."
    end

    return
  end

  m.reply ''.tap { |str|
    if options.has_key? :role
      str << "#{series.first.send(options[:role]).colorize} has worked as #{options[:role].colorize} on "
    else
      str << "#{options[:user].colorize} has worked on "
    end

    series.each { |s| str << "#{s.name.colorize}, " }
  }[0..-3]
end

#episode(m, things) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/descartes/modules/pigro.rb', line 162

def episode(m, things)
  unless m.user.authed?
    m.reply 'You are not authorized to do this.'
    return
  end

  things = things.split ' '
  len    = things.length

  if things.last.numeric?
    episode = things.pop
    show    = things.join ' '
  elsif len >= 4
    status  = things.pop
    field   = things.pop
    episode = things.pop
    show    = things.join ' '
  else
    m.reply 'usage: !pigro SHOW EPISODE [FIELD] [STATUS]'
    return
  end

  host      = get_host
  user      = get_user m.user.nick
  assonnato = Assonnato.new host

  series = assonnato.show.search show
  if series.length != 1
    m.reply 'You should refine your search.'
    return
  else
    show = series.first.name
  end

  if user
     = assonnato.user. user['username'], user['password']
    if ['status'] == 'error'
      m.reply ['message']
    else
      ep = assonnato.episode.add show, episode.to_i

      if !field || !status
        fails = []
        [ :translation, :editing, :checking, :timing, :typesetting, :encoding, :qchecking ].each do |f|
          ep = assonnato.episode.edit show, episode.to_i, { f => :done }
          fails << [f, ep['message']] if ep['status'] != 'success'
        end

        if fails.length == 7
          m.reply "The episode has not been edited. Maybe it doesn't exist yet?"
        elsif fails.any?
          fails.each { |field, error| m.reply("#{field}: #{ep['message']}") }
        else
          m.reply "The episode has been edited."
        end
      else
        ep = assonnato.episode.edit show, episode.to_i, { field.to_sym => status.to_sym }
        m.reply ep['message']
      end

      assonnato.user.logout
    end
  else
    m.reply 'You are not recognized.'
  end
end

#get_hostObject



242
243
244
245
246
247
248
249
250
251
# File 'lib/descartes/modules/pigro.rb', line 242

def get_host
  file = File.join $options[:dotfiles], 'pigro.txt'

  if File.exists? file
    url = File.read(file).strip
    return url unless url.empty?
  end

  'pigro.omnivium.it'
end

#get_user(nickname) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/descartes/modules/pigro.rb', line 229

def get_user(nickname)
  file = File.join $options[:dotfiles], 'pigro.json'

  if File.exists? file
    users = JSON.parse File.read(file)
    users.select { |user|
      user['nicknames'].select { |n| n['nickname'] == nickname }
    }.last
  else
    false
  end
end