Skip to content

Commit

Permalink
fix: api changed
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ld3x committed Feb 26, 2024
1 parent 9f325a7 commit b4b39c7
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COOKIE="PANWEB=1; csrfToken=; lang=en; TSID=; __bid_n=; _ga=; __stripe_mid=; ndus=; browserid==; ndut_fmt=; _ga_06ZNKL8C2E="
# COOKIE FOR AUTHENTICATION (get from chrome dev tools)
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ yarn-error.log*
next-env.d.ts

.vercel
.env
yarn.lock
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
<h1 align="center" id="title">Terabox Downloader</h1>

![terabox-app](https://socialify.git.ci/r0ld3x/terabox-app/image?description=1&font=KoHo&forks=1&issues=1&language=1&name=1&owner=1&pattern=Circuit%20Board&pulls=1&stargazers=1&theme=Dark)

<h2>Demo</h2>
<img src="demo.png">

This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
<h2>🛠️ Installation Steps:</h2>

<p>1. <a href="https://nodejs.org/en/download/current">Download latest version of nodejs</a></p>
<p>2. <a href="https://github.com/r0ld3x/terabox-app/archive/refs/heads/main.zip">Download this repo</a></p>

<p>3. Unzip and open terminal in that folder</p>

<p>4. Download requirement's</p>

```
npm i
```

<p>5. Rename .env.example to .env and fill it</p>

```bash
COOKIE="PANWEB=1; csrfToken=; lang=en; TSID=; __bid_n=; _ga=; __stripe_mid=; ndus=; browserid==; ndut_fmt=; _ga_06ZNKL8C2E="
# COOKIE FOR AUTHENTICATION (get from chrome dev tools), Dont use current one


```

### Get terabox cookie:

1. Login in terabox with premium account
2. Open any terabox link and watch the video below

## Getting Started
*Download code*
```git clone https://github.com/r0ld3x/terabox-app.git```
https://github.com/r0ld3x/terabox-downloader-bot/assets/77254818/1b68e6ae-715f-4778-845e-4b696762ea93

*Install dependencies*
```npm i```
<p>6. Run the development server</p>

*Run the development server*
```npm run dev```
```
npm run dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
<p>7. Open <a href="http://localhost:3000">http://localhost:3000</a> with your browser to see the result.</p>

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
<h2>🍰 Guidelines:</h2>

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
Use at own risk. I am not responsible for break of any Terms and conditions of any companies and businesses.

## Learn More

Expand Down
127 changes: 121 additions & 6 deletions app/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
import axios from "axios";
import { NextRequest, NextResponse } from "next/server";
import { env } from "process";

function getFormattedSize(sizeBytes: number) {
let size, unit;

if (sizeBytes >= 1024 * 1024) {
size = sizeBytes / (1024 * 1024);
unit = "MB";
} else if (sizeBytes >= 1024) {
size = sizeBytes / 1024;
unit = "KB";
} else {
size = sizeBytes;
unit = "bytes";
}

return `${size.toFixed(2)} ${unit}`;
}

if (!env.COOKIE) {
throw new Error("Missing COOKIE in env");
}

interface ResponseData {
file_name: string;
link: string;
direct_link: string;
thumb: string;
size: string;
sizebytes: number;
}

function findBetween(str: string, start: string, end: string) {
const startIndex = str.indexOf(start) + start.length;
const endIndex = str.indexOf(end, startIndex);
return str.substring(startIndex, endIndex);
}

export async function GET(req: NextRequest, res: NextResponse) {
const { searchParams: params } = new URL(req.url);
Expand All @@ -9,15 +47,92 @@ export async function GET(req: NextRequest, res: NextResponse) {
if (!link) {
return NextResponse.json({ error: "Missing data" }, { status: 400 });
}
const axiosInstance = axios.create({
headers: {
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9,hi;q=0.8",
Connection: "keep-alive",
DNT: "1",
Host: "www.terabox.app",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
"sec-ch-ua":
'"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
Cookie: env.COOKIE,
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
},
});
try {
const data = await fetch(
`https://afca-174-138-88-233.ngrok-free.app/get?url=${link}`
);
const tempReq = await axiosInstance.get(link);

if (!data)
if (!tempReq)
return NextResponse.json({ error: "Unknown Error" }, { status: 400 });
const respo = await data.json();
return NextResponse.json(respo, { status: 200 });
const { searchParams: requestUrl, href } = new URL(
tempReq.request.res.responseUrl
);
if (!requestUrl.has("surl")) {
return NextResponse.json({ error: "Missing data" }, { status: 400 });
}
const req = await axiosInstance.get(tempReq.request.res.responseUrl);
const respo = await req.data;
const jsToken = findBetween(respo, "fn%28%22", "%22%29");
const logid = findBetween(respo, "dp-logid=", "&");
const bdstoken = findBetween(respo, 'bdstoken":"', '"');
if (!jsToken || !logid || !bdstoken) {
return NextResponse.json({ error: "Invalid response" }, { status: 400 });
}
const surl = requestUrl.get("surl");
const params = {
app_id: "250528",
web: "1",
channel: "dubox",
clienttype: "0",
jsToken: jsToken,
"dp-logid": logid,
page: "1",
num: "20",
by: "name",
order: "asc",
site_referer: href,
shorturl: surl,
root: "1,",
};
const req2 = await axiosInstance.get("https://www.terabox.app/share/list", {
params: params,
});
const responseData2 = req2.data;
if (
!responseData2 ||
!("list" in responseData2) ||
!responseData2["list"] ||
responseData2["errno"]
) {
return NextResponse.json({ error: "Unknown Error" }, { status: 400 });
}
const directLinkResponse = await axiosInstance.head(
responseData2["list"][0]["dlink"],
{
withCredentials: false,
}
);
const direct_link = directLinkResponse.request.res.responseUrl;
const data: ResponseData = {
file_name: responseData2["list"][0]["server_filename"],
link: responseData2["list"][0]["dlink"],
direct_link: direct_link,
thumb: responseData2["list"][0]["thumbs"]["url3"],
size: getFormattedSize(parseInt(responseData2["list"][0]["size"])),
sizebytes: parseInt(responseData2["list"][0]["size"]),
};
return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.json({ error: "Unknown Error" }, { status: 400 });
}
Expand Down
24 changes: 20 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import Link from "next/link";
import { useEffect, useState } from "react";
import useSWR from "swr";

interface ResponseData {
file_name: string;
link: string;
direct_link: string;
thumb: string;
size: string;
sizebytes: number;
}

const fetchWithToken = async (url: URL | RequestInfo) => {
const res = await fetch(url);
if (!res.ok) {
Expand Down Expand Up @@ -71,7 +80,7 @@ export default function Home() {
const [token, setToken] = useState("");
const [disableInput, setdisableInput] = useState(false);

const { data, error, isLoading } = useSWR(
const { data, error, isLoading } = useSWR<ResponseData>(
token ? [`/api?data=${encodeURIComponent(token)}`] : null,
([url]) => fetchWithToken(url),
{
Expand All @@ -82,6 +91,7 @@ export default function Home() {
);

useEffect(() => {
if (data) document.title = data.file_name;
if (data || error) {
setdisableInput(false);
setLink("");
Expand Down Expand Up @@ -114,10 +124,16 @@ export default function Home() {
<div className="self-center">
<Link href="/">Terabox Downloader</Link>
</div>
<ul>
<ul className="flex items-center gap-3 ">
<li>
{/* <Camera color="red" size={48} /> */}
<Button className="bg-slate-600 ">
<Link href="https://github.com/r0ld3x/terabox-app">Github</Link>
</Button>
</li>
<li>
{/* <Camera color="red" size={48} /> */}
<Button className="bg-blue-600">
<Button className="bg-blue-600 ">
<Link href="https://t.me/RoldexVerse">Telegram</Link>
</Button>
</li>
Expand Down Expand Up @@ -197,7 +213,7 @@ export default function Home() {
<h1 className="text-sm lg:text-xl text-white ">
Title:{" "}
<span className="text-white text-md lg:text-2xl font-bold ">
{data?.file_name}
{data.file_name}
</span>
</h1>
<h1 className="text-sm lg:text-xl text-white ">
Expand Down

0 comments on commit b4b39c7

Please sign in to comment.