脚本的工作原理是先用ClientLogin
for Installed
Applications登录刚才新注册的Gmail帐号,得到Google返回的字符串AUTH=xxxxxxxxxxxxxx,然后根据这个AUTH向http://www.googleapis.com/analytics/v3/data/ga发送GET请求。
% readlink out/blog ../public % ls out/js/popular.js out/js/popular.js
把这段LiveScript脚本编译成out/js/popular.js:
1 2 3 4 5 6 7 8 9 10
truncate = (s) -> if s.length > 24 "#{s.slice(0, 21)}..." else s
$.get '/api/popular.json', (data) -> data = JSON.parse data iftypeof data is'string' for row in data $('#js-popular').append $('<li>').append $('<a>').attr('href', row.path).text("#{truncate row.title} (#{row.pageView})")
from googleapiclient.discovery import build from googleapiclient.http import HttpError from oauth2client.service_account import ServiceAccountCredentials import re, sys, json, os
r = [(normalize_path(x['dimensions'][0]), normalize_title(x['dimensions'][1]), int(x['metrics'][0]['values'][0])) for x in get_metrics()] r = sorted([x for x in r if re.match('/blog/20..-..-..-', x[0])], key=lambda x: x[0]) i = 0 rr = [] while i < len(r): j = i pv = 0 opt_pv = i while j < len(r) and r[i][0] == r[j][0]: pv += r[j][2] if r[j][2] > r[opt_pv][2] and r[j][1] != '(not set)': opt_pv = j j += 1 ifnot re.search('-document-viewer|build-system-tup|2013-07-28-beauty-of-programming|2012-11-12-build-website-with-docpad|2012-11-12-migrate-to-docpad|asc14-to-isc15-of-my', r[i][0]): #if not re.search('-document-viewer|build-system-tup|/-document-viewer|build-system-tup|2012-11-19-ai9|2012-11-12-build-website-with-docpad|2013-07-28-beauty-of-programming|2012-11-12-migrate-to-docpad|2015-05-01-jq-internals-bytecode|2015-03-26-leetcode-best-time-to-buy-and-sell-stock-iv|2015-03-25-elf-hacks|2015-03-22-bctf-2015-camlmaze|2015-03-13-debug-hacks-2|2012-11-01-perfect-maze-generation|2014-11-23-jsxajs-workgroup|2015-06-15-morris-post-order-traversal|2014-10-13-wechat-export|2012-09-09-parallel-n-body|2013-03-13-xv-olimpiada-informatyczna-etap-1-klo|2015-06-29-bmc-firmware-reverse-enginnering|2014-12-30-summary', r[i][0]): rr.append({'path':r[i][0], 'title':r[opt_pv][1], 'pageView':pv}) i = j rr.sort(key=lambda x: - x['pageView'])
# remove entries with wrong date rrr = [] exist = set() for x in rr: m = re.match('/blog/20..-..-..-([-\\w]*)', x['path']) ifnot m: rrr.append(x) elif m.groups()[0] notin exist: exist.add(m.groups()[0]) rrr.append(x) json.dump(rrr, sys.stdout, ensure_ascii=False)