-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (51 loc) · 2.34 KB
/
Copy pathmain.py
File metadata and controls
64 lines (51 loc) · 2.34 KB
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
from os.path import join as join_path
from secrets import bot_token
import requests
base_url = f'https://api.telegram.org'
url_with_token = join_path(base_url, bot_token)
def gather_media_ids(data, username, photo_ids, video_ids):
for message in data['result']:
message_username = message['message']['from']['username'].lower()
if message_username == username and ('photo' in message['message']):
photo_ids.append(message['message']['photo'][-1]['file_id'])
if message_username == username and ('video' in message['message']):
video_ids.append(message['message']['video']['file_id'])
def get_videos(ids):
for (idx, id) in enumerate(ids):
params = {
'file_id': id,
}
resp = requests.get(url=join_path(url_with_token, 'getFile'), params=params).json()
path = resp['result']['file_path']
name = path[7:len(path)]
print('Downloading %.2f MB video...' % (resp['result']['file_size'] / 2 ** 20))
file_resp = requests.get(url=join_path(base_url, 'file', bot_token, path))
with open(name, 'wb') as f:
for (idx2, chunk) in enumerate(file_resp.iter_content(chunk_size=1024)):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
print('video ' + str(idx + 1) + ' saved!')
def get_photos(ids):
for (idx, id) in enumerate(ids):
params = {
'file_id': id,
}
resp = requests.get(url=join_path(url_with_token, 'getFile'), params=params).json()
path = resp['result']['file_path']
name = path[7:len(path)]
file_resp = requests.get(url=join_path(base_url, 'file', bot_token, path))
with open(name, 'wb') as f:
for chunk in file_resp.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
print('photo ' + str(idx + 1) + ' saved!')
if __name__ == "__main__":
parsed_response = requests.get(join_path(url_with_token, 'getUpdates')).json()
username = (input('enter telegram id: ')).lower()
photo_ids = []
video_ids = []
gather_media_ids(parsed_response, username, photo_ids, video_ids)
print(len(photo_ids), ' photos found!')
print(len(video_ids), ' videos found!')
get_photos(photo_ids)
get_videos(video_ids)