-
Notifications
You must be signed in to change notification settings - Fork 3
/
InspireJSONTransformer.m
148 lines (146 loc) · 4.76 KB
/
InspireJSONTransformer.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
//
// InspireJSONTransformer.m
// inspire
//
// Created by Yuji on 2020/06/10.
//
#import "InspireJSONTransformer.h"
#import "LightweightArticle.h"
NSString*usedFields=@"authors.full_name,abstracts,collaborations,arxiv_eprints,titles,number_of_pages,publication_info,earliest_date,dois,citation_count";
@implementation InspireJSONTransformer
+(NSArray*)articlesFromJSON:(NSDictionary*)jsonDict
{
static NSDateFormatter*df=nil;
static NSDateFormatter*ymdf=nil;
static NSDateFormatter*ydf=nil;
if(!df){
df=[[NSDateFormatter alloc] init];
df.dateFormat=@"yyyy-MM-dd";
df.timeZone=[NSTimeZone timeZoneForSecondsFromGMT:0];
df.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
if(!ymdf){
ymdf=[[NSDateFormatter alloc] init];
ymdf.dateFormat=@"yyyy-MM";
ymdf.timeZone=[NSTimeZone timeZoneForSecondsFromGMT:0];
ymdf.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
if(!ydf){
ydf=[[NSDateFormatter alloc] init];
ydf.dateFormat=@"yyyy";
ydf.timeZone=[NSTimeZone timeZoneForSecondsFromGMT:0];
ydf.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
NSMutableArray*array=[NSMutableArray array];
NSDictionary*h=jsonDict[@"hits"];
NSArray*hits=h[@"hits"];
for(NSDictionary*entry in hits){
LightweightArticle*article=[[LightweightArticle alloc] init];
article.inspireKey=[NSNumber numberWithInteger:[entry[@"id"] integerValue]];
NSDictionary*metadata=entry[@"metadata"];
{
NSArray*as=metadata[@"authors"];
for(NSDictionary*a in as){
[article addAuthor:a[@"full_name"]];
}
}
{
NSArray*as=metadata[@"abstracts"];
BOOL found=NO;
for(NSDictionary*a in as){
NSString*source=a[@"source"];
if([source isEqualToString:@"arXiv"]){
article.abstract=a[@"value"];
found=YES;
break;
}
}
if(!found && as && as[0]){
NSDictionary*a=as[0];
article.abstract=a[@"value"];
}
}
{
NSArray*as=metadata[@"collaborations"];
NSDictionary*a=as[0];
if(a){
article.collaboration=a[@"value"];
}
}
{
NSArray*as=metadata[@"arxiv_eprints"];
NSDictionary*a=as[0];
if(a){
NSString*s=a[@"value"];
if(![s containsString:@"/"]){
s=[@"arXiv:" stringByAppendingString:s];
}
article.eprint=s;
}
}
{
NSArray*as=metadata[@"titles"];
BOOL found=NO;
for(NSDictionary*a in as){
NSString*source=a[@"source"];
if([source isEqualToString:@"arXiv"]){
article.title=a[@"title"];
found=YES;
break;
}
}
if(!found && as && as[0]){
NSDictionary*a=as[0];
article.title=a[@"title"];
}
}
{
NSString*numString=metadata[@"number_of_pages"];
if(numString){
article.pages=[NSNumber numberWithInteger:[numString integerValue]];
}
}
{
NSArray*as=metadata[@"publication_info"];
NSDictionary*a=as[0];
if(a){
NSString*page=a[@"page_start"];
if(!page){
page=a[@"artid"];
}
article.journalPage=page;
article.journalYear=[NSNumber numberWithInteger:[a[@"year"] integerValue]];
article.journalTitle=a[@"journal_title"];
article.journalVolume=a[@"journal_volume"];
}
}
{
NSString*dateString=metadata[@"earliest_date"];
NSDate*date=[df dateFromString:dateString];
if(date){
article.date=date;
}else{
date=[ymdf dateFromString:dateString];
if(date){
article.date=date;
}else{
date=[ydf dateFromString:dateString];
article.date=date;
}
}
}
{
NSArray*as=metadata[@"dois"];
NSDictionary*a=as[0];
if(a){
article.doi=a[@"value"];
}
}
{
article.citecount=[NSNumber numberWithInteger:[metadata[@"citation_count"] integerValue]];
}
[array addObject:article];
}
return array;
}
@end