-
Notifications
You must be signed in to change notification settings - Fork 0
/
ig.py
129 lines (120 loc) · 4.78 KB
/
ig.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
import time
import requests, json
class Trenstagram:
def __init__(self):
self.BASE_URL = 'https://www.instagram.com/'
self.USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0"
self.session = requests.Session()
self.session.headers = {'user-agent': self.USER_AGENT}
self.session.headers.update({'Referer': self.BASE_URL})
self.logged_in = False
def login(self, USERNAME, PASSWD, EMAIL):
self.email = EMAIL
self.username = USERNAME
try:
req = self.session.get(self.BASE_URL)
self.session.headers.update({'X-CSRFToken': req.cookies['csrftoken']})
str_time = str(int(time.time()))
PASSWORD = '#PWD_INSTAGRAM_BROWSER:0:' + str_time + ':' + PASSWD
login_data = {'username': USERNAME, 'enc_password': PASSWORD}
LOGIN_URL = self.BASE_URL + 'accounts/login/ajax/'
login = self.session.post(LOGIN_URL , data=login_data, allow_redirects=True)
self.session.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
if(login.json()['authenticated']):
self.logged_in = True
print("Logged in")
return True
else:
raise Exception()
except:
print("Error Logging in.")
return False
def verify(self):
if self.logged_in:
return True
print("Log in first.")
return False
def change_username(self, new_username):
if self.verify():
try:
data = {
'username': new_username,
'email': self.email
}
r = self.session.post(self.BASE_URL + "accounts/edit/", data=data)
if r.json()['status'] == "ok":
print(f'Username Changed to {new_username}')
self.username = new_username
return True
else:
raise Exception()
except:
print('Error Changing Username')
return False
def change_bio(self, new_bio):
if self.verify():
try:
data = {
'username': self.username,
'biography': new_bio,
'email': self.email
}
r = self.session.post(self.BASE_URL + "accounts/edit/", data=data)
if r.json()['status'] == "ok":
print(f'Bio Changed to {new_bio}')
return True
else:
raise Exception()
except:
print('Error Changing Bio')
return False
def change_name(self, new_name):
if self.verify():
try:
data = {
'username': self.username,
'first_name': new_name,
'email': self.email
}
r = self.session.post(self.BASE_URL + "accounts/edit/", data=data)
if r.json()['status'] == "ok":
print(f'Name Changed to {new_name}')
return True
else:
raise Exception()
except:
print('Error Changing Name')
return False
def change_website(self, new_website):
if self.verify():
try:
data = {
'username': self.username,
'external_url': new_website,
'email': self.email
}
r = self.session.post(self.BASE_URL + "accounts/edit/", data=data)
if r.json()['status'] == "ok":
print(f'Website Changed to {new_website}')
return True
else:
raise Exception()
except:
print('Error Changing Website')
return False
def change_profile_image(self, image_url):
if self.verify():
try:
data = {"Content-Disposition": "form-data", "name": "profile_pic", "filename":"profilepic.jpg","Content-Type": "image/jpeg"}
image_req = requests.get(image_url).content
pfp = bytes(image_req)
self.session.headers.update({'Content-Length' : str(len(pfp))})
r = self.session.post(self.BASE_URL + "accounts/web_change_profile_picture/", files={'profile_pic': pfp}, data=data)
if r.json()['changed_profile']:
print("Profile picture changed!")
return True
else:
raise Exception()
except:
print('Error Changing Profile Image')
return False