diff -urN saku-3.7.3/shingetsu/gateway_cgi.py saku-3.7.3-csv/shingetsu/gateway_cgi.py --- saku-3.7.3/shingetsu/gateway_cgi.py 2009-12-26 17:21:54.000000000 +0900 +++ saku-3.7.3-csv/shingetsu/gateway_cgi.py 2010-06-27 21:41:35.000000000 +0900 @@ -31,6 +31,7 @@ import re import cgi import csv +import json, yaml, bencode from time import time import config @@ -93,7 +94,13 @@ elif self.form.getfirst("cmd", "") == "new": self.jump_new_file() elif path.startswith("csv"): - self.print_csv(path) + self.print_api(path, "csv") + elif path.startswith("json"): + self.print_api(path, "json") + elif path.startswith("yaml"): + self.print_api(path, "yaml") + elif path.startswith("be"): + self.print_api(path, "be") elif re.search(r"^(thread)", path): m = re.search(r"^(thread)/?([^/]*)$", path) if m is None: @@ -184,9 +191,9 @@ cachelist = self.make_recent_cachelist() self.print_index_list(cachelist, "recent") - def print_csv(self, path): - """CSV output as API.""" - found = re.search(r"^csv/([^/]+)/(.+)", path) + def print_api(self, path, api="csv"): + """CSV, JSON, etc... output as API.""" + found = re.search(r"^%s/([^/]+)/(.+)" % api, path) if found: target, cols = found.groups() else: @@ -206,9 +213,17 @@ else: self.print404() return - self.stdout.write("Content-Type: text/comma-separated-values;" + - " charset=UTF-8\n\n") - writer = csv.writer(self.stdout) + if api == "csv": + self.stdout.write("Content-Type: text/comma-separated-values;" + + " charset=UTF-8\n\n") + writer = csv.writer(self.stdout) + elif api == "json": + self.stdout.write("Content-Type: application/json\n\n") + elif api == "yaml": + self.stdout.write("Content-Type: application/x-yaml\n\n") + elif api == "be": + self.stdout.write("Content-Type: application/x-bittorrent;" + + " charset=UTF-8\n\n") for cache in cachelist: title = self.file_decode(cache.datfile) if cache.type in config.types: @@ -218,36 +233,54 @@ else: type = "" path = "" - row = [] + row = {} for c in cols: if c == "file": - row.append(cache.datfile) + row[c] = cache.datfile elif c == "stamp": - row.append(cache.valid_stamp) + row[c] = cache.valid_stamp elif c == "date": - row.append(self.localtime(cache.valid_stamp)) + row[c] = self.localtime(cache.valid_stamp) elif c == "path": - row.append(path) + row[c] = path elif c == "uri": if self.host and path: - row.append("http://" + self.host + path) + row[c] = "http://" + self.host + path else: - row.append("") + row[c] = "" elif c == "type": - row.append(cache.type) + row[c] = cache.type elif c == "title": - row.append(title) + row[c] = title elif c == "records": - row.append(len(cache)) + row[c] = len(cache) elif c == "size": - row.append(cache.size) + row[c] = cache.size elif c == 'tag': - row.append(str(cache.tags)) + if api == "csv": + row[c] = str(cache.tags) + else: + row[c] = [] + for tags in cache.tags: + row[c].append(str(tags)) elif c == 'sugtag': - row.append(str(cache.sugtags)) - else: - row.append("") - writer.writerow(row) + if api == "csv": + row[c] = str(cache.sugtags) + else: + row[c] = [] + for sugtags in cache.sugtags: + row[c].append(str(sugtags)) + if api == "csv": + val = [] + for c in cols: + val.append(row[c]) + writer.writerow(val) + elif api == "json": + self.stdout.write(json.dumps(row)) + elif api == "yaml": + self.stdout.write(yaml.safe_dump(row)) + elif api == "be": + self.stdout.write(bencode.bencode(row)) def jump_new_file(self): if self.form.getfirst("link", "") == "":