batchtapaper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/batchtapaper +102 -0
- metadata +46 -0
data/bin/batchtapaper
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "net/https"
|
5
|
+
require "uri"
|
6
|
+
|
7
|
+
API_URL = URI.parse("https://www.instapaper.com/api/add")
|
8
|
+
|
9
|
+
# Fetch our URLs; either from the files given in the arguments, or from
|
10
|
+
# STDIN.
|
11
|
+
lines = ARGF.read
|
12
|
+
|
13
|
+
unless lines
|
14
|
+
puts "No URLs specified."
|
15
|
+
exit
|
16
|
+
end
|
17
|
+
|
18
|
+
lines = lines.split("\n")
|
19
|
+
|
20
|
+
# First, attempt to read usernames and passwords from the
|
21
|
+
# ~/.instapaperrc file.
|
22
|
+
rcfile = File.expand_path("~/.instapaperrc")
|
23
|
+
if File.exists? rcfile
|
24
|
+
username, password = IO.read(rcfile).split("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
if !username && !STDIN.tty?
|
28
|
+
# If we haven't got a username from the rc file, and we're reading input
|
29
|
+
# from STDIN, then we're out of luck; we can't proceed.
|
30
|
+
puts "When passing URLs via STDIN, you must have an ~/.instapaperrc file containing your auth details."
|
31
|
+
exit
|
32
|
+
elsif !username && STDIN.tty?
|
33
|
+
# If we do have a TTY (if the URLs are in a file), then we can prompt
|
34
|
+
# the user for their auth details.
|
35
|
+
begin
|
36
|
+
print "Email or username: "
|
37
|
+
username = STDIN.gets.chomp
|
38
|
+
|
39
|
+
print "Password (if you have one): "
|
40
|
+
# We don't want to echo input when the user is entering their
|
41
|
+
# password, to prevent shoulder surfing; so, we disable the
|
42
|
+
# terminal's echo temporarily.
|
43
|
+
system 'stty -echo'
|
44
|
+
password = STDIN.gets.chomp
|
45
|
+
system 'stty echo'
|
46
|
+
rescue NoMethodError, Interrupt
|
47
|
+
# In case the user interrupts when typing their password, we don't
|
48
|
+
# want to leave them with a terminal that isn't echoing.
|
49
|
+
system 'stty echo'
|
50
|
+
exit
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "\n\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Grab the lines of the file
|
57
|
+
lines.each do |line|
|
58
|
+
line.strip!
|
59
|
+
|
60
|
+
# If the line contains a tab, then assume it's in "URL[tab]title" format
|
61
|
+
if line =~ /\t/
|
62
|
+
url, title = line.split("\t")
|
63
|
+
else
|
64
|
+
url = line
|
65
|
+
end
|
66
|
+
|
67
|
+
# Attempt to parse the URL that we've been given.
|
68
|
+
begin
|
69
|
+
url = URI.parse(url)
|
70
|
+
rescue
|
71
|
+
puts "#{url} doesn't seem to be a valid URL. Moving on..."
|
72
|
+
continue
|
73
|
+
end
|
74
|
+
|
75
|
+
print "Processing URL: #{url}..."
|
76
|
+
|
77
|
+
post = { 'username' => username, 'password' => password, 'url' => url.to_s }
|
78
|
+
if title
|
79
|
+
post['title'] = title
|
80
|
+
end
|
81
|
+
|
82
|
+
# We need to use this slightly more verbose syntax, rather than just
|
83
|
+
# Net::HTTP::post_form, because the Instapaper API uses SSL.
|
84
|
+
request = Net::HTTP::Post.new(API_URL.path)
|
85
|
+
request.set_form_data(post)
|
86
|
+
|
87
|
+
sock = Net::HTTP.new(API_URL.host, API_URL.port)
|
88
|
+
sock.use_ssl = true
|
89
|
+
response = sock.start { |http| http.request(request) }
|
90
|
+
|
91
|
+
# The Instapaper API will return a 403 error for any request if the
|
92
|
+
# given username and password are wrong.
|
93
|
+
if response.code == 403
|
94
|
+
puts "Username/password incorrect."
|
95
|
+
exit
|
96
|
+
end
|
97
|
+
|
98
|
+
# We can fairly reasonably assume that any other request is valid.
|
99
|
+
print " Got response #{response.code}\n"
|
100
|
+
|
101
|
+
sleep 1
|
102
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: batchtapaper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rob Miller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Easily add multiple URLs to Instapaper, either from a file or from STDIN.
|
15
|
+
email: rob@bigfish.co.uk
|
16
|
+
executables:
|
17
|
+
- batchtapaper
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/batchtapaper
|
22
|
+
homepage: https://github.com/robmiller/batchtapaper
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.25
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Bulk-add URLs to Instapaper
|
46
|
+
test_files: []
|