-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArxivNewArticleListReloadOperation.m
232 lines (220 loc) · 7.9 KB
/
ArxivNewArticleListReloadOperation.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// ArxivNewArticleListReloadOperation.m
// spires
//
// Created by Yuji on 8/26/09.
// Copyright 2009 Y. Tachikawa. All rights reserved.
//
#import "ArxivNewArticleListReloadOperation.h"
#import "ArxivNewArticleList.h"
#import "MOC.h"
#import "ArxivHelper.h"
#import "Article.h"
#import "ArticleData.h"
#import "AllArticleList.h"
#import "AppDelegate.h"
#import "NSString+magic.h"
@implementation ArxivNewArticleListReloadOperation
{
NSManagedObjectID* alID;
NSManagedObjectContext*secondMOC;
NSString* listName;
}
-(NSOperation*)initWithArxivNewArticleList:(ArxivNewArticleList*)a;
{
self=[super init];
alID=a.objectID;
listName=a.name;
secondMOC=[[MOC sharedMOCManager] createSecondaryMOC];
return self;
}
-(NSString*)description
{
return [@"reloading " stringByAppendingString:listName];
}
-(void)registerAuthorsInString:(NSString*)tmp toArticle:(Article*)ar
{
NSArray*authors=[tmp componentsSeparatedByString:@"\">"];
NSMutableArray* array=[NSMutableArray array];
if([authors count]>1){
for(NSUInteger i=1;i<[authors count];i++){
NSString*s=authors[i];
s=[s componentsSeparatedByString:@"</a>"][0];
s=[s stringByReplacingOccurrencesOfString:@"." withString:@". "];
NSArray*x=[s componentsSeparatedByString:@" "];
NSString*lastName=[x lastObject];
if([lastName isEqualToString:@""] && [x count]>=3){
// Names like "A. Bates, Jr." comes here
NSString*n1=x[[x count]-3];
NSString*n2=x[[x count]-2];
lastName=[NSString stringWithFormat:@"%@ %@",n1,n2];
x=[x subarrayWithRange:NSMakeRange(0,[x count]-2)];
}
if([x count]>=2){
NSArray*particles=[[NSUserDefaults standardUserDefaults] objectForKey:@"particles"];
NSString*pen=x[[x count]-2];
if([x count]>=2 && [particles containsObject:[pen lowercaseString]] ){
lastName=[NSString stringWithFormat:@"%@ %@",pen,lastName];
x=[x subarrayWithRange:NSMakeRange(0,[x count]-1)];
}
}
NSMutableArray*b=[NSMutableArray array];
for(NSUInteger j=0;j<[x count]-1;j++){
NSString*t=x[j];
if(![t isEqualToString:@""]){
[b addObject:t];
}
}
s=[NSString stringWithFormat:@"%@, %@",lastName, [b componentsJoinedByString:@" "]];
[array addObject:s];
}
[ar setAuthorNames:array];
}
}
-(NSString*)eprintFromChunk:(NSString*)s
{
NSRange r=[s rangeOfString:@"arXiv:"];
if(r.location==NSNotFound)
return nil;
NSString*eprint=[s substringFromIndex:r.location];
{
NSArray*x=[eprint componentsSeparatedByString:@"</a>"];
eprint=x[0];
if([eprint rangeOfString:@"/"].location!=NSNotFound){
eprint=[eprint substringFromIndex:[(NSString*)@"arXiv:" length]];
}
eprint=[eprint stringByReplacingOccurrencesOfRegex:@"[ \n]+" withString:@""];
}
return eprint;
}
-(Article*)dealWithChunk:(NSString*)s writeToArticle:(Article*)ar
{
// NSLog(@"%@",s);
NSString*eprint=[self eprintFromChunk:s];
NSArray*b=[s componentsSeparatedByString:@"<div class="];
if([b count]<4)
return nil;
NSMutableArray*a=[NSMutableArray array];
for(NSString*c in b){
NSString*d=c;
NSInteger i=[c rangeOfString:@"</span>"].location;
if(i!=NSNotFound){
d=[c substringFromIndex:i+[@"</span>" length]];
}
[a addObject:d];
}
/*
a[0], a[1] :junk
a[2]: title
a[3]: authors
a[4]: comments or abstract
a[5]: abstract if there're comments
*/
NSString*title=a[2];
NSInteger i=[title rangeOfString:@"</div>"].location;
title=[title substringToIndex:i];
title=[title stringByExpandingAmpersandEscapes];
title=[title stringByReplacingOccurrencesOfRegex:@"\n" withString:@""];
title=[title stringByReplacingOccurrencesOfRegex:@" +$" withString:@""];
title=[title stringByReplacingOccurrencesOfRegex:@"^ +" withString:@""];
// NSLog(@"%@",title);
NSString*authorsList=a[3];
i=[authorsList rangeOfString:@"</div>"].location;
authorsList=[authorsList substringToIndex:i];
authorsList=[authorsList stringByExpandingAmpersandEscapes];
NSString*comments=nil;
if([a[3] rangeOfString:@"omments"].location!=NSNotFound){
comments=a[4];
i=[comments rangeOfString:@"</div>"].location;
comments=[comments substringToIndex:i];
comments=[comments stringByExpandingAmpersandEscapes];
}
// NSLog(@"%@",authorsList);
NSString*abstract=[a lastObject];
abstract=[abstract stringByReplacingOccurrencesOfRegex:@"<p class=.mathjax.>" withString:@"<p>"];
if([abstract rangeOfString:@"<p>"].location!=NSNotFound){
abstract=[abstract componentsSeparatedByString:@"<p>"][1];
abstract=[abstract componentsSeparatedByString:@"</p>"][0];
// abstract=[abstract stringByExpandingAmpersandEscapes];
// abstract is fed to the html view anyway, so there's no need to expand &...; escapes here.
}else{
abstract=nil;
}
abstract=[abstract stringByReplacingOccurrencesOfRegex:@"^[ \n]+" withString:@""];
abstract=[abstract stringByReplacingOccurrencesOfRegex:@"[ \n]+$" withString:@""];
NSString*subj=[s stringByMatching:@"primary-subject\">(.+?)</span>" capture:1];
subj=[subj stringByMatching:@"\\((.+?)\\)" capture:1];
ar.eprint=eprint;
ar.abstract=abstract;
ar.version=@1;
ar.title=title;
ar.comments=comments;
ar.arxivCategory=subj;
ArticleFlag af=ar.flag;
if([[NSUserDefaults standardUserDefaults] boolForKey:@"shouldPutUnreadMarksForArxivNew"]){
af|=AFIsUnread;
}
[ar setFlag:af];
[self registerAuthorsInString:authorsList toArticle:ar];
return ar;
}
-(void)main
{
dispatch_async(dispatch_get_main_queue(),^{
[[NSApp appDelegate] postMessage:[NSString stringWithFormat:@"Reloading %@",listName]];
});
NSString*s=[[ArxivHelper sharedHelper] list:listName];
NSMutableArray*a=[[s componentsSeparatedByString:@"<dt>"] mutableCopy];
NSMutableDictionary*dict=[NSMutableDictionary dictionary];
for(NSString*chunk in a){
NSString*eprint=[self eprintFromChunk:chunk];
if(eprint){
dict[eprint] = chunk;
}
}
if([[dict allKeys] count]==0){
dispatch_async(dispatch_get_main_queue(),^{
[[NSApp appDelegate] postMessage:nil];
#if !TARGET_OS_IPHONE
NSAlert*alert=[[NSAlert alloc] init];
alert.messageText=@"No new articles today.";
[alert addButtonWithTitle:@"OK"];
alert.informativeText=@"It might also be due to an internet problem.";
[alert beginSheetModalForWindow:[[NSApp appDelegate] mainWindow]
completionHandler:nil];
#endif
});
return;
}
NSEntityDescription*entity=[NSEntityDescription entityForName:@"ArticleData" inManagedObjectContext:secondMOC];
NSFetchRequest*req=[[NSFetchRequest alloc]init];
[req setEntity:entity];
NSPredicate*pred=[NSPredicate predicateWithFormat:@"eprint IN %@",[dict allKeys]];
[req setPredicate:pred];
[req setIncludesPropertyValues:NO];
[secondMOC performBlockAndWait:^{
NSMutableSet*generated=[NSMutableSet set];
NSArray*datas=[secondMOC executeFetchRequest:req error:nil];
for(ArticleData*data in datas){
NSString*v=[data valueForKey:@"eprint"];
NSString*chunk=dict[v];
[self dealWithChunk:chunk writeToArticle:data.article];
[generated addObject:data.article];
[dict removeObjectForKey:v];
}
NSEntityDescription*articleEntity=[NSEntityDescription entityForName:@"Article" inManagedObjectContext:secondMOC];
for(NSString*chunk in [dict allValues]){
Article*article=(Article*)[[NSManagedObject alloc] initWithEntity:articleEntity insertIntoManagedObjectContext:secondMOC];
[self dealWithChunk:chunk writeToArticle:article];
[generated addObject:article];
}
[[AllArticleList allArticleListInMOC:secondMOC] addArticles:generated];
ArticleList*al=(ArticleList*)[secondMOC objectWithID:alID];
al.articles=generated;
[secondMOC save:NULL];
}];
dispatch_async(dispatch_get_main_queue(),^{
[[NSApp appDelegate] postMessage:nil];
});
}
@end