-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmemepage.py
163 lines (132 loc) · 4.54 KB
/
memepage.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
'''
###################################
# INSTAGRAM MEME PAGE AUTOMATOR #
# BY Divdude77 #
###################################
'''
#############################################
# READ readme.md BEFORE RUNNING THIS SCRIPT #
#############################################
import os
import shutil
import praw
import urllib.request
import csv
from instabot import Bot
from PIL import Image
from datetime import date
from time import sleep
subreddits = ["memes", "dankmemes"]
subreddit = subreddits[0]
redditor = ""
file_type = ""
posted = 0
post_limit = 2
clientid = "ENTER YOUR CLIENT ID HERE" # Enter your client ID here
clientsecret = "ENTER YOUR CLIENT SECRET HERE" # Enter your client secret here
username = "ENTER YOUR USERNAME HERE" # Enter your username here
password = "ENTER YOUR PASSWORD HERE" # Eter your password here
memeskip = 1
post_cooldown = 10800
# Deleting Config Folder (Folder causes error if not deleted)
try:
shutil.rmtree("config")
except OSError as error:
print()
# Creating a Reddit Instance
reddit = praw.Reddit(
client_id=clientid,
client_secret=clientsecret,
user_agent="memeboi"
)
# Initializing the Instabot
bot = Bot()
bot.login(username=username, password=password) #Logging in to insta account
# Writing Function to Count Difference of Days Between Today and Given Date
def daysdiff(day):
d1 = [int(i) for i in day.split('-')]
d2 = [int(i) for i in str(date.today()).split('-')]
if d1[:2] == d2[:2]:
return d2[2] - d1[2]
elif d1[0] == d2[0]:
if d1[1]<d2[1]:
return ((30-d1[2]) + d2[2] + 30 * (d2[1]-d1[1]-1))
else:
return 365 # No need to check if years are different
while True:
# Getting Submitted Memes List
f = open("posted.csv","r")
submitted_memes = []
for i in csv.reader(f):
if i:
submitted_memes.append(i[0])
f.close()
# Getting Meme from Reddit
n = 0
for submission in reddit.subreddit(subreddit).hot(limit=100):
if n >= memeskip:
meme_url = submission.url
meme_id = submission.id
file_type = meme_url[-1:-4:-1][-1:0:-1] + "g"
if file_type == "jpg":
if submission.id not in submitted_memes:
urllib.request.urlretrieve(meme_url, "meme.jpg") # Download the meme (jpg)
redditor = submission.author.name
redditor = "u/" + redditor
break
else:
continue
elif file_type == "png":
if submission.id not in submitted_memes:
urllib.request.urlretrieve(meme_url, "meme.png") # Download the meme (png)
mem = Image.open("meme.png")
memjpg = mem.convert("RGB") # Convert it to jpg
memjpg.save("meme.jpg")
os.remove("meme.png")
redditor = submission.author.name
redditor = "u/" + redditor
break
else:
continue
else:
continue
n += 1
# Save Posted id Onto File
f = open("posted.csv","a")
csv.writer(f).writerow([submission.id,date.today()])
f.close()
# Create Post Caption
caption = submission.title + "\n \nPosted in r/" + subreddit + " by " + redditor + "\n \n#meme #memes #funny #dankmemes #memesdaily #funnymemes #lol #follow #humor #like #dank #love #instagram #memepage #dankmeme #tiktok #comedy #lmao #fun #anime #ol #dailymemes #edgymemes #offensivememes #memestagram #bhfyp #instagood #funnymeme #memer #bhfyp"
# Post Meme on Insta
try:
bot.upload_photo("meme.jpg", caption=caption)
except OSError as error:
pass
# Delete Posted Meme
try:
os.remove("meme.jpg.REMOVE_ME")
posted += 1
except OSError as error:
os.remove("meme.jpg")
# Delete old Saved ids
f = open("posted.csv","r")
currrentdata = []
for i in csv.reader(f):
if daysdiff(i[1]) >= 30:
continue
else:
currrentdata.append(i)
f.close()
f = open("posted.csv","w")
csv.writer(f).writerows(currrentdata)
f.close()
# Wait for an Hour
if posted == post_limit:
print("Post cooldown...")
sleep(post_cooldown)
posted = 0
# Switch the Subreddit
if subreddits.index(subreddit) == len(subreddits) - 1:
subreddit = subreddits[0]
else:
subreddit = subreddits[subreddits.index(subreddit)+1]