You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by se...@apache.org on 2017/12/13 12:31:11 UTC

[whimsy] branch master updated: Add interface to preferences

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 1d14597  Add interface to preferences
1d14597 is described below

commit 1d14597d064d064f592cdc07c84e36f0802f9e89
Author: Sebb <se...@apache.org>
AuthorDate: Wed Dec 13 12:30:39 2017 +0000

    Add interface to preferences
---
 tools/ponyapi.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/tools/ponyapi.rb b/tools/ponyapi.rb
index 205cc0f..cfedf83 100644
--- a/tools/ponyapi.rb
+++ b/tools/ponyapi.rb
@@ -12,9 +12,45 @@ require 'cgi'
 module PonyAPI
   PONYSTATS = 'https://lists.apache.org/api/stats.lua?list=' # board&domain=apache.org&d=2017-04 becomes board-apache-org-201704-stats.json
   PONYMBOX = 'https://lists.apache.org/api/mbox.lua?list=' # board@apache.org&date=2016-06 becomes board-apache-org-201707.mbox
+  PONYPREFS = 'https://lists.apache.org/api/preferences.lua' # => preferences.json
   
   extend self
-  
+
+  # Get summary of all mailing lists by domain
+  # Must supply cookie = 'ponymail-logged-in-cookie' to show private lists
+  # if cookie == 'prompt' and the script is run interactively then
+  # you will be prompted to enter the cookie value
+  # The method writes the file 'preferences.json' if dir != nil
+  # it returns the data as a hash
+  def get_pony_prefs(dir, cookie=nil, sort_list=false)
+    cookie=get_cookie() if cookie == 'prompt'
+    uri, request, response = fetch_pony(PONYPREFS, cookie)
+    jzon = {}
+    if response.code == '200' then
+      jzon = JSON.parse(response.body)
+      if dir
+        # no real point sorting unless writing the file
+        jzon['lists'] = Hash[jzon['lists'].sort] if sort_list && jzon['lists']
+        File.open(File.join("#{dir}", 'preferences.json'), "w") do |f|
+          begin
+            f.puts JSON.pretty_generate(jzon)
+          rescue JSON::GeneratorError
+            puts "WARN:get_pony_prefs(#{uri.request_uri}) threw JSON::GeneratorError, continuing without pretty"
+            f.puts jzon
+          end    
+        end
+      end
+    else
+      puts "ERROR:get_pony_prefs(#{uri.request_uri}) returned code #{response.code.inspect}"
+    end
+    if cookie
+      unless jzon['login'] && jzon['login']['credentials']
+        puts "WARN:get_pony_prefs(#{uri.request_uri}) failed cookie test"
+      end
+    end
+    jzon
+  end
+
   # Download one month of stats as a JSON
   # Must supply cookie = 'ponymail-logged-in-cookie' if a private list
   def get_pony_stats(dir, list, subdomain, year, month, cookie)
@@ -83,6 +119,15 @@ module PonyAPI
 
   private
 
+  def get_cookie()
+    unless STDIN.isatty
+      puts "WARN:Input is not a tty; cannot prompt for a cookie"
+      return nil
+    end
+    require 'io/console'
+    STDIN.getpass('Please provide the login cookie: ')
+  end
+
   # Fetch a Ponymail API, with optional logged-in cookie
   def fetch_pony(uri, cookie)
     uri = URI.parse(uri)
@@ -103,4 +148,5 @@ end
 if __FILE__ == $0
 #  PonyAPI.get_pony_mbox('.', 'dev', 'whimsical', 2017, 01, nil)
 #  PonyAPI.get_pony_stats('.', 'dev', 'whimsical', 2017, 01, nil)
+#  puts PonyAPI.get_pony_prefs(nil, nil, true)['login'].inspect
 end

-- 
To stop receiving notification emails like this one, please contact
['"commits@whimsical.apache.org" <co...@whimsical.apache.org>'].