-
Notifications
You must be signed in to change notification settings - Fork 4
/
post.py
52 lines (47 loc) · 1.75 KB
/
post.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
44
45
46
47
48
49
50
51
52
import instaloader
from instaloader import Profile, Post
import os
import shutil
import main
from telegram import (
ReplyKeyboardMarkup,
Update,
ReplyKeyboardRemove,
Bot
)
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
)
def download(post_id,bot,update,username, password):
bot.sendMessage(update.effective_user.id, "Downloading content ...")
instagram = instaloader.Instaloader()
instagram.login(username, password)
instagram.dirname_pattern = os.path.join(
os.path.abspath("."), f"post/{username}")
post = Post.from_shortcode(instagram.context, post_id)
bot.sendMessage(update.effective_user.id, """Content successfully downloaded\nIt will be sent to you in a few moments""")
directory = os.path.join(
os.path.abspath("."), f"post/{username}")
instagram.download_post(post, target = instagram.dirname_pattern)
bot.sendMessage(update.effective_user.id, "Please wait a moment")
for filename in os.listdir(directory):
if filename == f'{post_id}.json.xz':
pass
elif filename == f'{post_id}.txt':
pass
elif filename.endswith('.mp4'):
bot.send_video(chat_id=update.effective_user.id,
video=open(f'{directory}/{filename}', 'rb'))
elif filename.endswith('.jpg'):
bot.send_photo(chat_id=update.effective_user.id,
photo=open(f'{directory}/{filename}', 'rb'))
elif filename.endswith('.webp'):
bot.send_photo(chat_id=update.effective_user.id,
photo=open(f'{directory}/{filename}', 'rb'))
shutil.rmtree(directory)
main.done(update, CallbackContext)