Class: Stashboxr::File

Inherits:
Object
  • Object
show all
Defined in:
lib/stashboxr.rb

Constant Summary collapse

SIZES =
{
  '' => 9.765625e-4,
  'K' => 1,
  'M' => 1024
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ File

Returns a new instance of File.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/stashboxr.rb', line 102

def initialize(url)
  
  if (url.is_a? Mechanize::Page)
    # We're being initialized with an information page (ie. an upload) make use of it!
    refresh(url)
  elsif (url =~ /(?:http:\/\/)?(?:stashbox\.org)?(?:\/v)?\/?([0-9]+\/(.+))$/)
    @stashboxid = $1
    @filename = URI.decode($2)
  else
    raise RuntimeError, "That isn't a valid stashbox URL"
  end
  
  @autosave = Stashboxr.autosave?
  @saved = true
end

Instance Attribute Details

#autosaveObject

Returns the value of attribute autosave.



74
75
76
# File 'lib/stashboxr.rb', line 74

def autosave
  @autosave
end

#descriptionObject

Returns the value of attribute description.



73
74
75
# File 'lib/stashboxr.rb', line 73

def description
  @description
end

#filenameObject (readonly)

Returns the value of attribute filename.



71
72
73
# File 'lib/stashboxr.rb', line 71

def filename
  @filename
end

#know_codeObject (readonly)

Returns the value of attribute know_code.



72
73
74
# File 'lib/stashboxr.rb', line 72

def know_code
  @know_code
end

#ratingObject (readonly)

Returns the value of attribute rating.



72
73
74
# File 'lib/stashboxr.rb', line 72

def rating
  @rating
end

#sizeObject (readonly)

Returns the value of attribute size.



72
73
74
# File 'lib/stashboxr.rb', line 72

def size
  @size
end

#titleObject

Returns the value of attribute title.



73
74
75
# File 'lib/stashboxr.rb', line 73

def title
  @title
end

#unsavedObject (readonly)

Returns the value of attribute unsaved.



71
72
73
# File 'lib/stashboxr.rb', line 71

def unsaved
  @unsaved
end

#uploaded_onObject (readonly)

Returns the value of attribute uploaded_on.



72
73
74
# File 'lib/stashboxr.rb', line 72

def uploaded_on
  @uploaded_on
end

#urlObject (readonly)

Returns the value of attribute url.



71
72
73
# File 'lib/stashboxr.rb', line 71

def url
  @url
end

#viewsObject (readonly)

Returns the value of attribute views.



72
73
74
# File 'lib/stashboxr.rb', line 72

def views
  @views
end

Class Method Details

.upload(local_file, is_public = true) ⇒ Object

Upload a file to stashbox and return a Stashbox::File object referencing it (which can be used to edit the metadata)

Raises:

  • (RuntimeError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stashboxr.rb', line 83

def self.upload(local_file,is_public = true)
  # Does file exist?
  raise RuntimeError, "This file does not exist - I can't upload it!" if !::File.exists? local_file
  # Upload the file
  res = Agent.post('http://stashbox.org/upload.php',{
    :upload_type => "local_file",
    :file => open(local_file),
    :is_public => is_public
  })
  
  reason = res.search('//div[@class=\'error\']').inner_text
  
  if reason.empty?
    new(res)
  else
    raise RuntimeError, "The upload wasn't allowed"<<((reason.nil?) ? "" : " because '#{reason}'")
  end
end

Instance Method Details

#add_tag(new_tags) ⇒ Object Also known as: add_tags

Add tags to the file on stashbox



136
137
138
139
140
141
142
# File 'lib/stashboxr.rb', line 136

def add_tag(new_tags)
  # TODO: Parse input tags?
  @tags = (tags + parse_tags(new_tags)).uniq
  @saved = false
  save if @autosave
  @tags
end

#deleteObject

Delete this file (permanently!) from stashbox.org

Raises:

  • (RuntimeError)


206
207
208
209
210
211
212
# File 'lib/stashboxr.rb', line 206

def delete
  res = Agent.post("http://stashbox.org/delete_upload/"<<@stashboxid)
  
  raise RuntimeError, "You don't have permission to delete this file" if res.body =~ /don't have permission/
  
  return res.code == "200"
end

#downloadObject



201
202
203
# File 'lib/stashboxr.rb', line 201

def download
  Agent.get(url).body
end

#has_metadata?Boolean

Has metadata been loaded

Returns:

  • (Boolean)


119
# File 'lib/stashboxr.rb', line 119

def has_metadata?;   @metadata;                              end

#inspectObject



214
215
216
217
218
219
220
221
222
# File 'lib/stashboxr.rb', line 214

def inspect
  if @metadata
    keys = [@public ? "public" : "private"]
    keys.push("nsfw") if !@sfw
    "<Stash: #{@filename} (#{keys * ", "})>"
  else
    "<Stash: #{@filename}>"
  end
end

#public=(ispublic) ⇒ Object



180
181
182
183
184
# File 'lib/stashboxr.rb', line 180

def public=(ispublic)
  @public = (ispublic == true)
  @saved = false
  save if @autosave
end

#public?Boolean

Returns:

  • (Boolean)


133
# File 'lib/stashboxr.rb', line 133

def public?;         refresh if not @metadata; @public;      end

#refresh(doc = nil) ⇒ Object

Grab metadata from stashbox.org



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/stashboxr.rb', line 225

def refresh(doc = nil)
  # Get extended information
  doc ||= Agent.get "http://stashbox.org/v/"<<@stashboxid
  
  doc.search("//div[@class='subsection']").each do |sub|
    value = sub.search("div[@class='value']").inner_text.strip
    case sub.search("div[@class='label']").inner_text
    when "Filename"
      @filename = value
      @stashboxid = URI.parse(sub.search("div[@class='value']/a")[0]['href']).path[1..-1]
    when "Size"
      @size = value[/^(\d+\.\d+)\ ([K|M]?)B$/,1].to_f * SIZES[$2]
    when "Uploaded On"
      @uploaded_on = Time.parse(value)
    when "Views"
      @size = value[/^(\d+)\ \(/,1].to_i
    when "Rating"
      @rating = Percentage.new(value[/(\d+\.\d+)\/5\ $/,1].to_f/5.0)
    when "Tags"
      @tags = sub.search("div[@class='value']/a").collect do |tag|
        tag.inner_text
      end
    when "Title"
      if sub.search("div[@class='value']/i").inner_text == ""
        @title = value
      else
        @title = nil
      end
    when "KnowCode"
      @know_code = value
    when "Description"
      if sub.search("div[@class='value']/i").inner_text == ""
        @description = value
      else
        @description = nil
      end
    when "Is this file work safe?"
      @sfw = (value == "Yes")
    end
  end
  @metadata = true
  return
end

#remove_tag(new_tags) ⇒ Object Also known as: remove_tags

Remove tags from the file on stashbox



146
147
148
149
150
151
# File 'lib/stashboxr.rb', line 146

def remove_tag(new_tags)
  @tags = (tags - parse_tags(new_tags)).uniq
  @saved = false
  save if @autosave
  @tags
end

#saveObject

Save the metadata to Stashboxr. Called automatically with any changes by default.

Raises:

  • (RuntimeError)


187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/stashboxr.rb', line 187

def save
  res = Agent.post("http://stashbox.org/manage_file/"<<@stashboxid,{
    :tags => @tags * " ",
    :meta_title => @title,
    :meta_description => @description,
    :is_nsfw => !@sfw,
    :is_public => @public
  })
  
  raise RuntimeError, "You don't have permission to edit this file" if res.body =~ /don't have permission/
  
  return res.code == "200"
end

#sfw=(sfw) ⇒ Object



174
175
176
177
178
# File 'lib/stashboxr.rb', line 174

def sfw=(sfw)
  @sfw = (sfw == true)
  @saved = false
  save if @autosave
end

#sfw?Boolean

Returns:

  • (Boolean)


132
# File 'lib/stashboxr.rb', line 132

def sfw?;            refresh if not @metadata; @sfw;         end

#tagsObject



125
# File 'lib/stashboxr.rb', line 125

def tags;            refresh if not @metadata; @tags;        end

#tags=(new_tags) ⇒ Object

Reset the tags for this file to the ones in the given array



155
156
157
158
159
160
# File 'lib/stashboxr.rb', line 155

def tags=(new_tags)
  @tags = parse_tags(new_tags)
  @saved = false
  save if @autosave
  @tags
end

#unsaved?Boolean

Returns true if there are unsaved changes to the metadata for the file

Returns:

  • (Boolean)


123
# File 'lib/stashboxr.rb', line 123

def unsaved?;        !@saved;                                end