1
1
'use strict' ;
2
2
3
+ import fs from "fs" ;
4
+ import path from "path" ;
5
+ import { execSync } from "child_process" ;
6
+ import { fileTypeFromBuffer } from "file-type" ;
7
+
3
8
export default {
4
9
views : [ 'tiktok < url > --flag' ] , // view for message in menu
5
10
command : / ^ ( t t d l | t i k t o k | t t ) $ / i, //another command.
6
11
description : 'Download media from Tiktok Url' ,
7
12
query : true ,
8
13
url : true ,
9
14
usage : '%cmd% url tiktok --wm or --nowm\nFlag --wm untuk video dengan watermark.\nFlag --nowm untuk video tanpa watermark.' ,
10
- execute : async ( { xcoders, x, m, query, styleMessage, invalidUrlMessage , errorMessage , replyMessage, waitingMessage , apikeys, regex , host, getMessage, parseResult, getJson, addHitCommand } ) => {
15
+ execute : async ( { xcoders, x, m, query, styleMessage, errorMessage , waitingMessage , replyMessage, apikeys, host, getMessage, parseResult, getJson, addHitCommand, convertToBuffer , getRandom , zipFolder , invalidUrlMessage , regex } ) => {
11
16
try {
12
17
if ( ! regex . media ( query ) ) return invalidUrlMessage ( m . chat ) ;
13
18
let flag = null ;
@@ -25,9 +30,32 @@ export default {
25
30
const caption = styleMessage ( 'Tiktok Media Downloader' , result ) ;
26
31
addHitCommand ( 'Tiktok Downloader' , true ) ;
27
32
if ( data . result ?. result_url ) {
28
- for ( let { display_image } of data . result . result_url ) {
29
- await xcoders . sendFileFromUrl ( m . chat , display_image . url_list [ 1 ] , caption , x ) ;
33
+ await replyMessage ( '*_Success convert to file zip..._*' , 'success' ) ;
34
+ const pathFolder = path . join ( process . cwd ( ) , `tiktok_result_${ Date . now ( ) } ` ) ;
35
+ const pathZip = path . join ( process . cwd ( ) , 'temp' , 'tiktok_result_images.zip' ) ;
36
+
37
+ if ( ! fs . existsSync ( pathFolder ) ) fs . mkdirSync ( pathFolder ) ;
38
+ if ( ! fs . existsSync ( path . join ( pathFolder , 'photos' ) ) ) fs . mkdirSync ( path . join ( pathFolder , 'photos' ) ) ;
39
+ if ( ! fs . existsSync ( path . join ( pathFolder , 'music' ) ) ) fs . mkdirSync ( path . join ( pathFolder , 'music' ) ) ;
40
+
41
+ const musicBuffer = await fetch ( data . result . music_url ) . then ( async ( response ) => convertToBuffer ( await response . arrayBuffer ( ) ) ) ;
42
+ await fs . promises . writeFile ( path . join ( pathFolder , 'music' , `${ data . result . music_info . title } .mp3` ) , musicBuffer ) ;
43
+ for await ( let { display_image } of data . result . result_url ) {
44
+ const buffer = await fetch ( display_image . url_list [ 1 ] ) . then ( response => response . arrayBuffer ( ) ) ;
45
+ const result = convertToBuffer ( buffer ) ;
46
+ await fs . promises . writeFile ( path . join ( pathFolder , 'photos' , getRandom ( '.jpeg' ) ) , result ) ;
30
47
}
48
+ zipFolder ( pathFolder , pathZip , async function ( error , message ) {
49
+ if ( error ) {
50
+ console . error ( error ) ;
51
+ return errorMessage ( m . chat , null , 'Tiktok Downloader' ) ;
52
+ }
53
+ const responseResult = await fs . promises . readFile ( pathZip ) ;
54
+ const type = await fileTypeFromBuffer ( responseResult ) ;
55
+ execSync ( `rm -r "${ pathFolder } "` ) ;
56
+ if ( fs . existsSync ( pathZip ) ) await fs . promises . unlink ( pathZip ) ;
57
+ return xcoders . sendMessage ( m . chat , { document : responseResult , caption : message , jpegThumbnail : global . icon , mimetype : type . mime , fileName : getRandom ( type . ext ) , contextInfo : { forwardingScore : 9999999 , isForwarded : true } } , { quoted : x } ) ;
58
+ } ) ;
31
59
} else {
32
60
return xcoders . sendFileFromUrl ( m . chat , data . result [ flag ] , caption , x ) ;
33
61
}
0 commit comments