-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetMergeURL.java
149 lines (134 loc) · 5.19 KB
/
GetMergeURL.java
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
import java.sql.*;
import java.io.*;
import java.util.*;
public class GetMergeURL
{
private Connection con;
public GetMergeURL()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/openlibrary", "olbot", "public");
String q = "SELECT slug, dobslug, dodslug FROM dupe_authors_dates ORDER BY number DESC";
PreparedStatement p = con.prepareStatement(q);
ResultSet rs = p.executeQuery();
PreparedStatement p2 = con.prepareStatement("SELECT `key` FROM authors_2012_04_dates WHERE slug = ? AND dobslug = ? AND dodslug = ? ORDER BY 1 ASC");
PreparedStatement p2n1 = con.prepareStatement("SELECT `key` FROM authors_2012_04_dates WHERE slug = ? AND dobslug IS NULL AND dodslug = ? ORDER BY 1 ASC");
PreparedStatement p2n2 = con.prepareStatement("SELECT `key` FROM authors_2012_04_dates WHERE slug = ? AND dobslug = ? AND dodslug IS NULL ORDER BY 1 ASC");
PreparedStatement p2nn = con.prepareStatement("SELECT `key` FROM authors_2012_04_dates WHERE slug = ? AND dobslug IS NULL AND dodslug IS NULL ORDER BY 1 ASC");
ResultSet rs2 = null;
String url = null;
int fileNum = 1;
List<String> listOfUrls = new LinkedList<String>();
while(rs.next())
{
String slug = rs.getString("slug");
String dobslug = rs.getString("dobslug");
String dodslug = rs.getString("dodslug");
if (dobslug == null && dodslug == null)
{
p2nn.setString(1, slug);
rs2 = p2nn.executeQuery();
}
else if (dobslug == null)
{
p2n1.setString(1, slug);
p2n1.setString(2, dodslug);
rs2 = p2n1.executeQuery();
}
else if (dodslug == null)
{
p2n2.setString(1, slug);
p2n2.setString(2, dobslug);
rs2 = p2n2.executeQuery();
}
else
{
p2.setString(1, slug);
p2.setString(2, dobslug);
p2.setString(3, dodslug);
rs2 = p2.executeQuery();
}
url = createURL(rs2);
listOfUrls.add(wrapURL(url, slug+" | "+dobslug+" | "+dodslug));
// Limit to 100 URLs per file
if (rs.getRow() % 100 == 0)
{
writeFile(fileNum, listOfUrls);
System.out.println("Wrote file number "+fileNum);
fileNum++;
listOfUrls.clear();
}
}
if (!listOfUrls.isEmpty())
{
writeFile(fileNum, listOfUrls);
System.out.println("Wrote file number "+fileNum);
}
con.close();
}
catch (SQLException e)
{
System.out.println("SQL exception in main: " + e.getMessage());
}
catch (Exception e)
{
System.out.println("General exception in main: " + e.getMessage());
e.printStackTrace();
}
}
private String createURL(ResultSet rs) throws SQLException
{
String s = "http://openlibrary.org/authors/merge?";
while (rs.next())
{
s += "key=" + rs.getString(1) + "&";
}
//System.out.println(s);
return s;
}
private String wrapURL(String url, String label)
{
return "<a href=\""+url+"\" target=\"_blank\">"+label+"</a>";
}
private void writeFile(int fileNum, List<String> listOfUrls)
{
try
{
BufferedWriter fw = new BufferedWriter( new FileWriter("ol_merge_links_"+fileNum+".html"));
fw.write("<html>");
fw.newLine();
fw.write("<head>");
fw.newLine();
fw.write("<title>Merge Authors</title>");
fw.newLine();
fw.write("</head>");
fw.newLine();
fw.write("<body>");
fw.newLine();
fw.write("<p>Take care when merging, it's easier to merge than to split authors :)</p>");
fw.newLine();
Iterator<String> it = listOfUrls.iterator();
while (it.hasNext())
{
fw.write( it.next() + "<br />" );
fw.newLine();
}
fw.write( "<p>Next file: <a href=\"ol_merge_links_"+(fileNum+1)+".html\">"+(fileNum+1)+"</a></p></body></html>");
fw.close();
}
catch (IOException ie)
{
System.out.println("IO exception: " + ie.getMessage());
}
catch (Exception se)
{
System.out.println("General exception in writeFile: " + se.getMessage());
}
}
public static void main(String[] args)
{
new GetMergeURL();
}
}