instapaper_download 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # InstapaperDownload
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'instapaper_download'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install instapaper_download
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require 'instapaper_download.rb'
3
+ require 'mechanize'
4
+
5
+ config_file = Instapaper::ConfigFile.new
6
+
7
+ account = Instapaper::Account.from_config_file(config_file)
8
+ agent = Mechanize.new
9
+ agent.user_agent_alias = 'Mac Safari'
10
+
11
+ login_page = Instapaper::LoginPage.new(agent)
12
+ agent = login_page.login(account)
13
+ if login_page.logged_in?
14
+ download_page = Instapaper::EpubDownloadPage.new(agent)
15
+ file = download_page.download
16
+ if file.respond_to?(:save)
17
+ file.save(file.filename)
18
+ instapaper_file = file.filename
19
+ puts %Q{Saved your epub to "#{instapaper_file}"}
20
+ else
21
+ $stderr.puts "Something is broken, I logged in successfully but then I couldn't save the file. Weird, huh?!"
22
+ exit 1
23
+ end
24
+ else
25
+ error_message = "Could not login, maybe something is wrong with your credentials."
26
+ error_message += %Q{ You probably want to check that configuration file thingy you have at "#{config_file.path}".} if config_file.exists?
27
+ $stderr.puts error_message
28
+ exit 1
29
+ end
30
+
31
+ reader = EbookReader.from_config_file(config_file)
32
+ if reader.is_mounted?
33
+ reader.move_to_device(instapaper_file)
34
+ else
35
+ error_message = %Q{No reader found at "#{reader.mount_point}".}
36
+ error_message += %Q{ If you are sure that it is mounted, you probably want to check that configuration file thingy you have at "#{config_file.path}".} if config_file.exists?
37
+ $stderr.puts error_message
38
+ end
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env ruby
2
+ require 'mechanize'
3
+ require 'yaml'
4
+ require 'fileutils'
5
+
6
+ class Instapaper
7
+ class ConfigFile
8
+
9
+ def initialize(config_file = "~/.instapaper_download")
10
+ @config_file = File.expand_path(config_file)
11
+ @config_hash = {}
12
+ @save_to_file = nil
13
+ end
14
+
15
+ def read_from_file
16
+ raise "Please make sure that #{@config_file} exists" unless exists?
17
+ @config_hash = YAML::load_file(@config_file)
18
+ end
19
+
20
+ def create_or_update
21
+ File.open(@config_file, "w") do |file|
22
+ file.write(YAML::dump(@config_hash))
23
+ end
24
+ puts %Q{Saved configuration to "#{@config_file}"}
25
+ end
26
+
27
+ def exists?
28
+ File.exists?(@config_file) && File.file?(@config_file)
29
+ end
30
+
31
+ def path
32
+ @config_file
33
+ end
34
+
35
+ def save_to_file_if_necessary(hash)
36
+ @config_hash = @config_hash.merge(hash) #Overwrites old values from @config_hash with new values from hash in case of identical keys
37
+ puts
38
+ while @save_to_file.nil? do
39
+ print "Should I hold on to this information for you? Caution: The password is stored in plain text! [y/n] "
40
+ response = gets.chomp.downcase
41
+ if response == "y"
42
+ @save_to_file = true
43
+ elsif response == "n"
44
+ @save_to_file = false
45
+ end
46
+ end
47
+ if @save_to_file
48
+ create_or_update
49
+ end
50
+ end
51
+ end
52
+
53
+ class Account
54
+ attr_reader :email, :password
55
+ def initialize(email, password)
56
+ @email = email
57
+ @password = password
58
+ end
59
+ def self.from_config_file(config_file = ConfigFile.new)
60
+ config = {}
61
+ config = config_file.read_from_file if config_file.exists?
62
+ unless config[:email] and config[:password]
63
+ print "Please enter instapaper email address: "
64
+ email = gets.chomp
65
+ print "Please enter instapaper password: "
66
+ `stty -echo`
67
+ password = gets.chomp
68
+ `stty echo`
69
+ config[:email] = email
70
+ config[:password] = password
71
+ config_file.save_to_file_if_necessary(config)
72
+ end
73
+ return Account.new(config[:email], config[:password])
74
+ end
75
+ end
76
+
77
+ class LoginPage
78
+ def initialize(agent)
79
+ @agent = agent
80
+ @url = "http://www.instapaper.com/user/login"
81
+ @logged_in = false
82
+ end
83
+ def login(account)
84
+ puts "Logging in as #{account.email}"
85
+ page = @agent.get(@url)
86
+ login_form = page.forms.first
87
+ login_form.field_with(:name => "username").value = account.email
88
+ login_form.field_with(:name => "password").value = account.password
89
+ page = @agent.submit(login_form)
90
+ @logged_in = true if page.title == "Logging in..."
91
+ @agent
92
+ end
93
+ def logged_in?
94
+ @logged_in
95
+ end
96
+ end
97
+
98
+ class EpubDownloadPage
99
+ def initialize(agent)
100
+ @agent = agent
101
+ @url = "http://www.instapaper.com/epub"
102
+ end
103
+ def download
104
+ page = @agent.get(@url)
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ class EbookReader
111
+ attr_reader :mount_point
112
+
113
+ def initialize(path)
114
+ @mount_point = path
115
+ end
116
+
117
+ def self.from_config_file(config_file = ConfigFile.new)
118
+ config = {}
119
+ config = config_file.read_from_file if config_file.exists?
120
+ unless config[:ebook_reader_path]
121
+ print "Where is your ebook reader mounted?: "
122
+ path = gets.chomp
123
+ config[:ebook_reader_path] = path
124
+ config_file.save_to_file_if_necessary(config)
125
+ end
126
+ return EbookReader.new(config[:ebook_reader_path])
127
+ end
128
+
129
+ def is_mounted?
130
+ File.exists?(@mount_point) and File.directory?(@mount_point)
131
+ end
132
+
133
+ def move_to_device(file)
134
+ raise %Q{The ebook reader does not seem to be mounted on "#{@mount_point}"} unless is_mounted?
135
+ file = File.expand_path(file)
136
+ @mount_point = File.expand_path(@mount_point)
137
+ FileUtils.mv(file, @mount_point)
138
+ puts %Q{Moved "#{file}" to your ebook reader}
139
+ end
140
+
141
+ end
142
+
143
+
144
+ #TODO: Deploy it like: https://github.com/defunkt/cheat
@@ -0,0 +1,5 @@
1
+ require "instapaper_download/version"
2
+
3
+ module InstapaperDownload
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module InstapaperDownload
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instapaper_download
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Geiger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.5.0
30
+ description:
31
+ email:
32
+ - sebastian.geiger@gmail.com
33
+ executables:
34
+ - instapaper_download
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - bin/instapaper_download
39
+ - lib/instapaper_download/version.rb
40
+ - lib/instapaper_download.rb
41
+ - lib/instapaper_download.rb.back
42
+ - README.md
43
+ homepage: http://sebastiangeiger.github.com/categories/instapaper-download/
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: instapaper_download provies a binary that lets you download an epub from
67
+ instapaper and put it on an ebook reader
68
+ test_files: []