/
oclc_api_classify.cs
164 lines (137 loc) · 5.31 KB
/
oclc_api_classify.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace oclc_api
{
public class oclc_api_classify
{
//lcc = Library of Congress
//dd = dewey decimal
//nn = none
public enum Class_Type { lcc = 0, dd = 1, nn = 2 }
private string pLastError = "";
private System.Net.WebProxy pSetProxy = null;
public string LastError
{
get { return pLastError; }
set { pLastError = value; }
}
public System.Net.WebProxy SetProxy
{
get { return pSetProxy; }
set { pSetProxy = value; }
}
public string GetFastRecord(string url)
{
return helpers.MakeHTTPRequest(url, SetProxy);
}
public string DoSearch(string url, oclc_api_classify.Class_Type ctype, bool bsummary, out string[] subjects)
{
LastError = "";
string sreturn = helpers.MakeOpenHTTPRequest(url, SetProxy, "GET"); // helpers.MakeHTTPRequest(url, SetProxy);
//process results
return ProcessClassify(sreturn, ctype, bsummary, out subjects);
}
private string ProcessClassify(string s, oclc_api_classify.Class_Type ctype, bool bsummary, out string[] subjects)
{
LastError = "";
subjects = null;
System.Xml.XmlDocument objXML = new System.Xml.XmlDocument();
System.Xml.XmlNamespaceManager oMan = new System.Xml.XmlNamespaceManager(objXML.NameTable);
oMan.AddNamespace("oclc", "http://classify.oclc.org");
try
{
objXML.LoadXml(s);
//First check to see if this is is a record with multiple works
System.Xml.XmlNodeList test_nodes = objXML.SelectNodes("//oclc:works/oclc:work", oMan);
//System.Windows.Forms.MessageBox.Show(test_nodes.Count.ToString());
if (test_nodes != null && test_nodes.Count > 0)
{
try
{
string url = "http://classify.oclc.org/classify2/Classify?swid=" + test_nodes[0].Attributes["swid"].InnerText + "&summary=" + bsummary.ToString();
string txml = helpers.MakeOpenHTTPRequest(url, SetProxy, "GET"); // helpers.MakeHTTPRequest(url, SetProxy);
if (txml != "")
{
objXML.LoadXml(txml);
}
else
{
return "";
}
}
catch
{
return "";
}
}
}
catch (System.Exception ee)
{
//System.Windows.Forms.MessageBox.Show(ee.ToString());
LastError = s;
}
System.Xml.XmlNodeList objList;
try
{
System.Collections.ArrayList tsubject = new System.Collections.ArrayList();
//look to see if subjects are present
objList = objXML.SelectNodes("//oclc:fast/oclc:headings/oclc:heading", oMan);
if (objList != null)
{
foreach (System.Xml.XmlNode objN in objList)
{
if (objN.Attributes["ident"] != null)
{
tsubject.Add(objN.Attributes["ident"].InnerText);
}
}
subjects = new string[tsubject.Count];
tsubject.CopyTo(subjects);
}
if (ctype == Class_Type.dd)
{
objList = objXML.SelectNodes("//oclc:ddc/oclc:mostPopular", oMan);
if (objList != null)
{
foreach (System.Xml.XmlNode objN in objList)
{
if (objN.Attributes["nsfa"] != null)
{
return objN.Attributes["nsfa"].InnerText;
break;
}
}
}
}
else
{
objList = objXML.SelectNodes("//oclc:lcc/oclc:mostPopular", oMan);
if (objList != null)
{
foreach (System.Xml.XmlNode objN in objList)
{
if (objN.Attributes["nsfa"] != null)
{
return objN.Attributes["nsfa"].InnerText;
break;
}
}
}
else
{
//System.Windows.Forms.MessageBox.Show("miss");
}
}
return "";
}
catch (Exception e)
{
LastError = e.ToString();
return "";
}
}
}
}