flickr_cli 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/flickr_cli ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'flickr_cli'
3
+ require 'image_cutter'
4
+ FlickrCli.start_program
@@ -0,0 +1,38 @@
1
+ module FlickrCli
2
+ module Authentication
3
+ def self.establish_session
4
+ auth_file = File.expand_path("~/.flick_airliftrc")
5
+ FlickRaw.api_key = "e8505c88feb2c0cc9d2da6bcbe18767c"
6
+ FlickRaw.shared_secret = "d344de362ea86f0e"
7
+
8
+ if File.exists?(auth_file)
9
+ puts "authenticating thought #{auth_file}"
10
+ data = YAML.load_file(auth_file)
11
+ auth = flickr.auth.checkToken :auth_token => data["api_token"]
12
+ else
13
+ frob = flickr.auth.getFrob
14
+ auth_url = FlickRaw.auth_url :frob => frob, :perms => "write"
15
+
16
+ puts " "
17
+ puts "opening your browser..."
18
+ sleep 1
19
+ puts "Come back and press Enter when you are finished"
20
+ sleep 2
21
+ Launchy.open(auth_url)
22
+
23
+ STDIN.getc
24
+
25
+ # Authentication
26
+ auth = flickr.auth.getToken :frob => frob
27
+ login = flickr.test.login
28
+
29
+ puts auth.token
30
+
31
+ require 'yaml'
32
+ data = {}
33
+ data["api_token"] = auth.token
34
+ File.open(auth_file, "w"){|f| YAML.dump(data, f) }
35
+ end
36
+ end
37
+ end
38
+ end
data/lib/flickr_cli.rb ADDED
@@ -0,0 +1,22 @@
1
+ # Extental Dependencies
2
+ require 'rubygems'
3
+ require 'tempfile'
4
+ require 'flickraw'
5
+ require 'net/http'
6
+ require 'cgi'
7
+ require 'launchy'
8
+ require 'highline/import'
9
+ require 'RMagick'
10
+ # Internal Libraries
11
+ require 'authentication'
12
+ require 'menu'
13
+
14
+ module FlickrCli
15
+
16
+ def self.start_program
17
+ HighLine.track_eof = false
18
+
19
+ Authentication.establish_session
20
+ Menu.main_menu
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ module FlickrCli
2
+ module ImageCutter
3
+
4
+ CHARS = [ 'W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+',':', '~', '.', ' ' ]
5
+ FONT_ROWS = 8
6
+ FONT_COLS = 4
7
+
8
+ def self.convert_to_ascii(file)
9
+
10
+ img = Magick::Image.read(file).first
11
+
12
+ # Resize too-large images. The resulting image is going to be
13
+ # about twice the size of the input, so if the original image is too
14
+ # large we need to make it smaller so the ASCII version won't be too
15
+ # big. The `change_geometry' method computes new dimensions for an
16
+ # image based on the geometry argument. The '320x320>' argument says
17
+ # "If the image is too big to fit in a 320x320 square, compute the
18
+ # dimensions of an image that will fit, but retain the original aspect
19
+ # ratio. If the image is already smaller than 320x320, keep the same
20
+ # dimensions."
21
+ img.change_geometry('728x728>') do |cols, rows|
22
+ img.resize!(cols, rows) if cols != img.columns || rows != img.rows
23
+ end
24
+
25
+ # Compute the image size in ASCII "pixels" and resize the image to have
26
+ # those dimensions. The resulting image does not have the same aspect
27
+ # ratio as the original, but since our "pixels" are twice as tall as
28
+ # they are wide we'll get our proportions back (roughly) when we render.
29
+ pr = img.rows / FONT_ROWS
30
+ pc = img.columns / FONT_COLS
31
+ img.resize!(pc, pr)
32
+
33
+ img = img.quantize(16, Magick::GRAYColorspace)
34
+ img = img.normalize
35
+
36
+ # Draw the image surrounded by a border. The `view' method is slow but
37
+ # it makes it easy to address individual pixels. In grayscale images,
38
+ # all three RGB channels have the same value so the red channel is as
39
+ # good as any for choosing which character to represent the intensity of
40
+ # this particular pixel.
41
+ border = '+' + ('-' * pc) + '+'
42
+ puts border
43
+
44
+ img.view(0, 0, pc, pr) do |view|
45
+ pr.times do |i|
46
+ putc '|'
47
+ pc.times { |j| putc CHARS[view[i][j].red/16] }
48
+ puts '|'
49
+ end
50
+
51
+ end
52
+ border
53
+ end
54
+ end
55
+ end
data/lib/menu.rb ADDED
@@ -0,0 +1,68 @@
1
+ module FlickrCli
2
+ module Menu
3
+
4
+ def self.main_menu
5
+ choose do |menu|
6
+ menu.prompt = "What's up?"
7
+ menu.choice(:Contacts) { contacts }
8
+ # menu.choice(:"Your Sets") { sets }
9
+ end
10
+ end
11
+
12
+ def self.menu_for(contact)
13
+
14
+ user_id = flickr.people.findByUsername(:username => contact).id
15
+ photos = flickr.photos.search(:user_id => user_id)
16
+
17
+ choose do |menu|
18
+ menu.prompt = "Pick a file"
19
+
20
+ photos.map(&:title).each do |photo|
21
+ menu.choice(photo) do
22
+ # When you choose a photo...
23
+ FlickrCli::Menu.download_and_print(photos.detect{|x| x.title == photo})
24
+ puts "Press any ENTER to continue"
25
+ do_nothing = STDIN.gets
26
+ self.menu_for(FlickrCli::Menu.menu_for(contact))
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.download_and_print(picked_photo)
33
+
34
+ photos = flickr.photos.getSizes(:photo_id => picked_photo.id)
35
+ download_url = nil
36
+
37
+ ["Medium", "Medium 640", "Small"].each do |style|
38
+ if picture = photos.find{ |photo| photo.label == style }
39
+ download_url = picture.source
40
+ end
41
+ break if download_url
42
+ end
43
+
44
+ my_file = Tempfile.new('tempimage.jpg')
45
+ my_file << Net::HTTP.get_response(URI.parse(download_url)).body
46
+ my_file.close
47
+ puts FlickrCli::ImageCutter.convert_to_ascii(my_file.path)
48
+ my_file.delete
49
+ end
50
+
51
+ def self.contacts
52
+ contacts ||= flickr.contacts.getList.map(&:username).sort
53
+
54
+ choose do |menu|
55
+ menu.prompt = "Choose a contact"
56
+ contacts.each do |contact|
57
+ menu.choice(contact.to_sym) { FlickrCli::Menu.menu_for(contact) }
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ # TODO: implement sets
64
+ # def self.sets
65
+ #
66
+ # end
67
+
68
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flickr_cli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Stephen Schor
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-11 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: launchy
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 4
33
+ - 0
34
+ version: 0.4.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: flickraw
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 55
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 4
50
+ version: 0.8.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: A Command-Line tool for exploring your flickr account
54
+ email:
55
+ - beholdthepanda@gmail.com
56
+ executables:
57
+ - flickr_cli
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - bin/flickr_cli
64
+ - lib/authentication.rb
65
+ - lib/flickr_cli.rb
66
+ - lib/image_cutter.rb
67
+ - lib/menu.rb
68
+ has_rdoc: true
69
+ homepage: https://github.com/nodanaonlyzuul/flickr_cli
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: nowarning
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A Command-Line tool for exploring your flickr account
102
+ test_files: []
103
+