forked from prashantsengar/RedIns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredins.py
153 lines (120 loc) · 4.04 KB
/
redins.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
import json
import os as __os
import requests as __requests
from InstagramAPI import InstagramAPI as __ig
import random as __random
import time as __time
import twitter
from JSONs import *
from caps import *
import moviepy
user = "" # Enter Instagram username
passw = "" # Add Instagram password
consumer_key = "" # Add twitter consumer key
consumer_secret = "" # Add twitter consumer secret key
access_token_key = "" # Add twitter
access_token_secret = "" # Add twitter
img = []
curr_dir = __os.path.dirname(__file__)
def check_folder():
try:
if not __os.path.exists(__os.path.join(curr_dir, 'red_media')):
__os.mkdir(__os.path.join(curr_dir, 'red_media'))
return True
return True
except Exception:
return False
def get_links(__JSON):
global img
file = open(__os.path.join(curr_dir, __JSON))
meme = json.loads(file.read())
n = meme['data']['dist']
for i in range(n):
img.append(meme['data']['children'][i]['data']['preview']['images'][0]['source']['url'])
print("I got links")
file.close()
def write_meme():
global img
memes = {'data': '[]'}
memes['data'] = img
for i in JSONs:
file = open(f'{i}.txt', 'w+')
data = json.dumps(memes)
file.write(data)
file.close()
print("Wrote json")
def dload(JSON):
get_links(JSON)
write_meme()
file = open('meme.txt')
data = file.read()
data = json.loads(data)
links = data['data']
if len(links) == 0:
print("No links")
else:
if check_folder():
__os.chdir(__os.path.join(curr_dir, 'red_media'))
i = 0
print(__os.getcwd())
for link in links:
try:
print(link)
link = link.replace('amp;', '')
f = __requests.get(link)
m_file = open(__os.path.join(__os.path.dirname(
__file__), 'red_media', f'{i}.jpg'), 'wb')
for chunk in f.iter_content(100000):
m_file.write(chunk)
m_file.close()
print("Downloaded")
__os.chdir('..')
i += 1
except Exception as e:
print(e)
else:
raise Exception('There has been an error')
def uload(num):
i = __ig(user, passw)
i.login()
a = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret)
# __os.chdir('\\red_media')
dirs = __os.listdir(__os.path.join(curr_dir, 'red_media'))
for j in range(num):
try:
files = __random.choice(dirs)
files = f'{__os.getcwd()}\\red_media\\' + files
print("1:post to instagram 2:post to twitter 3:post on both sites")
import get_input
user_choice=get_input.input("Enter index where to post images",'int')
if(user_choice==1):
i.uploadPhoto(files, caption=__random.choice(cap))
print("insta upload")
elif(user_choice==2):
a.PostUpdate(__random.choice(caps), files)
print("Uploaded..")
elif(user_choice==3):
i.uploadPhoto(files, caption=__random.choice(cap))
print("insta upload")
a.PostUpdate(__random.choice(caps), files)
print("Uploaded..")
else:
print("Wrong input")
__os.remove(files)
__time.sleep(10)
except Exception as e:
print("Error occured {}" .format(str(e)))
i.logout()
print("Logged out")
if __name__ == '__main__':
if check_folder():
__os.chdir(__os.path.join(curr_dir, 'red_media'))
for j in JSONs:
get_links(j)
write_meme()
dload(j)
uload(input("Enter the number of files to be uploaded: "))
else:
print("Error has occured in creating file")