-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.py
82 lines (64 loc) · 2.67 KB
/
init.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
from time import sleep
import tweepy
import praw
from config import config
from keys import keys
copyFrom = config['twitter_user']
postTo = config['reddit_sub']
mentionRT = config['mention_rt']
mentionMI = config['mention_mi']
mentionIm = config['mention_im']
# REST API connection
reddit = praw.Reddit(client_id=keys['reddit_client_id'],
client_secret=keys['reddit_client_secret'],
user_agent=keys['reddit_user_agent'],
username=config['reddit_username'],
password=config['reddit_password'])
# Twitter Api Connection
auth = tweepy.OAuthHandler(keys['twitter_consumer_key'], keys['twitter_consumer_secret'])
auth.set_access_token(keys['twitter_access_token'], keys['twitter_access_token_secret'])
api = tweepy.API(auth, wait_on_rate_limit=True)
def get_last_tweet(self):
tweet = self.user_timeline(copyFrom, count=1, tweet_mode="extended", include_entities=True)[0]
return tweet
lastTweet = 0
while True:
newTweet = get_last_tweet(api)
if newTweet.id != lastTweet:
lastTweet = newTweet.id
if hasattr(newTweet, 'retweeted_status'):
newTweet = newTweet.retweeted_status
reTweet = True
else:
reTweet = False
fullTweetText = newTweet.full_text
mediaUrl = []
if 'media' in newTweet.entities:
for media in newTweet.extended_entities['media']:
mediaUrl.append(media['media_url'])
try:
flair = ""
if len(mediaUrl) == 1: # Just one media
post = reddit.subreddit(postTo).submit(title=fullTweetText, url=mediaUrl)
flair += mentionIm + " | "
elif len(mediaUrl) > 1: # More than one media file
postText = ""
for item in mediaUrl:
postText += item + "\n\n"
post = reddit.subreddit(postTo).submit(title=fullTweetText + mentionMI, selftext=postText)
flair += mentionMI + " | "
else: # No media just the title
post = reddit.subreddit(postTo).submit(title=fullTweetText, selftext="")
if reTweet:
flair += mentionRT
if len(flair) != 0:
sleep(5)
if str(post.link_flair_text) != "None":
post.mod.flair(str(post.link_flair_text) + " | " + str(flair))
else:
post.mod.flair(str(flair))
except Exception as e:
print("Error, please copy this and open a issue on the git page with this info:")
print("Error Code: " + str(e))
print("Tweet: " + fullTweetText)
sleep(11 * 60)