Class: Stashboxr::File
- Inherits:
-
Object
- Object
- Stashboxr::File
- Defined in:
- lib/stashboxr.rb
Constant Summary collapse
- SIZES =
{ '' => 9.765625e-4, 'K' => 1, 'M' => 1024 }
Instance Attribute Summary collapse
-
#autosave ⇒ Object
Returns the value of attribute autosave.
-
#description ⇒ Object
Returns the value of attribute description.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#know_code ⇒ Object
readonly
Returns the value of attribute know_code.
-
#rating ⇒ Object
readonly
Returns the value of attribute rating.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#title ⇒ Object
Returns the value of attribute title.
-
#unsaved ⇒ Object
readonly
Returns the value of attribute unsaved.
-
#uploaded_on ⇒ Object
readonly
Returns the value of attribute uploaded_on.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#views ⇒ Object
readonly
Returns the value of attribute views.
Class Method Summary collapse
-
.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).
Instance Method Summary collapse
-
#add_tag(new_tags) ⇒ Object
(also: #add_tags)
Add tags to the file on stashbox.
-
#delete ⇒ Object
Delete this file (permanently!) from stashbox.org.
- #download ⇒ Object
-
#has_metadata? ⇒ Boolean
Has metadata been loaded.
-
#initialize(url) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #public=(ispublic) ⇒ Object
- #public? ⇒ Boolean
-
#refresh(doc = nil) ⇒ Object
Grab metadata from stashbox.org.
-
#remove_tag(new_tags) ⇒ Object
(also: #remove_tags)
Remove tags from the file on stashbox.
-
#save ⇒ Object
Save the metadata to Stashboxr.
- #sfw=(sfw) ⇒ Object
- #sfw? ⇒ Boolean
- #tags ⇒ Object
-
#tags=(new_tags) ⇒ Object
Reset the tags for this file to the ones in the given array.
-
#unsaved? ⇒ Boolean
Returns true if there are unsaved changes to the metadata for the file.
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
#autosave ⇒ Object
Returns the value of attribute autosave.
74 75 76 |
# File 'lib/stashboxr.rb', line 74 def autosave @autosave end |
#description ⇒ Object
Returns the value of attribute description.
73 74 75 |
# File 'lib/stashboxr.rb', line 73 def description @description end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
71 72 73 |
# File 'lib/stashboxr.rb', line 71 def filename @filename end |
#know_code ⇒ Object (readonly)
Returns the value of attribute know_code.
72 73 74 |
# File 'lib/stashboxr.rb', line 72 def know_code @know_code end |
#rating ⇒ Object (readonly)
Returns the value of attribute rating.
72 73 74 |
# File 'lib/stashboxr.rb', line 72 def @rating end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
72 73 74 |
# File 'lib/stashboxr.rb', line 72 def size @size end |
#title ⇒ Object
Returns the value of attribute title.
73 74 75 |
# File 'lib/stashboxr.rb', line 73 def title @title end |
#unsaved ⇒ Object (readonly)
Returns the value of attribute unsaved.
71 72 73 |
# File 'lib/stashboxr.rb', line 71 def unsaved @unsaved end |
#uploaded_on ⇒ Object (readonly)
Returns the value of attribute uploaded_on.
72 73 74 |
# File 'lib/stashboxr.rb', line 72 def uploaded_on @uploaded_on end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
71 72 73 |
# File 'lib/stashboxr.rb', line 71 def url @url end |
#views ⇒ Object (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)
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 to the file on stashbox
136 137 138 139 140 141 142 |
# File 'lib/stashboxr.rb', line 136 def add_tag() # TODO: Parse input tags? @tags = ( + ()).uniq @saved = false save if @autosave @tags end |
#delete ⇒ Object
Delete this file (permanently!) from stashbox.org
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 |
#download ⇒ Object
201 202 203 |
# File 'lib/stashboxr.rb', line 201 def download Agent.get(url).body end |
#has_metadata? ⇒ Boolean
Has metadata been loaded
119 |
# File 'lib/stashboxr.rb', line 119 def ; @metadata; end |
#inspect ⇒ Object
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
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 from the file on stashbox
146 147 148 149 150 151 |
# File 'lib/stashboxr.rb', line 146 def remove_tag() @tags = ( - ()).uniq @saved = false save if @autosave @tags end |
#save ⇒ Object
Save the metadata to Stashboxr. Called automatically with any changes by default.
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
132 |
# File 'lib/stashboxr.rb', line 132 def sfw?; refresh if not @metadata; @sfw; end |
#tags ⇒ Object
125 |
# File 'lib/stashboxr.rb', line 125 def ; 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 = () @saved = false save if @autosave @tags end |
#unsaved? ⇒ Boolean
Returns true if there are unsaved changes to the metadata for the file
123 |
# File 'lib/stashboxr.rb', line 123 def unsaved?; !@saved; end |