~khaeru/referencer/daily

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Referencer is released under the GNU General Public License v2
 * See the COPYING file for licensing details.
 *
 * Copyright 2007 John Spray
 * (Exceptions listed in README)
 *
 */


#include <iostream>

#include <gtkmm.h>
#include <glibmm/i18n.h>

#include "ucompose.hpp"

#include "BibData.h"
#include "BibUtils.h"
#include "Preferences.h"
#include "Transfer.h"
#include "Utility.h"

#include "config.h"
#include "ArxivPlugin.h"

int ArxivPlugin::canResolve (Document &doc)
{
	if (doc.hasField("eprint") || doc.hasField("ee"))
		return 80;
	return -1;
}

bool ArxivPlugin::resolve (Document &doc)
{
	DEBUG (">> resolve");
	if (!doc.hasField("eprint") || _global_prefs->getWorkOffline())
		return false;


	Glib::ustring arxivid = doc.getField("eprint");
	Glib::ustring::size_type index = arxivid.find ("v");
	if (index != Glib::ustring::npos) {
		arxivid = arxivid.substr (0, index);
	}

	arxivid = Glib::Markup::escape_text (arxivid);

	Glib::ustring const filename = "http://www.citebase.org/openurl?url_ver=Z39.88-2004&svc_id=bibtex&rft_id=oai%3AarXiv.org%3A" + arxivid;

	Glib::ustring messagetext =
		String::ucompose (
			"<b><big>%1</big></b>\n\n%2\n",
			_("Retrieving metadata"),
			String::ucompose (
				_("Contacting citebase.org to retrieve metadata for '%1'"),
				arxivid)
		);

	DEBUG (">> netops");
	Glib::ustring *rawtext;
	try {
		rawtext = &Transfer::readRemoteFile (
			_("Downloading Metadata"), messagetext, filename);

		DEBUG ("Raw citebase:\n%1\n----", *rawtext);
	} catch (Transfer::Exception ex) {
		Utility::exceptionDialog (&ex, _("Downloading metadata"));
		return false;
	}

	DEBUG ("<< netops");

	if (rawtext->size() == 0)
		return false;

	BibUtils::param p;
	BibUtils::bibl b;
	BibUtils::bibl_init( &b );
	BibUtils::bibl_initparams( &p, BibUtils::FORMAT_BIBTEX, BIBL_MODSOUT);

	try {
		BibUtils::biblFromString (b, *rawtext, BibUtils::FORMAT_BIBTEX, p);
		if (b.nrefs < 1)
			return false;

		Document newdoc = BibUtils::parseBibUtils (b.ref[0]);

		// Sometimes citebase gives us an URL which is just a doi
		Glib::ustring const url = newdoc.getBibData().extras_["Url"];
		DEBUG ("url = %1", url);
		DEBUG ("substr = ",  url.substr (0, 4));
		if (url.size() >= 5 && url.substr (0, 4) == Glib::ustring("doi:")) {
			if (newdoc.getBibData().getDoi().empty()) {
				newdoc.getBibData().setDoi (url.substr(4, url.size()));
				BibData::ExtrasMap::iterator it = newdoc.getBibData().extras_.find("Url");
				newdoc.getBibData().extras_.erase(it);
			}
		}

		doc.getBibData().mergeIn (newdoc.getBibData());	
		
		BibUtils::bibl_free( &b );
	} catch (Glib::Error ex) {
		BibUtils::bibl_free( &b );
		Utility::exceptionDialog (&ex, _("Parsing BibTeX"));
		return false;
	}

	return true;
}


Glib::ustring const ArxivPlugin::getShortName ()
{
	return Glib::ustring ("arxiv");
}


Glib::ustring const ArxivPlugin::getLongName ()
{
	return Glib::ustring (_("Arxiv.org ArXiv-ID resolver"));
}


Glib::ustring const ArxivPlugin::getAuthor ()
{
	return Glib::ustring ("John Spray");
}


Glib::ustring const ArxivPlugin::getVersion ()
{
	return Glib::ustring (VERSION);
}