本文整理匯總了Python中reddit.Reddit.compose_message方法的典型用法代碼示例。如果您正苦於以下問題:Python Reddit.compose_message方法的具體用法?Python Reddit.compose_message怎麽用?Python Reddit.compose_message使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類reddit.Reddit
的用法示例。
在下文中一共展示了Reddit.compose_message方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from reddit import Reddit [as 別名]
# 或者: from reddit.Reddit import compose_message [as 別名]
class AnagramBot:
OUT_STDOUT = 1
OUT_MAINTAINER = 2
OUT_REPLY = 4
OUT_DEBUG_REPLY = 8
def __init__(self):
self._reddit = Reddit(user_agent='anagram_bot')
self._anagram = Wordplay()
self._maintainer = None
self._output = AnagramBot.OUT_STDOUT
def setMaintainer(self, username):
self._maintainer = username
def setOutput(self, outputMode):
self._output = outputMode
def login(self, username, password):
self._reddit.login(username, password)
def postPalindrome(self):
comments = list(self._fetchComments())
for comment in comments:
palindrome = self._anagram.pickRandomPalindrome(comment.body)
if palindrome != None:
print palindrome
else:
print "Nope:", comment.body[:70].replace("\n", "")
def makeFunny(self):
comments = list(self._fetchComments())
attempts = []
anagrams = []
maxAttempts = 20
i = 0
while len(attempts) < 10 and i < maxAttempts:
i += 1
comment = random.choice(comments)
anagrams = self._attempt(comment.body)
anagrams = sorted(anagrams, key=lambda x: -len(x[1]))
if len(anagrams) > 0:
attempts.append( (comment,anagrams) )
if len(attempts) == 0:
return
attempts = sorted(attempts, key=lambda x: -len(x[1][0][1]))
(comment, anagrams) = attempts[0]
anagrams = filter(lambda x: len(x[1]) > 3, anagrams)
reply = self._replace(comment.body, anagrams)
self._sendFunny(comment, reply)
def _sendFunny(self, comment, reply):
if self._output & AnagramBot.OUT_STDOUT:
self._printReply(comment, reply)
if self._output & AnagramBot.OUT_MAINTAINER:
self._debugPM(comment.permalink + "\n\n" + reply)
if self._output & AnagramBot.OUT_DEBUG_REPLY:
self._moderatedReply(comment, reply)
if self._output & AnagramBot.OUT_REPLY:
comment.reply( reply )
def _debugPM(self, message):
if self._maintainer == None:
raise ValueError("No maintainer is set! Use setMaintainer(str).")
self._reddit.compose_message(self._maintainer, "AnagramBot debug",
message)
def _printReply(self, comment, reply):
print comment.body
print "==================="
print reply
def _moderatedReply(self, comment, reply):
self._printReply(comment,reply)
print comment.permalink
response = raw_input("Send this [YES/NO]? ")
if response.strip() == "YES":
print "Sending reply..."
comment.reply(reply)
else:
print "Aborted."
def _replace(self, text, anagrams):
for anagram in anagrams:
pattern = "([^A-Za-z'0-9])" + anagram[0] + "([^A-Za-z'0-9])"
replace = "\\1" + anagram[1] + "\\2"
text = re.sub(pattern, replace, text)
return text
#.........這裏部分代碼省略.........