-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArxivHelper.m
196 lines (183 loc) · 6.27 KB
/
ArxivHelper.m
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// ArxivHelper.m
// spires
//
// Created by Yuji on 08/10/14.
// Copyright 2008 Y. Tachikawa. All rights reserved.
//
#import "ArxivHelper.h"
#import "AppDelegate.h"
ArxivHelper* _sharedHelper=nil;
@implementation ArxivHelper
+(ArxivHelper*)sharedHelper
{
if(!_sharedHelper){
_sharedHelper=[[ArxivHelper alloc]init];
}
return _sharedHelper;
}
-(ArxivHelper*)init
{
self=[super init];
connections=[NSMutableArray array];
return self;
}
-(NSString*)arXivHead
{
NSString*mirror= [[NSUserDefaults standardUserDefaults] stringForKey:@"mirrorToUse"];
return [NSString stringWithFormat:@"http://%@arxiv.org/",mirror];
}
-(NSString*)arXivPDFPathForID:(NSString*)arXivID
{
if([arXivID hasPrefix:@"arXiv:"]){
arXivID=[arXivID substringFromIndex:[(NSString*)@"arXiv:" length]];
}
return [NSString stringWithFormat:@"%@pdf/%@",[self arXivHead],arXivID];
}
-(NSString*)arXivAbstractPathForID:(NSString*)arXivID
{
if([arXivID hasPrefix:@"arXiv:"]){
arXivID=[arXivID substringFromIndex:[(NSString*)@"arXiv:" length]];
}
return [NSString stringWithFormat:@"%@abs/%@",[self arXivHead],arXivID];
}
-(void)startDownloadPDFforID:(NSString*)arXivID delegate:(id)dele didEndSelector:(SEL)s;
{
NSURL* url=[NSURL URLWithString:[self arXivPDFPathForID:arXivID]];
NSLog(@"fetching:%@",url);
NSURLRequest* urlRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30];
temporaryData=[NSMutableData data];
returnDict=[NSMutableDictionary dictionary];
delegate=dele;
sel=s;
connection=[NSURLConnection connectionWithRequest:urlRequest
delegate:self];
}
/*-(NSXMLDocument*)xmlForPath:(NSString*)path
{
NSString*p=[[path componentsSeparatedByString:@"/"]objectAtIndex:0];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@/rss/%@",[self arXivHead],p]];
NSError*error=nil;
NSXMLDocument*doc=[[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error];
return doc;
}*/
-(NSString*)list_internal:(NSString*)path
{
NSURL* url=[NSURL URLWithString:[NSString stringWithFormat:@"%@list/%@",[self arXivHead],path]];
NSURLRequest*request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
NSLog(@"fetching:%@",url);
NSError*error=nil;
NSURLResponse*responsee=nil;
NSData*data=[NSURLConnection sendSynchronousRequest:request returningResponse:&responsee error:&error];
NSString* s=nil;
if(data){
s=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(!s){
s=[[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}
}
if(!s)return nil;
if([s isEqualToString:@""]) return nil;
return s;
}
-(NSString*)list:(NSString*)path
{
NSArray* a=[path componentsSeparatedByString:@"/"];
NSString* category=[a objectAtIndex:0];
NSString* t=[a objectAtIndex:1];
if([t hasPrefix:@"new"]){
NSString* content=[self list_internal:[NSString stringWithFormat:@"%@/%@",category,@"new"]];
NSRange r=[content rangeOfString:@"<h3>Cross"];
if(r.location!=NSNotFound){
content=[content substringToIndex:r.location];
}
NSRange s=[content rangeOfString:@"<h3>Repla"];
if(s.location!=NSNotFound){
content=[content substringToIndex:s.location];
}
return content;
}else if([t hasPrefix:@"rep"]){
NSString* content=[self list_internal:[NSString stringWithFormat:@"%@/%@",category,@"new"]];
NSRange r=[content rangeOfString:@"<h3>Repla"];
if(r.location!=NSNotFound){
content=[content substringFromIndex:r.location];
}else{
content=nil;
}
return content;
}else if([t hasPrefix:@"cros"]){
NSString* content=[self list_internal:[NSString stringWithFormat:@"%@/%@",category,@"new"]];
NSRange r=[content rangeOfString:@"<h3>Cross"];
NSRange s=[content rangeOfString:@"<h3>Repl"];
if(r.location!=NSNotFound){
if(s.location!=NSNotFound){
content=[content substringWithRange:NSMakeRange(r.location,s.location-r.location)];
}else{
content=[content substringFromIndex:r.location];
}
}else{
content=nil;
}
return content;
}else if([t hasPrefix:@"rec"]){
NSString* content=[self list_internal:[NSString stringWithFormat:@"%@/%@",category,@"pastweek?show=99"]];
NSRange r=[content rangeOfString:@"<h3>Cross"];
if(r.location!=NSNotFound){
content=[content substringToIndex:r.location];
}
return content;
}
NSLog(@"arxiv list %@ not understood",path);
return nil;
}
#pragma mark URL connection delegates
- (void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data
{
[temporaryData appendData:data];
}
-(void)connection:(NSURLConnection*)c didReceiveResponse:(NSURLResponse*)resp
{
response=resp;
}
-(void)connectionDidFinishLoading:(NSURLConnection*)c
{
if( ![[response MIMEType] hasSuffix:@"html"] && [temporaryData length]>10240){
[returnDict setValue:temporaryData forKey:@"pdfData"];
[returnDict setValue:[NSNumber numberWithBool:YES] forKey:@"success"];
}else{
[returnDict setValue:[NSNumber numberWithBool:NO] forKey:@"success"];
if([[response MIMEType] hasSuffix:@"html"]){
NSString*s=[[NSString alloc] initWithData:temporaryData encoding:NSUTF8StringEncoding];
NSArray*a=[s componentsSeparatedByString:@"http-equiv=\"refresh\" content=\""];
if([a count]>1){
s=[a objectAtIndex:1];
a=[s componentsSeparatedByString:@"\""];
if([a count]>0){
s=[a objectAtIndex:0];
NSNumber*num=[NSNumber numberWithInt:[s intValue]];
[returnDict setValue:num forKey:@"shouldReloadAfter"];
}
}
}
}
[delegate performSelector:sel withObject:returnDict];
connection=nil;
}
-(void)connection:(NSURLConnection*)c didFailWithError:(NSError*)error
{
[returnDict setValue:[NSNumber numberWithBool:NO] forKey:@"success"];
[returnDict setValue:error forKey:@"error"];
[delegate performSelector:sel withObject:returnDict];
NSAlert*alert=[NSAlert alertWithMessageText:@"Connection Error to arXiv"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil informativeTextWithFormat:[error localizedDescription]];
//[alert setAlertStyle:NSCriticalAlertStyle];
[alert beginSheetModalForWindow:[[NSApp appDelegate] mainWindow]
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];
}
@end