-
Notifications
You must be signed in to change notification settings - Fork 3
/
CrossXParellelBot.py
127 lines (94 loc) · 4.1 KB
/
CrossXParellelBot.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
import authentication as auth
from imgurpython import ImgurClient
from prettySleep import pretty_sleep
import praw
import urllib2, re
#import Image
from PIL import Image
import os,sys
import image_slicer
import json
import time
import string
POSTED_HASH = "/home/nickfrosst/crossXparallel/CrossXParallelBot/postedHash.txt"
AGENT_NAME = "CrossXParallel"
TEMP_NAME = "temp.jpg"
TARGET_SUB = "parallelview"
#TARGET_SUB = "reddit_api_test"
SOURCE_SUB = "crossview"
SEARCH_LIMIT = 20
POST_LIMIT = 5
ImgurConfig = {
'album': "OWRXY",
'name': None,
'title': None,
'description': None
}
black_listed_authors = ['chrisleblanc79']
def flip(image):
tiles = image_slicer.slice(image, 2, save=False)
new_im = Image.new('RGB', (tiles[0].image.size[0]*2,tiles[0].image.size[1]))
new_im.paste(tiles[1].image, (0,0))
new_im.paste(tiles[0].image, (tiles[0].image.size[0],0))
new_im.save(TEMP_NAME,"jpeg")
def flickr_download(url, save_name):
html = urllib2.urlopen(url).read()
img_url = re.findall(r'http[s]?\:[^" \\:]*_b\.jpg', html)[0]
with open(save_name, "wb") as fp:
fp.write(urllib2.urlopen(img_url).read())
def raw_download(url, save_name):
with open(save_name, "wb") as fp:
fp.write(urllib2.urlopen(url).read())
posted = json.load(open(POSTED_HASH))
downloaders = [(flickr_download,'.*www\.flickr\.com.*'),
(raw_download,'.*\.jpg'),
(raw_download,'.*\.png')]
imgur_client = ImgurClient(auth.client_id, auth.client_secret, auth.access_token, auth.refresh_token)
reddit = praw.Reddit(client_id=auth.reddit_client_id,
client_secret=auth.reddit_secret,
user_agent=AGENT_NAME,
username = auth.reddit_username,
password = auth.reddit_password)
if not reddit.read_only:
print("obtained non read only reddit")
subreddit = reddit.subreddit(SOURCE_SUB)
submitted = 0
for submission in subreddit.hot(limit = SEARCH_LIMIT):
if submitted == POST_LIMIT:
break
for downloader,patern in downloaders:
s = str(submission.title.encode("utf-8"))
if submission.author.name not in black_listed_authors and \
not posted.has_key(submission.url) and \
s.lower().find("r/parallelview") == -1 and \
re.match(patern,submission.url):
print ("matched " + patern)
downloader(submission.url, TEMP_NAME)
flip(TEMP_NAME)
ImgurConfig['title'] = filter(lambda x: x in string.printable, s)
print (ImgurConfig['title'])
ImgurConfig['description'] = "parallel view version - original by " + str(submission.author.name) + " - " + str(submission.shortlink) + " - " + str(submission.url)
response = imgur_client.upload_from_path(TEMP_NAME, config=ImgurConfig, anon=False)
while True:
try:
print(str(response['link']))
print ("posting")
title = s + " [by u/" + str(submission.author.name) + " converted]"
reddit_post = reddit.subreddit(TARGET_SUB).submit( title, url=str(response['link']))
break
except praw.exceptions.APIException as e:
if e.field == 'ratelimit':
print(e.message)
m = re.search("\d+",e.message)
pretty_sleep(60 * int(m.group(0)))
else:
print(e)
raise NameError('GiveUp')
reddit_post.reply('''Hello, I am a bot :) \nI was writtten by nick_ok\n I cross post from r/crossview \n\n ''' + ImgurConfig['description'])
print ("adding comment")
posted[submission.url] = reddit_post.shortlink
print("submited " + posted[submission.url])
submitted += 1
#write hash to disc
json.dump(posted, open(POSTED_HASH,'w'))
break