-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupindia_dl.py
43 lines (38 loc) · 1.28 KB
/
upindia_dl.py
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
import re
import logging
import urllib.parse
import requests
logger = logging.getLogger('upindia_dl')
def get_direct_download_link(url):
REGEX = r'(http[s]*://(?:upindia|uploadfile|upload)\.(?:cc|mobi)+/\d{6}/\S{7})'
match = re.findall(REGEX, url)
if not match:
return "The provided link do not match with the standard format."
session = requests.Session()
url = match[0]
logger.debug(f"Matched url: {url}")
file_id, file_code = url.split('/')[-2:]
logger.debug(f"file_code: {file_code}, file_id: {file_id}")
url_parts = urllib.parse.urlparse(url)
req = session.get(url)
page_html = req.text
itemlink = re.findall(r'class="download_box_new[^"]*".*itemlink="([^">]+)"', page_html)
if not itemlink:
return "File does not exist!"
itemlink = itemlink[0]
itemlink_parsed = urllib.parse.parse_qs(itemlink)
file_key = itemlink_parsed['down_key'][0]
logger.debug(f"file_key: {file_key}")
params = {
'file_id':file_id,
'file_code':file_code,
'file_key':file_key,
'serv':1
}
req_url = url_parts.scheme + '://' + url_parts.netloc + "/download"
r = session.head(req_url, params=params)
dl_url = r.headers.get('location', None)
if dl_url is None:
return "This file cannot be downloaded at this moment!"
logger.debug(dl_url)
return dl_url