19
19
// along with MyTube. If not, see <http://www.gnu.org/licenses/>.
20
20
//
21
21
22
+ #import " DownloadsViewController.h"
22
23
#import " MyTubeViewController.h"
23
24
24
25
@implementation MyTubeViewController
@@ -29,8 +30,11 @@ @implementation MyTubeViewController
29
30
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
30
31
- (void )viewDidLoad {
31
32
[super viewDidLoad ];
32
-
33
- [[UIApplication sharedApplication ] setDelegate: self ];
33
+
34
+ [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (applicationDidEnterBackground: ) name: UIApplicationDidEnterBackgroundNotification object: nil ];
35
+ [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (applicationWillEnterForeground: ) name: UIApplicationWillEnterForegroundNotification object: nil ];
36
+
37
+ [webView setBackgroundColor: [UIColor clearColor ]];
34
38
}
35
39
36
40
- (void )viewDidAppear : (BOOL )animated {
@@ -46,7 +50,6 @@ - (void)applicationDidEnterBackground:(UIApplication *)application {
46
50
[bar removeFromSuperview ];
47
51
48
52
[videoTitle release ];
49
- videoTitle = nil ;
50
53
51
54
[downloadButton setEnabled: YES ];
52
55
}
@@ -62,20 +65,31 @@ - (IBAction)download {
62
65
[downloadButton setEnabled: NO ];
63
66
64
67
[webView setUserInteractionEnabled: NO ];
65
-
66
- NSString *getURL = [webView stringByEvaluatingJavaScriptFromString: @" function getURL() {var player = document.getElementById('player'); var video = player.getElementsByTagName('video')[0]; return video.getAttribute('src');} getURL();" ];
67
- NSString *getTitle = [webView stringByEvaluatingJavaScriptFromString: @" function getTitle() {var kp = document.getElementsByClassName('kp')[0]; return kp.childNodes[0].innerHTML;} getTitle();" ];
68
+
69
+ UIUserInterfaceIdiom userInterfaceIdiom = [UIDevice currentDevice ].userInterfaceIdiom ;
70
+
71
+ NSString *getURL = @" " ;
72
+
73
+ if (userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
74
+ getURL = [webView stringByEvaluatingJavaScriptFromString: @" function getURL() {var player = document.getElementById('player'); var video = player.getElementsByTagName('video')[0]; return video.getAttribute('src');} getURL();" ];
75
+ } else {
76
+ getURL = [webView stringByEvaluatingJavaScriptFromString: @" function getURL() {var bh = document.getElementsByClassName('bh')[0]; return bh.getAttribute('src');} getURL();" ];
77
+ }
78
+
79
+ NSString *getTitle = [webView stringByEvaluatingJavaScriptFromString: @" function getTitle() {var lp = document.getElementsByClassName('lp')[0]; return lp.childNodes[0].innerHTML;} getTitle();" ];
80
+
68
81
NSString *getTitleFromChannel = [webView stringByEvaluatingJavaScriptFromString: @" function getTitleFromChannel() {var video_title = document.getElementById('video_title'); return video_title.childNodes[0].innerHTML;} getTitleFromChannel();" ];
69
-
82
+
83
+ // NSLog(@"%@, %@, %@", getURL, getTitle, getTitleFromChannel);
84
+
70
85
[webView setUserInteractionEnabled: YES ];
71
86
72
87
NSArray *components = [getTitle componentsSeparatedByCharactersInSet: [[NSCharacterSet alphanumericCharacterSet ] invertedSet ]];
73
88
getTitle = [components componentsJoinedByString: @" " ];
74
89
75
90
if ([getURL length ] > 0 ) {
76
91
if ([getTitle length ] > 0 ) {
77
- videoTitle = getTitle;
78
- [videoTitle retain ];
92
+ videoTitle = [getTitle retain ];
79
93
80
94
bar = [[UIDownloadBar alloc ] initWithURL: [NSURL URLWithString: getURL]
81
95
progressBarFrame: CGRectMake (85.0 , 17.0 , 150.0 , 11.0 )
@@ -92,8 +106,7 @@ - (IBAction)download {
92
106
getTitleFromChannel = [components componentsJoinedByString: @" " ];
93
107
94
108
if ([getTitleFromChannel length ] > 0 ) {
95
- videoTitle = getTitleFromChannel;
96
- [videoTitle retain ];
109
+ videoTitle = [getTitleFromChannel retain ];
97
110
98
111
bar = [[UIDownloadBar alloc ] initWithURL: [NSURL URLWithString: getURL]
99
112
progressBarFrame: CGRectMake (85.0 , 17.0 , 150.0 , 11.0 )
@@ -106,15 +119,21 @@ - (IBAction)download {
106
119
107
120
[bar release ];
108
121
} else {
122
+ // NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML;"]);
123
+
109
124
UIAlertView *alertView = [[UIAlertView alloc ] initWithTitle: @" MyTube" message: @" Couldn't get video title." delegate: nil cancelButtonTitle: @" OK" otherButtonTitles: nil ];
125
+
110
126
[alertView show ];
111
127
[alertView release ];
112
128
113
129
[downloadButton setEnabled: YES ];
114
130
}
115
131
}
116
132
} else {
133
+ // NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML;"]);
134
+
117
135
UIAlertView *alertView = [[UIAlertView alloc ] initWithTitle: @" MyTube" message: @" Couldn't get MP4 URL." delegate: nil cancelButtonTitle: @" OK" otherButtonTitles: nil ];
136
+
118
137
[alertView show ];
119
138
[alertView release ];
120
139
@@ -156,7 +175,7 @@ - (void)downloadBar:(UIDownloadBar *)downloadBar didFinishWithData:(NSData *)fil
156
175
157
176
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc ] initWithAsset: asset];
158
177
159
- Float64 durationSeconds = CMTimeGetSeconds ([ asset duration ] );
178
+ Float64 durationSeconds = CMTimeGetSeconds (asset. duration );
160
179
161
180
CMTime midpoint = CMTimeMakeWithSeconds (durationSeconds / 2.0 , 600 );
162
181
CMTime actualTime;
@@ -184,20 +203,18 @@ - (void)downloadBar:(UIDownloadBar *)downloadBar didFinishWithData:(NSData *)fil
184
203
[asset release ];
185
204
186
205
[videoTitle release ];
187
- videoTitle = nil ;
188
206
189
207
[downloadBar removeFromSuperview ];
190
208
191
- [downloadButton setEnabled: TRUE ];
209
+ [downloadButton setEnabled: YES ];
192
210
}
193
211
194
212
- (void )downloadBar : (UIDownloadBar *)downloadBar didFailWithError : (NSError *)error {
195
213
[videoTitle release ];
196
- videoTitle = nil ;
197
214
198
215
[downloadBar removeFromSuperview ];
199
216
200
- [downloadButton setEnabled: TRUE ];
217
+ [downloadButton setEnabled: YES ];
201
218
}
202
219
203
220
- (void )downloadBarUpdated : (UIDownloadBar *)downloadBar {}
@@ -221,12 +238,14 @@ - (void)didReceiveMemoryWarning {
221
238
}
222
239
223
240
- (void )viewDidUnload {
241
+ [super viewDidUnload ];
242
+
224
243
// Release any retained subviews of the main view.
225
244
// e.g. self.myOutlet = nil;
226
245
}
227
246
228
247
- (void )dealloc {
229
- [bar release ];
248
+ [[ NSNotificationCenter defaultCenter ] removeObserver: self ];
230
249
231
250
[super dealloc ];
232
251
}
0 commit comments