dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

pocket (2080B)


      1 #!/usr/bin/env ruby
      2 require_relative './libpocket.rb'
      3 def usage
      4   puts <<~HEREDOC
      5     Usage: pocket command [arg1 [arg2...]]
      6 
      7     Commands:
      8     save [URL]\tsave URL to Pocket (can also read list of URLs from stdin)
      9     audio [n]\tget n recently saved audio URLs (if n is negative, get oldest)
     10     video [n]\tget n recently saved video URLs (if n is negative, get oldest)
     11     articles [n]\tget n recently saved article URLS (if n is negative, get oldest)
     12     list [n]\tlist n recently saved URLs (if n is negative, get oldest)
     13   HEREDOC
     14 end
     15 
     16 if ARGV.empty?
     17   usage
     18   exit 1
     19 end
     20 
     21 def get_count_sort(args)
     22   begin
     23     count = Integer(args[1])
     24   rescue TypeError
     25     die 'Second argument must be an integer.'
     26   end
     27   return { count: count.abs, sort: 'oldest' } if count.negative?
     28 
     29   { count: count.abs }
     30 end
     31 def article_list_to_s(article_list)
     32   article_list.to_a.inject([]) do |lines, article|
     33     lines << "#{article['resolved_title']}\t#{article['resolved_url']}"
     34   end.join("\n")
     35 end
     36 
     37 
     38 pocket = Pocket.new
     39 if ARGV[0] == 'save'
     40   if $stdin.stat.pipe?
     41     urls = $stdin.readlines
     42     die 'save: URL required' if urls.empty?
     43     urls.each { |url| pocket.save url }
     44   else
     45     die 'save: URL required' unless ARGV[1]
     46     pocket.save ARGV[1]
     47   end
     48 elsif ARGV[0].start_with? 'audio'
     49   params = { domain: '*.bandcamp.com', contentType: 'article' }.merge(get_count_sort(ARGV))
     50   print article_list_to_s(pocket.retrieve_list(params))
     51 elsif ARGV[0].start_with? 'video'
     52   params = { contentType: 'video' }.merge(get_count_sort(ARGV))
     53   print article_list_to_s(pocket.retrieve_list(params))
     54 elsif ARGV[0].start_with? 'article'
     55   if ARGV[1]
     56     params = { contentType: 'article' }.merge(get_count_sort(ARGV))
     57     print article_list_to_s(pocket.retrieve_list(params))
     58   else
     59     print article_list_to_s(pocket.retrieve_list(contentType: 'article'))
     60   end
     61 elsif ARGV[0].start_with? 'list'
     62   if ARGV[1]
     63     print article_list_to_s(pocket.retrieve_list(get_count_sort(ARGV)))
     64   else
     65     print article_list_to_s(pocket.retrieve_list({}))
     66   end
     67 else
     68   die "Command '#{ARGV[0]}' not recognised."
     69 end