-
Notifications
You must be signed in to change notification settings - Fork 8
/
functions.py
88 lines (68 loc) · 2.49 KB
/
functions.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
from variables import *
import instaloader
from instaloader import *
import re
import requests
import traceback # to print error traceback
import telebot
# classes (for rate limiting - might delete them)
class MyCustomException(Exception):
def __init__(self, message="Custom rate limit exceeded"):
self.message = message
super().__init__(self.message)
class MyRateController(instaloader.RateController):
def sleep(self, secs):
raise MyCustomException("Custom rate limit exceeded")
# functions
def log(log_message):
if not log_channel_id:
return # set to False log channel means it is not needed
log_bot_url = "https://api.telegram.org/bot" + bot_token + "/"
log = requests.post(log_bot_url + "sendMessage", data={
"chat_id": log_channel_id,
"text": log_message
})
# Check if the log was sent successfully
if log.status_code == 200:
print('log registered')
else:
print('Error in registering log:', log.status_code)
def try_to_delete_message(chat_id, message_id):
# initialize bot
bot = telebot.TeleBot(bot_token)
try:
bot.delete_message(chat_id, message_id)
except:
pass # ignore errors if user has already deleted the message
def get_ready_to_work_insta_instance():
# # (for rate limiting - might delete it)
# L = instaloader.Instaloader(rate_controller=lambda ctx: MyRateController(ctx))
# create instance
L = instaloader.Instaloader()
# prepare session
try:
L.load_session_from_file(USER, session_file_name) # gives error if file doesn't exist
print(L.test_login()) # gives error if session is expired
print("using old session")
except:
log("session failed")
print("session failed")
#### commented for debug
# if os.path.exists(session_file_name):
# os.remove(session_file_name) # delete older session if it file exists
# L.login(USER, PASS)
# log("new login with instagram bot")
# L.save_session_to_file(session_file_name)
# print("save to a new session")
####
return L
def download_post_to_folder(post_shortcode, folder):
L = get_ready_to_work_insta_instance()
post = Post.from_shortcode(L.context, post_shortcode)
L.download_post(post, target=folder)
def get_post_or_reel_shortcode_from_link(link):
match = re.search(insta_post_or_reel_reg, link)
if match:
return match.group(2)
else:
return False