-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArxivMetadataFetchOperation.m
106 lines (98 loc) · 3.43 KB
/
ArxivMetadataFetchOperation.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
//
// ArxivMetadataFetchOperation.m
// spires
//
// Created by Yuji on 09/02/08.
// Copyright 2009 Y. Tachikawa. All rights reserved.
//
#import "ArxivMetadataFetchOperation.h"
#import "Article.h"
#import "NSString+magic.h"
#import "MOC.h"
@implementation ArxivMetadataFetchOperation
{
NSString*eprint;
NSString*xmlString;
}
-(ArxivMetadataFetchOperation*)initWithArticle:(Article*)a;
{
self=[super init];
eprint=a.eprint;
return self;
}
-(NSString*)description
{
return [NSString stringWithFormat:@"fetching metadata for %@",eprint];
}
-(NSString*)valueForXMLTag:(NSString*)tag
{
NSString*regex=[NSString stringWithFormat:@"<%@[^>]*>(.+?)</%@",tag,tag];
NSString*s=[xmlString stringByMatching:regex options:RKLDotAll inRange:NSMakeRange(0,xmlString.length) capture:1 error:NULL];
if(!s || [s isEqualToString:@""])
return nil;
return s;
}
-(void)main
{
// see http://export.arxiv.org/api_help/docs/user-manual.html
NSString*arXivID=eprint;
if([arXivID hasPrefix:@"arXiv:"]){
arXivID=[arXivID substringFromIndex:[(NSString*)@"arXiv:" length]];
}
NSURL* url=[NSURL URLWithString:[NSString stringWithFormat:@"https://export.arxiv.org/api/query?id_list=%@",arXivID]];
NSLog(@"query:%@",url);
xmlString=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
xmlString=[self valueForXMLTag:@"entry"];
NSMutableDictionary* dict=[NSMutableDictionary dictionary];
{
NSString* comment=[self valueForXMLTag:@"arxiv:comment"];
if(comment){
comment=[comment stringByReplacingOccurrencesOfString:@"\n " withString:@" "];
comment=[comment stringByReplacingOccurrencesOfString:@" \n" withString:@" "];
comment=[comment stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
dict[@"comments"]=comment;
}
}
{
NSString*pc=[xmlString stringByMatching:@"term=\"([^\"]+)\"" capture:1];
if(pc && ![pc isEqualToString:@""]){
dict[@"primaryCategory"]=pc;
}
}
{
NSString* s=[self valueForXMLTag:@"id"];
s=[s substringFromIndex:[(NSString*)@"https://arxiv.org/abs/" length]];
NSArray*a=[s componentsSeparatedByString:@"v"];
int v=[[a lastObject] intValue];
if(v==0){
dict=nil;
}else{
dict[@"version"]=@(v);
NSString*abstract=[self valueForXMLTag:@"summary"];
[dict setValue:abstract forKey:@"abstract"];
}
}
{
NSString*title=[self valueForXMLTag:@"title"];
title=[title stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
title=[title stringByReplacingOccurrencesOfRegex:@" +" withString:@" "];
dict[@"title"]=title;
}
if(dict){
NSManagedObjectContext*s=[[MOC sharedMOCManager] createSecondaryMOC];
[s performBlock:^{
Article*article=[Article intelligentlyFindArticleWithId:eprint inMOC:s];
{
article.abstract=dict[@"abstract"];
article.version=dict[@"version"];
article.comments=dict[@"comments"];
if(![[article.title lowercaseString] isEqualToString:[dict[@"title"] lowercaseString]]){
article.title=dict[@"title"];
}
article.arxivCategory=dict[@"primaryCategory"];
}
[s save:NULL];
}];
}
}
@end