Skip to content

Instantly share code, notes, and snippets.

@kongscn

kongscn/kanyanbao.js

Last active Dec 13, 2018
Embed
What would you like to do?
Zotero translator for kanyanbao.com
{
"translatorID": "56ebac6c-56f7-4564-ac07-58d6a1b2948a",
"label": "kanyanbao",
"creator": "Shel Kong",
"target": "^https?://(www.)?kanyanbao.com/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": false,
"translatorType": 6,
"browserSupport": "gcs",
"lastUpdated": "2018-12-13 12:54:50"
}
// Zotero translator for kanyanbao.com
// Supports detail and pdf page for single report
function detectWeb(doc, url) {
if (url.indexOf("report/detail.htm") != -1) {
return "detail";
} else if (url.indexOf("report/frame") != -1) {
return "pdf";
}
}
function doWeb(doc, url) {
var detected = detectWeb(doc, url);
if (detected == "detail") {
scrape_detail(doc, url);
} else if (detected == "pdf") {
scrape_pdf(doc, url);
}
}
function scrape(doc) {
item.title = ZU.xpathText(doc, '//span[@class="maintitle"]');
// item.reportNumber = url.match(/docid=(\d+)/)[1];
// creators
// item.institution = ZU.xpathText(doc, '//span[@class="brokername *"]');
var metarow = ZU.xpath(doc, '//div[@class="brokeranalyst"]')[0];
meta = ZU.xpathText(doc, '//div[@class="brokeranalyst"]');
item.institution = ZU.xpathText(metarow, '//span[contains(@class, "brokername")]');
item.date = meta.match(/[\d\/]+/)[0].replace(/\//g, '-').replace(/^/, '20');
abnode = ZU.xpath(doc, '//div[contains(@class,"fullsummary")]')[0]
summary = ZU.xpathText(abnode, '.').trim();
if (summary !== "get_summary('')") {
item.abstractNote = summary;
}
item.reportNumber = abnode.getAttribute('data-docid');
tags = meta.match(/([^|]+)\s*\|\s*\d/)[1].trim().split(/\s/);
for (var i in tags) {
tag = tags[i];
if (tag !== "") {
item.tags.push(tag)
}
}
authors = ZU.xpathText(metarow, '//span[contains(@class, "analyst")]');
authors = ZU.trimInternal(authors).split(',');
for (var j in authors) {
author = authors[j];
author = ZU.cleanAuthor(author, 'author', false);
item.creators.push(author);
}
attachs = ZU.xpath(doc, '//div[@class="attachmentrow"]');
for (var i in attachs) {
attnode = attachs[i];
fname = attnode.textContent.trim().split(/\s/)[0];
dlnode = ZU.xpath(attnode, '//a[text() = "下载"]')[0];
dlink = 'https://kanyanbao.com' + dlnode.getAttribute('href');
item.attachments.push({
title: fname.split(".")[0],
url: dlink,
mimeType: 'application/pdf',
snapshot: true,
})
}
return item;
}
// detail
function scrape_detail(doc, url) {
// title
item = new Zotero.Item("report");
scrape(doc, item);
item.url = url;
item.complete();
}
function get_summary(doc) {
item.abstractNote = doc.body.textContent;
}
// pdf
function scrape_pdf(doc, url) {
item = new Zotero.Item("report");
var docid = url.match(/docid=(\d+)/)[1];
var detailink = 'https://kanyanbao.com/report/detail.htm?docid=' + docid;
item.url = detailink;
ZU.processDocuments(detailink, scrape, function() {
var summarylink = 'https://www.kanyanbao.com/report/get_summary.htm?id=' + docid;
ZU.processDocuments(summarylink, get_summary, function() {
item.complete();
});
});
}
function doExport() {
while (item = Zotero.nextItem()) {
var url = 'https://kanyanbao.com/report/detail.htm?docid=' + item.reportNumber;
var short = '【'
short += item.date + " " + item.institution + "】";
anchor = "[^" + item.title + "_" + item.reportNumber + "]: "
var str = "[" + short + "](" + url + ") " + item.title + "\n";
Zotero.write(anchor + str);
}
}
{
"translatorID": "3039c289-904b-405c-9368-9fb89c50b948",
"label": "markdown_title",
"creator": "Shel Kong",
"target": "",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": false,
"translatorType": 2,
"browserSupport": "gcsibv",
"lastUpdated": "2018-12-13 06:28:58"
}
// Export with markdown title and link
function doExport() {
while (item = Zotero.nextItem()) {
var url = 'https://kanyanbao.com/report/detail.htm?docid=' + item.reportNumber;
var str = "[" + item.title + "](" + url + ")";
Zotero.write(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment