-
Notifications
You must be signed in to change notification settings - Fork 0
/
redditAPI.py
194 lines (175 loc) · 8.92 KB
/
redditAPI.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import praw, random, requests, shutil, os, time, tweepy, funcs
from variables import REDDIT_CLIENT_ID, REDDIT_SECRET, REDDIT_ACC_PASS, REDDIT_ACC_USER, REDDIT_USER_AGENT, Subreddits_list, PostsToAvoid
reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_SECRET,
username=REDDIT_ACC_USER,
password=REDDIT_ACC_PASS,
user_agent=REDDIT_USER_AGENT)
def Choose_subreddit(type): #Elige algun subreddit al azar
Z = funcs.Scheduling(type)
Chosen_sub= Z.get_NextSub()
return Chosen_sub
def get_top(chosen_sub, LIMITE, Tweets):
for post in reddit.subreddit(chosen_sub).top(time_filter= 'all', limit=LIMITE):
if(funcs.isOriginal(post.title, Tweets) and funcs.CheckInPosts(funcs.process_title(post.title))):
print("Se procesa: " + post.title)
return post
return get_top(chosen_sub, LIMITE + 30, Tweets)
def Top_post(api):
print("Se empieza la función de redditAPI.py: Top_post(api)")
chosen_sub = Choose_subreddit("TOP_last_sub.txt")
TIMELINE_ACTUAL= tweepy.Cursor(api.user_timeline, screen_name=api.me().screen_name, tweet_mode="extended").items()
Tweets = []
for x in TIMELINE_ACTUAL:
Tweets.append(x._json["full_text"])
submission = get_top(chosen_sub, 30, Tweets)
Estado = '"' + funcs.process_title(submission.title) + '"'
if(submission.selftext == "" and ("jpg" in submission.url or "jpeg" in submission.url or "png" in submission.url)):
#Si la publicación con una foto
res = requests.get(submission.url, stream=True)
img_path = ""
if("jpeg" in submission.url):
img_path = "redditTOP.jpeg"
elif("jpg" in submission.url):
img_path = "redditTOP.jpg"
elif("png" in submission.url):
img_path = "redditTOP.png"
with open(img_path, "wb") as out_file:
shutil.copyfileobj(res.raw, out_file)
del res
try:
if(funcs.Redimensionar(img_path)):
try:
api.update_with_media(filename= img_path, status= Estado + " #Astronomy #Space") #Subir tweet con foto
Z = funcs.Scheduling("TOP_last_sub.txt")
Z.set_NewSub(chosen_sub)
print("Se publico el tweet del subreddit: " + chosen_sub)
except tweepy.error.TweepError as e:
if("'code': 186" in str(e)):
PostsToAvoid.append(funcs.process_title(submission.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(submission.title) + ". Se ejecutara de nuevo la función.")
Top_post(api)
else:
print(str(e))
else:
PostsToAvoid.append(funcs.process_title(submission.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(submission.title) + ". Se ejecutara de nuevo la función.")
Top_post(api)
except:
try:
api.update_status(status= Estado + " " + submission.url + " #Astronomy #Space") #Subir tweet
Z = funcs.Scheduling("TOP_last_sub.txt")
Z.set_NewSub(chosen_sub)
print("Se publico el tweet del subreddit: " + chosen_sub)
except tweepy.error.TweepError as e:
if("'code': 186" in str(e)):
PostsToAvoid.append(funcs.process_title(submission.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(submission.title) + ". Se ejecutara de nuevo la función.")
Top_post(api)
else:
print(str(e))
try:
os.remove(img_path)
except:
pass
else:
#Subir tweet con el link de lo adjuntado
try:
api.update_status(status= Estado + " " + submission.url + " #Astronomy #Space")
Z = funcs.Scheduling("TOP_last_sub.txt")
Z.set_NewSub(chosen_sub)
print("Se publico el tweet del subreddit: " + chosen_sub)
except tweepy.error.TweepError as e:
if("'code': 186" in str(e)):
PostsToAvoid.append(funcs.process_title(submission.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(submission.title) + ". Se ejecutara de nuevo la función.")
Top_post(api)
else:
print(str(e))
print("Se termina la funcion de redditAPI.py: Top_post(api)\n")
return None
def get_new(chosen_sub, LIMITE, Tweets):
#Consigue el mejor post de los más recientes
submissions_list = []
for submission in reddit.subreddit(chosen_sub).new(limit= LIMITE):
submissions_list.append(submission)
submissions_list.sort(key= lambda x: x.ups, reverse= True)
for post in submissions_list:
if(funcs.isOriginal(post.title, Tweets) and funcs.CheckInPosts(funcs.process_title(post.title))):
print("Se procesa: " + post.title)
return post
return get_new(chosen_sub, LIMITE + 5, Tweets)
def New_post(api):
print("Se empieza la función de redditAPI.py: New_post(api)")
chosen_sub = Choose_subreddit("BEST5_last_sub.txt")
TIMELINE_ACTUAL= tweepy.Cursor(api.user_timeline, screen_name=api.me().screen_name, tweet_mode="extended").items()
Tweets = []
for x in TIMELINE_ACTUAL:
Tweets.append(x._json["full_text"])
best_one = get_new(chosen_sub, 5, Tweets)
Estado= '"' + funcs.process_title(best_one.title) + '"'
if(best_one.selftext == "" and ("jpg" in best_one.url or "jpeg" in best_one.url or "png" in best_one.url)):
#Si la publicación tiene una foto
res = requests.get(best_one.url, stream=True)
img_path = ""
if("jpeg" in best_one.url):
img_path = "redditNEW.jpeg"
elif("jpg" in best_one.url):
img_path = "redditNEW.jpg"
elif("png" in best_one.url):
img_path = "redditNEW.png"
with open(img_path, "wb") as out_file:
shutil.copyfileobj(res.raw, out_file)
del res
#Manejo de errores para tweetear. El nombre del post tiene que estar en el tweet, sino va a publicar doble cuando se compruebe si es original o no.
try:
if(funcs.Redimensionar(img_path)):
try:
api.update_with_media(filename= img_path, status= Estado + " #Astronomy #Space")
Z = funcs.Scheduling("BEST5_last_sub.txt")
Z.set_NewSub(chosen_sub)
print("Se publico el tweet del subreddit: " + chosen_sub)
except tweepy.error.TweepError as e:
if("'code': 186" in str(e)):
PostsToAvoid.append(funcs.process_title(best_one.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(best_one.title) + ". Se ejecutara de nuevo la función.")
New_post(api)
else:
print(str(e))
else:
PostsToAvoid.append(funcs.process_title(best_one.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(best_one.title) + ". Se ejecutara de nuevo la función.")
New_post(api)
except:
try:
api.update_status(status= Estado + " " + best_one.url + " #Astronomy #Space")
Z = funcs.Scheduling("BEST5_last_sub.txt")
Z.set_NewSub(chosen_sub)
print("Se publico el tweet del subreddit: " + chosen_sub)
except tweepy.error.TweepError as e:
if("'code': 186" in str(e)):
PostsToAvoid.append(funcs.process_title(best_one.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(best_one.title) + ". Se ejecutara de nuevo la función.")
New_post(api)
else:
print(str(e))
try:
os.remove(img_path)
except:
pass
else:
#Subir tweet con el link de lo adjuntado
try:
api.update_status(status= Estado + " " + best_one.url + " #Astronomy #Space")
Z = funcs.Scheduling("BEST5_last_sub.txt")
Z.set_NewSub(chosen_sub)
print("Se publico el tweet del subreddit: " + chosen_sub)
except tweepy.error.TweepError as e:
if("'code': 186" in str(e)):
PostsToAvoid.append(funcs.process_title(best_one.title))
print("Se agrego a los posts para no publicar: " + funcs.process_title(best_one.title) + ". Se ejecutara de nuevo la función.")
New_post(api)
else:
print(str(e))
print("Se termina la funcion de redditAPI.py: New_post(api)\n")
return None