forked from dayanch96/YTMusicUltimate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloading.x
172 lines (139 loc) · 7.35 KB
/
Downloading.x
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
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "FFMpegDownloader.h"
#import "Headers/YTUIResources.h"
#import "Headers/YTMActionSheetController.h"
#import "Headers/YTMActionRowView.h"
#import "Headers/YTIPlayerOverlayRenderer.h"
#import "Headers/YTIPlayerOverlayActionSupportedRenderers.h"
#import "Headers/YTMNowPlayingViewController.h"
#import "Headers/YTPlayerView.h"
#import "Headers/YTIThumbnailDetails_Thumbnail.h"
#import "Headers/YTIFormatStream.h"
#import "Headers/YTAlertView.h"
#import "Headers/ELMNodeController.h"
static BOOL YTMU(NSString *key) {
NSDictionary *YTMUltimateDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"YTMUltimate"];
return [YTMUltimateDict[key] boolValue];
}
@interface UIView ()
- (UIViewController *)_viewControllerForAncestor;
@end
@interface ELMTouchCommandPropertiesHandler : NSObject
- (void)downloadAudio:(YTPlayerViewController *)playerResponse;
- (void)downloadCoverImage:(YTPlayerViewController *)playerResponse;
- (NSString *)getURLFromManifest:(NSURL *)manifest;
@end
%hook ELMTouchCommandPropertiesHandler
- (void)handleTap {
if (class_getInstanceVariable([self class], "_controller") == NULL) {
return %orig;
}
if (class_getInstanceVariable([self class], "_tapRecognizer") == NULL) {
return %orig;
}
ELMNodeController *node = [self valueForKey:@"_controller"];
UIGestureRecognizer *tapRecognizer = [self valueForKey:@"_tapRecognizer"];
if (![node.key isEqualToString:@"music_download_badge_1"]) {
return %orig;
}
if (![tapRecognizer.view._viewControllerForAncestor isKindOfClass:%c(YTMNowPlayingViewController)]) {
return %orig;
}
YTMNowPlayingViewController *playingVC = (YTMNowPlayingViewController *)tapRecognizer.view._viewControllerForAncestor;
YTMWatchViewController *watchVC = (YTMWatchViewController *)playingVC.parentViewController;
YTPlayerViewController *playerVC = watchVC.playerViewController;
YTPlayerResponse *playerResponse = playerVC.playerResponse;
if (playerResponse) {
YTMActionSheetController *sheetController = [%c(YTMActionSheetController) musicActionSheetController];
sheetController.sourceView = tapRecognizer.view;
[sheetController addHeaderWithTitle:LOC(@"SELECT_ACTION") subtitle:nil];
[sheetController addAction:[%c(YTActionSheetAction) actionWithTitle:LOC(@"DOWNLOAD_AUDIO") iconImage:[%c(YTUIResources) audioOutline] style:0 handler:^ {
[self downloadAudio:playerVC];
}]];
[sheetController addAction:[%c(YTActionSheetAction) actionWithTitle:LOC(@"DOWNLOAD_COVER") iconImage:[%c(YTUIResources) outlineImageWithColor:[UIColor whiteColor]] style:0 handler:^ {
[self downloadCoverImage:playerVC];
}]];
[sheetController addAction:[%c(YTActionSheetAction) actionWithTitle:LOC(@"DOWNLOAD_PREMIUM") iconImage:[%c(YTUIResources) downloadOutline] secondaryIconImage:[%c(YTUIResources) youtubePremiumBadgeLight] accessibilityIdentifier:nil handler:^ {
return %orig;
}]];
if (YTMU(@"downloadAudio") && YTMU(@"downloadCoverImage")) {
[sheetController presentFromViewController:playingVC animated:YES completion:nil];
} else if (YTMU(@"downloadAudio")) {
[self downloadAudio:playerVC];
} else if (YTMU(@"downloadCoverImage")) {
[self downloadCoverImage:playerVC];
}
} else {
YTAlertView *alertView = [%c(YTAlertView) infoDialog];
alertView.title = LOC(@"DONT_RUSH");
alertView.subtitle = LOC(@"DONT_RUSH_DESC");
[alertView show];
}
}
%new
- (void)downloadAudio:(YTPlayerViewController *)playerVC {
YTPlayerResponse *playerResponse = playerVC.playerResponse;
NSString *title = [playerResponse.playerData.videoDetails.title stringByReplacingOccurrencesOfString:@"/" withString:@""];
NSString *author = [playerResponse.playerData.videoDetails.author stringByReplacingOccurrencesOfString:@"/" withString:@""];
NSString *urlStr = playerResponse.playerData.streamingData.hlsManifestURL;
FFMpegDownloader *ffmpeg = [[FFMpegDownloader alloc] init];
ffmpeg.tempName = playerVC.contentVideoID;
ffmpeg.mediaName = [NSString stringWithFormat:@"%@ - %@", author, title];
ffmpeg.duration = round(playerVC.currentVideoTotalMediaTime);
NSString *extractedURL = [self getURLFromManifest:[NSURL URLWithString:urlStr]];
if (extractedURL.length > 0) {
[ffmpeg downloadAudio:extractedURL];
NSMutableArray *thumbnailsArray = playerResponse.playerData.videoDetails.thumbnail.thumbnailsArray;
YTIThumbnailDetails_Thumbnail *thumbnail = [thumbnailsArray lastObject];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnail.URL]];
if (imageData) {
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *coverURL = [documentsURL URLByAppendingPathComponent:[NSString stringWithFormat:@"YTMusicUltimate/%@ - %@.png", author, title]];
[imageData writeToURL:coverURL atomically:YES];
}
} else {
YTAlertView *alertView = [%c(YTAlertView) infoDialog];
alertView.title = LOC(@"OOPS");
alertView.subtitle = LOC(@"LINK_NOT_FOUND");
[alertView show];
}
}
%new
- (NSString *)getURLFromManifest:(NSURL *)manifest {
NSData *manifestData = [NSData dataWithContentsOfURL:manifest];
NSString *manifestString = [[NSString alloc] initWithData:manifestData encoding:NSUTF8StringEncoding];
NSArray *manifestLines = [manifestString componentsSeparatedByString:@"\n"];
NSArray *groupIDS = @[@"234", @"233"]; // Our priority to find group id 234
for (NSString *groupID in groupIDS) {
for (NSString *line in manifestLines) {
NSString *searchString = [NSString stringWithFormat:@"TYPE=AUDIO,GROUP-ID=\"%@\"", groupID];
if ([line containsString:searchString]) {
NSRange startRange = [line rangeOfString:@"https://"];
NSRange endRange = [line rangeOfString:@"index.m3u8"];
if (startRange.location != NSNotFound && endRange.location != NSNotFound) {
NSRange targetRange = NSMakeRange(startRange.location, NSMaxRange(endRange) - startRange.location);
return [line substringWithRange:targetRange];
}
}
}
}
return nil;
}
%new
- (void)downloadCoverImage:(YTPlayerViewController *)playerVC {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
dispatch_async(dispatch_get_main_queue(), ^{
hud.mode = MBProgressHUDModeIndeterminate;
});
YTPlayerResponse *playerResponse = playerVC.playerResponse;
NSMutableArray *thumbnailsArray = playerResponse.playerData.videoDetails.thumbnail.thumbnailsArray;
YTIThumbnailDetails_Thumbnail *thumbnail = [thumbnailsArray lastObject];
NSString *thumbnailURL = [thumbnail.URL stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"w%u-h%u-", thumbnail.width, thumbnail.width] withString:@"w2048-h2048-"];
FFMpegDownloader *ffmpeg = [[FFMpegDownloader alloc] init];
[ffmpeg downloadImage:[NSURL URLWithString:thumbnailURL]];
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
}
%end