-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchg-background.rb
More file actions
116 lines (95 loc) · 2.69 KB
/
Copy pathchg-background.rb
File metadata and controls
116 lines (95 loc) · 2.69 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env ruby
require 'rubygems'
require 'flickr'
require 'ftools'
require 'yaml'
API_KEY = "bd58d18427f40e82d7b2f0dbf6f2dc7a"
SHARED_SECRET = "b87f58d931a16d4f"
SCREEN_RESOLUTION = [1440,900]
def is_background_image(photo, config)
return false unless photo.sizes[:Original]
photo.sizes[:Original].width > photo.sizes[:Original].height &&
photo.sizes[:Original].width > 800 && !config[:used_backgrounds].include?(photo.id)
end
def get_flickr_photo(flickr,config)
found = nil
10.times { |i|
is_end = false
page = i +(config[:last_page]||0) +1
puts "checking page #{page}"
photos = flickr.interestingness.getList(nil, nil, 10, page)
break if photos.nil? || photos.empty?
for photo in photos
if photo.nil?
is_end = true
break
end
if is_background_image(photo, config)
found = photo
break
end
end
break if is_end
if found
config[:last_page] = page
break
end
}
found
end
def clear_config(config)
config[:used_backgrounds] = []
config[:last_page] = nil
config[:last_updated] = Date.today
end
bg_dir = File.expand_path '~/.flickr-backgrounds'
`mkdir -p #{bg_dir}` unless File.exist?(bg_dir)
config = {:last_updated=> []}
config_file = File.join(bg_dir, 'config.yml')
config = YAML::load(File.open(config_file)) if File.exists?(config_file)
if config[:last_updated] != Date.today
clear_config config
end
flickr = Flickr.new(File.join(bg_dir, 'rflickr.cache'), API_KEY, SHARED_SECRET)
unless flickr.auth.token
flickr.auth.getFrob
url = flickr.auth.login_link
puts "You must visit #{url} to authorize this application. Press enter "+
'when you have done so. This is the only time you will have to do this.'
gets
flickr.auth.getToken
flickr.auth.cache_token
end
photo = get_flickr_photo flickr, config
unless photo
clear_config config
photo = get_flickr_flickr, config
end
config[:used_backgrounds].push photo.id
f = File.open(config_file, 'w+')
f.write config.to_yaml
f.close
size = nil
unless photo.sizes[:Original].width < SCREEN_RESOLUTION[0]
distances = {}
photo.sizes.each_pair { |k,v|
d = (SCREEN_RESOLUTION[0] - v.width).abs
distances[k] = d
size = k if d < 500
}
unless size
size = distances.min { |arr1, arr2| arr1[1] <=> arr2[1] }[0]
end
else
size = :Original
end
f = File.open(File.join(bg_dir, 'background_new'), 'w+')
f.write(Net::HTTP.get(URI.parse(photo.sizes[size].source)))
f.close
bg_file = File.join(bg_dir, 'background')
if File.exists?(bg_file)
File.delete bg_file
bg_file = File.join(bg_dir, 'background2')
end
File.move(f.path, bg_file)
`gconftool -t string -s /desktop/gnome/background/picture_filename #{bg_file}`