This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch_originals.rb
executable file
·94 lines (72 loc) · 1.86 KB
/
fetch_originals.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
59
60
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
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'sqlite3'
require 'fileutils'
def fetch_orignals(db)
filenames = {}
filetypes = ['jpg', 'png', 'gif']
images_sql = "SELECT id, orig_src from images"
db.execute( images_sql ) do |row|
id = row[0]
src = row[1]
# this needs work
filetype = src.split('.')[-1][0,3]
filetype.downcase!
if not filetypes.include? filetype
filetype = "jpg"
end
filename = id+"."+filetype
url = "http://" + src
download_file(url, filename, id)
end
end
def download_file(url, filename, id)
# does it exist?
if File.exist?('images/originals/'+filename)
puts '- already downloaded '+url+' to '+filename
return
end
begin
image_data = open(url).read
rescue
puts ' error with '+url
return
end
puts "image #{id}"
puts "remote size #{image_data.length}"
puts "local size "+File.size("images/#{id}.jpg").to_s
if image_data.length > File.size("images/#{id}.jpg")
writeOut = open("images/originals/"+filename, 'wb')
writeOut.write(image_data)
writeOut.close
puts '+ downloaded ' + url + " to " + filename
# write Finder metadata for download location
`xattr -w com.apple.metadata:kMDItemWhereFroms "#{url}" images/originals/#{filename}`
else
puts ' downloaded image was no bigger: not saved'
end
puts ""
end
# this needs work
user = ARGV[0]
type = ARGV[1] || 'found'
if not user
puts "A ffffound username must be supplied"
exit
else
if user == "--all"
puts "Invoked for all posts"
user = "all"
end
puts "Invoked for posts by #{user} of type #{type}"
end
if not File.exist?("images/originals")
FileUtils.mkdir "images/originals"
end
path = 'db/ffffound-'+user+'.db' # ick
db = SQLite3::Database.new(path)
fetch_orignals(db)
exit
# puts img.to_json
# DONE puts img.to_database_table(s)