-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbot.py
282 lines (214 loc) · 12.2 KB
/
bot.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This program is dedicated to the public domain under the CC0 license.
# Coded with ❤️ by Neranjana Prasad (@NandiyaLive)
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, run_async
import requests
from bs4 import BeautifulSoup as bs
from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup
# from instaloader import Instaloader, Profile, Post
import sys
import shutil
import glob
import os
import zipfile
import pathlib
bot_token = os.environ.get("BOT_TOKEN", "")
bot = Bot(token=bot_token)
help_keyboard = [[InlineKeyboardButton("Updates Channel", url="https://t.me/MBNUpdates"),
InlineKeyboardButton("Support Chat", url="https://t.me/MBNChat")]]
help_reply_markup = InlineKeyboardMarkup(help_keyboard)
def start(update, context):
user = update.message.from_user
chat_member = context.bot.get_chat_member(
chat_id='-1001225141087', user_id=update.message.chat_id)
status = chat_member["status"]
if(status == 'left'):
context.bot.send_message(chat_id=update.message.chat_id,
text=f"Hi {user.first_name}, to use me you have to be a member of the updates channel in order to stay updated with the latest developments.\nPlease click below button to join and /start the bot again.", reply_markup=help_reply_markup)
return
else:
context.bot.send_message(chat_id=update.message.chat_id,
text=f"Hi {user.first_name}!\nI'm Instagram Media Downloader Bot. I can help you to download Stories and IGTV Videos from any public instagram account.\nPlease read the /help before using me.", parse_mode=telegram.ParseMode.HTML, reply_markup=help_reply_markup)
def help(update, context):
keyboard = [[InlineKeyboardButton("Updates Channel", url="https://t.me/MBNUpdates"),
InlineKeyboardButton("Support Chat", url="https://t.me/MBNChat")]]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('''<b>Usage:</b>\n/stories username - Download stories from the username’s profile.\n/igtv username - Download IGTV videos from the username’s profile.\n/feed username - Download all posts from the username’s profile as a zip file.\n\n<b>How to find the username?</b>\nOpen Instagram app & then go to a profile that you want to download items. Username must be on the top.\nIn case you are using a browser you can find it in the Address bar.\n<b>Example : </b>Username for instagram.com/rashmika_mandanna & @rashmika_mandanna is 'rashmika_mandanna' 😉''', parse_mode=telegram.ParseMode.HTML, reply_markup=reply_markup)
def about(update, context):
keyboard = [[InlineKeyboardButton(
"Source Code", url="https://github.com/NandiyaLive/xIGDLBot")]]
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(chat_id=update.message.chat_id,
text='''I can help you to download media from any public instagram account without leaving Telegram.\n\nMade with ❤️ + python-telegram-bot by @NandiyaLive''', parse_mode=telegram.ParseMode.HTML, reply_markup=reply_markup)
def echo(update, context):
context.bot.send_message(
chat_id=update.message.chat_id, text='''Please read /help''')
def stories(update, context):
user = context.bot.get_chat_member(
chat_id='-1001225141087', user_id=update.message.chat_id)
status = user["status"]
if(status == 'left'):
context.bot.send_message(chat_id=update.message.chat_id,
text="To use to bot you need to be a member of @MBNUpdates in order to stay updated with the latest developments.")
return
else:
status_page = "https://www.insta-stories.com/en/status"
req_status = requests.get(status_page).text
status = bs(req_status, "lxml")
if status.find("div", class_="status status--ok"):
fullmsg = update.message.text
if fullmsg == "/stories":
update.message.reply_text(
'/stories [instagram username]\nPlease read /help')
else:
msg = fullmsg.replace("/stories ", "")
if "@" in msg.lower():
query = msg.replace("@", "")
else:
query = msg
url = f"https://www.insta-stories.com/en/stories/{query}"
r = requests.get(url).text
soup = bs(r, "lxml")
if soup.find("div", class_="msg msg-user-not-found"):
update.message.reply_text(
"This username doesn't exist. Please try with another one.")
elif soup.find("div", class_="error"):
update.message.reply_text(
"API Error 🤒\nPlease try again later.")
else:
if soup.find("div", class_="msg msg-no-stories"):
update.message.reply_text(
"No stories available. Please try again later.")
else:
try:
profile = soup.find("div", class_="user-name").text
update.message.reply_text(
f"Downloading stories of {profile}")
videos = soup.findAll(class_='story-video')
photos = soup.findAll(class_='story-image')
for video in videos:
src = video.find("source")['src']
context.bot.send_video(
chat_id=update.message.chat_id, video=f"https://www.insta-stories.com{src}")
for photo in photos:
context.bot.send_photo(
chat_id=update.message.chat_id, photo=f"https://www.insta-stories.com{photo['src']}")
bot.send_message(
text="Thanks for using @xIGDLBot\nPlease /donate to keep this service alive!", chat_id=update.message.chat_id)
except:
context.bot.send_message(chat_id=update.message.chat_id,
text="Something went wrong. Please try again later.", parse_mode=telegram.ParseMode.HTML)
else:
update.message.reply_text(
"API is not working. Please try again later.")
# def igtv(update, context):
# user = context.bot.get_chat_member(
# chat_id='-1001225141087', user_id=update.message.chat_id)
# status = user["status"]
# if(status == 'left'):
# context.bot.send_message(chat_id=update.message.chat_id,
# text="To use to bot you need to be a member of @MBNUpdates in order to stay updated with the latest developments.")
# return
# else:
# fullmsg = update.message.text
# if fullmsg == "/igtv":
# update.message.reply_text(
# '/igtv [instagram username]\nPlease read /help')
# else:
# msg = fullmsg.replace("/igtv ", "")
# if "@" in msg.lower():
# query = msg.replace("@", "")
# else:
# query = msg
# L = Instaloader(dirname_pattern=query, download_comments=False,
# download_video_thumbnails=False, save_metadata=False, download_geotags=True, compress_json=True, post_metadata_txt_pattern=None, storyitem_metadata_txt_pattern=None)
# profile = Profile.from_username(L.context, query)
# igtv_count = profile.igtvcount
# posts = profile.get_igtv_posts()
# update.message.reply_text("Cooking your request 👨🍳\nProfile : " + query + "\nIGTV Video Count : " + str(
# igtv_count) + "\nThis may take longer, take a nap I can handle this without you.")
# try:
# L.posts_download_loop(posts, query)
# except Exception as e:
# context.bot.send_message(chat_id=update.message.chat_id, text="<b>ERROR</b>\n"+str(
# e), parse_mode=telegram.ParseMode.HTML)
# return
# src_dir = query
# for vidfile in glob.iglob(os.path.join(src_dir, "*.mp4")):
# context.bot.send_video(
# chat_id=update.message.chat_id, video=open(vidfile, 'rb'))
# bot.send_message(
# text="Thanks for using @xIGDLBot\nPlease /donate to keep this service alive!", chat_id=update.message.chat_id)
# try:
# shutil.rmtree(query)
# except Exception:
# pass
def feed(update, context):
bot.send_message(chat_id=update.message.chat_id,
text="This feature is still under development. Please use @MBNBetaBot if you like to beta test this feature.")
# user = context.bot.get_chat_member(chat_id='-1001225141087', user_id=update.message.chat_id)
# status = user["status"]
# if(status == 'left'):
# context.bot.send_message(chat_id=update.message.chat_id,text="To use to bot you need to be a member of @MBNUpdates in order to stay updated with the latest developments.")
# return
# else :
# fullmsg = update.message.text
# if fullmsg == "/feed":
# update.message.reply_text(
# '/feed [instagram username]\nPlease read /help')
# else:
# msg = fullmsg.replace("/feed ", "")
# if "@" in msg.lower():
# query = msg.replace("@", "")
# else:
# query = msg
# L = Instaloader(dirname_pattern=query, download_comments=False,
# download_video_thumbnails=False, save_metadata=False, download_geotags=True, compress_json=True, post_metadata_txt_pattern=None, storyitem_metadata_txt_pattern=None)
# profile = Profile.from_username(L.context, query)
# media = profile.mediacount
# update.message.reply_text("Cooking your request 👨🍳\nProfile : " + query + "\nMedia Count : " + str(media) +
# "\nThis may take longer, take a nap I can handle this without you.")
# posts = profile.get_posts()
# try:
# L.posts_download_loop(posts, query)
# except Exception as e:
# context.bot.send_message(chat_id=update.message.chat_id, text="<b>ERROR\n"+str(
# e), parse_mode=telegram.ParseMode.HTML)
# return
# update.message.reply_text("Download Completed.\n🗄 Archiving files...")
# zf = zipfile.ZipFile(f"{query}.zip", "w")
# for dirname, subdirs, files in os.walk(query):
# zf.write(query)
# for filename in files:
# zf.write(os.path.join(dirname, filename))
# zf.close()
# update.message.reply_text("Uploading to Telegram...")
# for zip_file in glob.glob("*.zip"):
# context.bot.send_document(chat_id=update.message.chat_id,
# document=open(zip_file, 'rb'))
# try:
# shutil.rmtree(query)
# os.remove(f"{query}.zip")
# except Exception:
# pass
def donate(update, context):
user = update.message.from_user
bot.send_message(chat_id=update.message.chat_id,
text=f"Hey {user.first_name}! \nThanks for showing interest in my works\nPlease contact @NandiyaLive for more info. You can send any amount you wish to donate me.")
def main():
updater = Updater(bot_token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start, run_async=True))
dp.add_handler(CommandHandler("help", help, run_async=True))
dp.add_handler(CommandHandler("stories", stories, run_async=True))
dp.add_handler(CommandHandler("about", about, run_async=True))
# dp.add_handler(CommandHandler("igtv", igtv, run_async=True))
dp.add_handler(CommandHandler("feed", feed, run_async=True))
dp.add_handler(CommandHandler("donate", donate, run_async=True))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()