Created
June 1, 2019 16:51
-
-
Save cgimenes/5fd599e7ca9ca007011c27de84e64ccf to your computer and use it in GitHub Desktop.
Import to Pocket from a file (CSV or lines)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'socket' | |
require 'cgi' | |
require 'csv' | |
require 'json' | |
require 'thor' | |
class PocketAPI | |
def initialize(consumer_key) | |
@consumer_key = consumer_key | |
@redirect_uri = 'http://localhost:5678' | |
authenticate | |
end | |
def wait_authorization() | |
puts 'Waiting for authorization' | |
server = TCPServer.new 5678 | |
session = server.accept | |
request = session.gets | |
session.print "HTTP/1.1 200\r\n" # 1 | |
session.print "Content-Type: text/html\r\n" # 2 | |
session.print "\r\n" # 3 | |
session.print "<html><head></head><body><h2>Now, you can close this tab</h2></body></html>" #4 | |
session.close | |
puts 'Authorized' | |
end | |
def get_code() | |
response = HTTParty::post 'https://getpocket.com/v3/oauth/request', query: { consumer_key: @consumer_key, redirect_uri: @redirect_uri } | |
CGI::parse(response.body)['code'][0] | |
end | |
def get_access_token(code) | |
response = HTTParty::post 'https://getpocket.com/v3/oauth/authorize', query: { consumer_key: @consumer_key, code: code } | |
CGI::parse(response.body)['access_token'][0] | |
end | |
def authenticate() | |
code = get_code | |
puts "https://getpocket.com/auth/authorize?request_token=#{code}&redirect_uri=#{@redirect_uri}" | |
wait_authorization | |
@access_token = get_access_token code | |
end | |
def get() | |
HTTParty::post "https://getpocket.com/v3/get", query: { consumer_key: @consumer_key, access_token: @access_token } | |
end | |
def add(url) | |
HTTParty::post "https://getpocket.com/v3/add", query: { url: url, consumer_key: @consumer_key, access_token: @access_token } | |
end | |
def search(text) | |
HTTParty::post "https://getpocket.com/v3/get", query: { search: text, consumer_key: @consumer_key, access_token: @access_token } | |
end | |
def exists?(url) | |
results = JSON.parse(search(url).body) | |
!results['list'].empty? | |
end | |
end | |
class PocketImporter < Thor | |
desc "import_file FILE CONSUMER_KEY", "import lines at FILE" | |
def import_file(file, consumer_key) | |
File.foreach(file) do |line| | |
api(consumer_key).add line | |
end | |
end | |
desc "import_csv CSV_FILE CONSUMER_KEY", "import column 'URL' for each rows at CSV_FILE" | |
def import_csv(file, consumer_key) | |
CSV.foreach(file, :headers => true) do |r| | |
api(consumer_key).add r['URL'] | |
end | |
end | |
desc "check_file FILE CONSUMER_KEY", "check if lines at FILE were added" | |
def check_file(file, consumer_key) | |
File.foreach(file) do |line| | |
unless api(consumer_key).exists? line | |
puts line | |
end | |
end | |
end | |
desc "check_csv FILE CONSUMER_KEY", "check if CSV rows at FILE were added" | |
def check_csv(file, consumer_key) | |
CSV.foreach(file, :headers => true) do |r| | |
unless api(consumer_key).exists? r['URL'] | |
puts r['URL'] | |
end | |
end | |
end | |
private | |
def api(consumer_key) | |
if @api.nil? | |
@api = PocketAPI.new consumer_key | |
end | |
@api | |
end | |
end | |
PocketImporter.start(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment