-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.tsx
181 lines (171 loc) · 4.72 KB
/
App.tsx
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
import { ThemedText } from "@/components/ThemedText";
import { ThemedView } from "@/components/ThemedView";
import VideoPlayer from "@/components/VideoPlayer";
import { checkUrlPatterns, replaceNetlocWithNewDomain } from "@/helper/utils";
import { ApiResponse } from "@/types/api";
import * as Linking from "expo-linking";
import { useCallback, useEffect, useRef, useState } from "react";
import {
ActivityIndicator,
StatusBar,
StyleSheet,
TextInput,
ToastAndroid,
TouchableOpacity,
} from "react-native";
export interface ApiResponseTypes {
response: ApiResponse[];
}
const Page = () => {
const [text, setText] = useState("");
const inputRef = useRef<TextInput>(null);
const [data, setData] = useState<ApiResponse | null>(null);
const [isFetching, setIsFetching] = useState(false);
const clearText = () => {
setText("");
if (inputRef && inputRef.current) {
inputRef.current.clear();
}
if (data) {
setData(null);
}
};
const submit = async (text: string) => {
const check = checkUrlPatterns(text);
if (!check) return ToastAndroid.show("Invalid URI.", ToastAndroid.SHORT);
setIsFetching(true);
const url = replaceNetlocWithNewDomain(text);
const req = await fetch(
"https://ytshorts.savetube.me/api/v1/terabox-downloader",
{
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0",
Accept: "application/json, text/plain, */*",
"Accept-Language": "en-US,en;q=0.5",
"Content-Type": "application/json",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
Priority: "u=0",
},
body: JSON.stringify({ url: url }),
method: "POST",
}
);
setIsFetching(false);
if (!req.ok) {
ToastAndroid.show("Failed to fetch data", ToastAndroid.SHORT);
return;
}
const res: ApiResponseTypes = await req.json();
setData(res.response[0]);
};
const getInitUrl = useCallback(() => {
const func = async () => {
const data = await Linking.getInitialURL();
const check = checkUrlPatterns(text);
if (!check || !data || data.length < 0) return;
setText(data);
submit(data);
};
func();
}, []);
useEffect(() => {
const handleUrl = ({ url }: { url: string }) => {
setText(url);
submit(url);
};
Linking.addEventListener("url", handleUrl);
}, []);
useEffect(() => {
getInitUrl();
}, []);
return (
// <SafeAreaView style={{ flex: 1 }}>
<ThemedView style={{ flex: 1 }}>
<StatusBar backgroundColor={"black"} />
<ThemedText
style={{
fontSize: 18,
textAlign: "center",
paddingVertical: 10,
borderBottomColor: "white",
borderBottomWidth: StyleSheet.hairlineWidth,
fontWeight: "500",
}}
>
Terabox Player
</ThemedText>
<ThemedView style={{ flex: 1, paddingHorizontal: 10 }}>
<ThemedView style={styles.container}>
<TextInput
style={styles.input}
ref={inputRef}
value={text}
onChangeText={setText}
placeholder="Enter your text here"
placeholderTextColor={styles.input.color}
/>
<TouchableOpacity
onPress={clearText}
style={{}}
disabled={isFetching}
>
<ThemedText style={styles.btn}>❌</ThemedText>
</TouchableOpacity>
</ThemedView>
<TouchableOpacity onPress={() => submit(text)} style={{}}>
<ThemedText style={styles.submitBtn}>Play</ThemedText>
</TouchableOpacity>
{isFetching ? (
<ActivityIndicator
size={30}
style={{ flex: 1, justifyContent: "center", alignItems: "center" }}
/>
) : data ? (
<VideoPlayer data={data} />
) : null}
</ThemedView>
</ThemedView>
// </SafeAreaView>
);
};
export default Page;
const styles = StyleSheet.create({
container: {
width: "100%",
flexDirection: "row",
gap: 4,
paddingVertical: 10,
},
input: {
borderColor: "gray",
color: "white",
borderWidth: 1,
padding: 10,
flex: 1,
backgroundColor: "rgba(0, 0, 0, 0.3)",
borderRadius: 6,
},
btn: {
borderColor: "gray",
color: "white",
borderWidth: 1,
padding: 10,
flex: 1,
backgroundColor: "transparent",
borderRadius: 6,
textAlignVertical: "center",
},
submitBtn: {
borderColor: "gray",
color: "white",
textAlign: "center",
width: "100%",
borderWidth: 1,
padding: 10,
backgroundColor: "green",
borderRadius: 6,
},
});