aheskandani commited on
Commit
adbc58c
1 Parent(s): fe88846

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -146
app.py CHANGED
@@ -1,149 +1,14 @@
1
- from telegram.ext.filters import Filters
2
- from telegram.ext.messagehandler import MessageHandler
3
- from telegram import Update, ParseMode
4
- from telegram.ext import (Updater,
5
- PicklePersistence,
6
- CommandHandler,
7
- CallbackQueryHandler,
8
- CallbackContext,
9
- ConversationHandler)
10
- from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup
11
- from instaloader import Instaloader, Profile
12
 
13
- EXPECT_STATUS, EXPECT_USERNAME, EXPECT_PASSWORD, EXPECT_SAVE, EXPECT_BACK, EXPECT_BUTTON_CLICK = range(6)
 
 
 
14
 
15
- bot_token = '5523077604:AAGJuWt-VYjD-kwwS3EuHQFWvcT3LySEgd4'
16
- admin = 37087739
 
 
17
 
18
- # Bot commands
19
- def start(update: Update, context: CallbackContext):
20
- chat_id = update.effective_chat.id
21
- print(chat_id, admin)
22
- text = 'Hello! '
23
- if chat_id == admin:
24
- update.message.reply_text(text + '(Admin)', reply_markup=main_keyboard)
25
- else:
26
- update.message.reply_text(text)
27
-
28
- def set_info_handler(update: Update, context: CallbackContext):
29
- ''' Entry point of conversation this gives buttons to user'''
30
- text = (f'Status: {data["is_login"]}\n'
31
- f'Username: {data["username"]}\n'
32
- f'Password: {data["password"]}\n\n'
33
- 'Do you want to edit?')
34
- button = [ [InlineKeyboardButton('Login Status', callback_data='status')],
35
- [InlineKeyboardButton('Username', callback_data='username'),
36
- InlineKeyboardButton('Password', callback_data='password')],
37
- [InlineKeyboardButton('Cancel', callback_data='cancel')],
38
- ]
39
- inline_keyboard = InlineKeyboardMarkup(button)
40
- update.message.reply_text(text, reply_markup=inline_keyboard)
41
- return EXPECT_BUTTON_CLICK
42
-
43
- def button_click_handler(update: Update, context: CallbackContext):
44
- ''' This gets executed on button click '''
45
- chat_id = update.effective_chat.id
46
- query = update.callback_query
47
- query.answer()
48
- query.delete_message()
49
- if query.data == 'status':
50
- context.bot.send_message(chat_id, text='Change login status? Yes/No', reply_markup=ForceReply())
51
- return EXPECT_STATUS
52
- elif query.data == 'username':
53
- context.bot.send_message(chat_id, text='Enter instagram username', reply_markup=ForceReply())
54
- return EXPECT_USERNAME
55
- elif query.data == 'password':
56
- context.bot.send_message(chat_id, text='Enter instagram password', reply_markup=ForceReply())
57
- return EXPECT_PASSWORD
58
- elif query.data == 'cancel':
59
- query.answer('Opeartion canceled!')
60
- return ConversationHandler.END
61
-
62
- def set_status(update: Update, context: CallbackContext):
63
- ''' The user's reply to the name prompt comes here '''
64
- if update.message.text.lower() in ['no', 'false', 'n', '0']:
65
- data['is_login'] = 'No'
66
- else:
67
- data['is_login'] = 'Yes'
68
- save_login(data)
69
- update.message.reply_text(f'New status: {data["is_login"]}', reply_markup=main_keyboard)
70
- return ConversationHandler.END
71
-
72
-
73
- def set_username(update: Update, context: CallbackContext):
74
- ''' The user's reply to the name prompt comes here '''
75
- data['username'] = update.message.text.lower()
76
- save_login(data)
77
- update.message.reply_text(f'New username: {data["username"]}', reply_markup=main_keyboard)
78
- return ConversationHandler.END
79
-
80
-
81
- def set_password(update: Update, context: CallbackContext):
82
- ''' The user's reply to the name prompt comes here '''
83
- data['password'] = update.message.text
84
- save_login(data)
85
- update.message.reply_text(f'New password: {data["password"]}', reply_markup=main_keyboard)
86
- return ConversationHandler.END
87
-
88
-
89
- def cancel(update: Update, context: CallbackContext):
90
- return ConversationHandler.END
91
-
92
-
93
- def download_pic(update: Update, context: CallbackContext):
94
- insta = Instaloader()
95
- update.message.text = text
96
- if data['is_login'] == 'Yes':
97
- try:
98
- insta.load_session_from_file(data['username'])
99
- has_session = True
100
- update.message.reply_text('Session loaded!')
101
- except:
102
- has_session = False
103
-
104
- if not has_session:
105
- update.message.reply_text('No session!')
106
- try:
107
- insta.login(data['username'], data['password'])
108
- insta.save_session_to_file()
109
- except:
110
- update.message.reply_text('Login faild!')
111
- try:
112
- username = url_to_username(text)
113
- profile = Profile.from_username(insta.context, username)
114
- url = profile.get_profile_pic_url(text)
115
- cap = profile.full_name + '\n\n' \
116
- + profile.biography + '\n\n' \
117
- + '@instaprofilesave_bot'
118
- update.message.reply_photo(photo=url, caption=cap)
119
- except:
120
- update.message.reply_text('Under maintenance!')
121
-
122
-
123
- def save_login(data):
124
- with open('log.txt', 'w') as file:
125
- file.write('\n'.join(list(data.values())))
126
-
127
- if __name__ == '__main__':
128
- with open('log.txt', 'r') as file:
129
- data = dict(zip(['is_login', 'username', 'password'], file.read().splitlines()))
130
-
131
- main_keyboard = ReplyKeyboardMarkup([['Edit login info']], one_time_keyboard=True, resize_keyboard=True)
132
- updater = Updater(token=bot_token)
133
- updater.dispatcher.add_handler(CommandHandler('start', start))
134
- updater.dispatcher.add_handler(ConversationHandler(
135
- entry_points=[MessageHandler(Filters.regex('Edit login info'), set_info_handler)],
136
- states={
137
- EXPECT_STATUS: [MessageHandler(Filters.text, set_status)],
138
- EXPECT_USERNAME: [MessageHandler(Filters.text, set_username)],
139
- EXPECT_PASSWORD: [MessageHandler(Filters.text, set_password)],
140
- EXPECT_BUTTON_CLICK: [CallbackQueryHandler(button_click_handler)],
141
- EXPECT_BACK : [CallbackQueryHandler('back', start)]
142
- },
143
- fallbacks=[MessageHandler(Filters.regex('cancel'), cancel)],
144
- ))
145
- updater.dispatcher.add_handler(MessageHandler(Filters.text, download_pic))
146
- updater.dispatcher.add_handler(MessageHandler(Filters.command, download_pic))
147
- updater.dispatcher.add_handler(MessageHandler(Filters.text, download_pic))
148
- updater.start_polling()
149
- print('Bot started ..........')
 
1
+ import urllib.request
2
+ import os
 
 
 
 
 
 
 
 
 
3
 
4
+ url = 'https://math.berkeley.edu/~gmelvin/math113su14/math113su14notes2.pdf'
5
+ dir = ''
6
+ filename = url.split('/')[-1]
7
+ response = urllib.request.urlopen(urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'}))
8
 
9
+ if response.status == 200:
10
+ print(f'{float(response.headers["Content-Length"]) / 2**20:0.3f} MB')
11
+ urllib.request.urlretrieve(url, dir + filename)
12
+ print('Downloaded!')
13
 
14
+ os.remove(dir + filename)