This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJakimSolatParser.m
200 lines (163 loc) · 8.01 KB
/
JakimSolatParser.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
//
// JakimSolatParser.m
//
// Created by Sumardi Shukor <[email protected]> on 2/26/12.
// Copyright (c) 2012 Wutmedia. All rights reserved.
//
// JakimSolatWrapper is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with JakimSolatWrapper. If not, see <http://www.gnu.org/licenses/>
//
#import "JakimSolatParser.h"
#import "JakimPrayerTime.h"
#import "XPathQuery.h"
@implementation JakimSolatParser
@synthesize prayerTime, delegate;
- (id)init
{
if (self = [super init]) {
prayerTimeData = [[NSMutableData alloc] init];
prayerTime = [[JakimPrayerTime alloc] init];
[self initWithCode:@"sgr03"];
}
return self;
}
- (id)initWithURL:(NSURL *)urlJakim
{
if (self = [super init]) {
prayerTimeData = [[NSMutableData alloc] init];
prayerTime = [[JakimPrayerTime alloc] init];
if([urlJakim isKindOfClass:[NSString class]]) {
urlJakim = [NSURL URLWithString:(NSString *)urlJakim];
}
url = urlJakim;
NSArray *pairs = [[url query] componentsSeparatedByString:@"&"];
NSMutableDictionary *kvPairs = [NSMutableDictionary dictionary];
for (NSString *pair in pairs) {
NSArray *bits = [pair componentsSeparatedByString:@"="];
NSString *key = [[bits objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *value = [[bits objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
[kvPairs setObject:value forKey:key];
}
prayerTime.code = [kvPairs objectForKey:@"kod"];
}
return self;
}
- (id)initWithCode:(NSString *)code
{
if (self = [super init]) {
prayerTimeData = [[NSMutableData alloc] init];
prayerTime = [[JakimPrayerTime alloc] init];
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.e-solat.gov.my/solat.php?kod=%@&lang=EN&url=http://www.wutlab.com&frameborder=0", code]];
prayerTime.code = code;
}
return self;
}
- (BOOL)parse
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
[request setValue:@"JakimSolatParser" forHTTPHeaderField:@"User-Agent"];
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
return YES;
}
- (void)dispatchSolatToDelegate
{
if (prayerTime) {
if([[self delegate] respondsToSelector:@selector(jakimSolatParser:didParsePrayerTime:)]) {
[[self delegate] jakimSolatParser:self didParsePrayerTime:prayerTime];
}
}
}
- (void)performXPathQuery
{
NSArray *node = [[NSArray alloc] init];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//b");
prayerTime.location = [[node objectAtIndex:0] objectForKey:@"nodeContent"];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd"];
[dateComponents setDay:[[format stringFromDate:currentDate] intValue]];
[format setDateFormat:@"MM"];
[dateComponents setMonth:[[format stringFromDate:currentDate] intValue]];
[format setDateFormat:@"yyyy"];
[dateComponents setYear:[[format stringFromDate:currentDate] intValue]];
[dateComponents setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+8"]];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[6]/td/font[6]");
NSArray *imsakTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[imsakTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[imsakTime objectAtIndex:1] intValue]];
prayerTime.imsak = [cal dateFromComponents:dateComponents];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[7]/td/font[6]");
NSArray *subuhTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[subuhTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[subuhTime objectAtIndex:1] intValue]];
prayerTime.subuh = [cal dateFromComponents:dateComponents];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[8]/td/font[6]");
NSArray *syurukTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[syurukTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[syurukTime objectAtIndex:1] intValue]];
prayerTime.syuruk = [cal dateFromComponents:dateComponents];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[9]/td/font[6]");
NSArray *zohorTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[zohorTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[zohorTime objectAtIndex:1] intValue]];
prayerTime.zohor = [cal dateFromComponents:dateComponents];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[10]/td/font[6]");
NSArray *asarTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[asarTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[asarTime objectAtIndex:1] intValue]];
prayerTime.asar = [cal dateFromComponents:dateComponents];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[11]/td/font[6]");
NSArray *maghribTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[maghribTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[maghribTime objectAtIndex:1] intValue]];
prayerTime.maghrib = [cal dateFromComponents:dateComponents];
node = PerformHTMLXPathQuery([NSData dataWithData:prayerTimeData], @"//table/tr[12]/td/font[6]");
NSArray *isyakTime = [[[node objectAtIndex:0] objectForKey:@"nodeContent"] componentsSeparatedByString:@":"];
[dateComponents setHour:[[isyakTime objectAtIndex:0] intValue]];
[dateComponents setMinute:[[isyakTime objectAtIndex:1] intValue]];
prayerTime.isyak = [cal dateFromComponents:dateComponents];
[self dispatchSolatToDelegate];
}
- (void)dealloc
{
[prayerTimeData release];
[urlConnection release];
[prayerTime release];
[delegate release];
[url release];
[super dealloc];
}
#pragma mark - Connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[prayerTimeData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[prayerTimeData appendData:data];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if ([[self delegate] respondsToSelector:@selector(jakimSolatParser:didFailWithError:)]) {
[[self delegate] jakimSolatParser:self didFailWithError:error];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self performXPathQuery];
}
@end