-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRadarDownloader.m
101 lines (74 loc) · 2.54 KB
/
RadarDownloader.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
//
// RadarDownloader.m
// BrissyBom
//
// Created by Michael Dawson on 26/01/11.
// Copyright 2011 Nippysaurus. All rights reserved.
//
#import "RadarDownloader.h"
@implementation RadarDownloader
-(id)init
{
if (self = [super init])
{
fileNameDateFormat = [[NSDateFormatter alloc] init]; // 1
[fileNameDateFormat setDateFormat:@"yyyyMMddHHmm"];
[fileNameDateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
}
return self;
}
-(NSString*)urlForDate:(NSDate*)time
{
NSString* fileNameDate = [fileNameDateFormat stringFromDate:time]; // auto
Configuration* configuration = [Configuration sharedManager];
NSString* radarImageTemplate = configuration.RadarImageTemplate;
NSString* location = [[NSUserDefaults standardUserDefaults] valueForKey:@"CurrentLocation"]; // auto
NSString* radar = [configuration.Locations objectForKey:location]; // auto
NSString* result = [NSString stringWithFormat:radarImageTemplate, radar, fileNameDate]; // auto?
return result;
}
-(NSDictionary*)radarUrlSequenceStarting:(NSDate*)starting AtIntervals:(NSTimeInterval)interval ForMaximum:(NSTimeInterval)max
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary* result = [[NSMutableDictionary alloc] init]; // 1
NSDate* lowerLimit = [starting dateByAddingTimeInterval:-max]; // auto
NSDate* upperLimit = [starting dateByAddingTimeInterval:max]; // auto
while ([starting timeIntervalSinceDate:lowerLimit] > 0 && [starting timeIntervalSinceDate:upperLimit] < 0)
{
NSString* url = [self urlForDate:starting];
[result setObject:url forKey:starting];
starting = [starting dateByAddingTimeInterval:interval];
}
[pool drain];
return [result autorelease];
}
-(BOOL)latestRadarAfter:(NSDate*)starting outDate:(NSDate**)outDate outUrl:(NSString**)outUrl outData:(NSData**)outData
{
NSDictionary* urls = [self radarUrlSequenceStarting:starting
AtIntervals:(NSTimeInterval)-(1 * 60)
ForMaximum:(NSTimeInterval)(10 * 60)]; // auto
NSLog(@"urls to download: %@", urls);
NSEnumerator* enumerator = [urls keyEnumerator]; // auto
id key = nil;
while (key = [enumerator nextObject]) // auto
{
NSString* value = [urls objectForKey:key]; // auto
NSURL *url = [NSURL URLWithString:value]; // auto
NSData* data = [NSData dataWithContentsOfURL: url]; // auto
if (data != nil)
{
NSLog(@"the winner was: %@", url);
*outDate = key;
*outUrl = value;
*outData = data;
return YES;
}
}
return NO;
}
-(void)dealloc
{
[fileNameDateFormat release];
[super dealloc];
}
@end