Skip to content

Instantly share code, notes, and snippets.

@IvanTurgenev
Last active July 31, 2017 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanTurgenev/5f1bc5513ca76afdf6d594a6f0b4914c to your computer and use it in GitHub Desktop.
Save IvanTurgenev/5f1bc5513ca76afdf6d594a6f0b4914c to your computer and use it in GitHub Desktop.
Line 45 ERROR UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 16: ordinal not in range(128), has something to do with add
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 804, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 757, in run
self.__target(*self.__args, **self.__kwargs)
File "redditbot.py", line 48, in poststream # GIST HAS DIFFERENT NUMBER OF LINES
subredditra.contributor.add(author)
File "/home/kenmr/.local/lib/python2.7/site-packages/praw/models/reddit/subreddit.py", line 1362, in add
self.subreddit._reddit.post(url, data=data)
File "/home/kenmr/.local/lib/python2.7/site-packages/praw/reddit.py", line 432, in post
return self._objector.objectify(data)
File "/home/kenmr/.local/lib/python2.7/site-packages/praw/objector.py", line 122, in objectify
raise APIException(*errors[0])
File "/home/kenmr/.local/lib/python2.7/site-packages/praw/exceptions.py", line 25, in __init__
error_str = '{}: \'{}\''.format(error_type, message)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 16: ordinal not in range(128)
import praw
import re
import collections
import threading
reddit = praw.Reddit(client_id='XXXXX', client_secret="XXX",
password='X', user_agent='BOT',
username='X')
subredditra = reddit.subreddit("subreddit to get contributors list with mod permission")
contlist = []
for contributor in subredditra.contributor(limit=None):
contlist.append(contributor.name)
wanted = ["wanted","words","in","comments","to","add","to","contributors"]
def commstream(subreddit):
sub = reddit.subreddit(subreddit)
for comment in sub.stream.comments():
text = comment.body # Fetch body
author = comment.author
# print author
cnt = collections.Counter()
words = re.findall('\w+', text.lower())
for word in words:
if word in wanted:
cnt[word] += 1
cntsum = (sum(cnt.values()))
if (not author in contlist) and (len (cnt) >= 2) :#and ((len cnt) >= 2):
print author
print text
print cntsum
print cnt
subredditra.contributor.add(author)
contlist.append(author)
def poststream(subreddit):
sub = reddit.subreddit(subreddit)
for submission in sub.stream.submissions():
author = submission.author
title = submission.title
if (not author in contlist):
print title
print author
subredditra.contributor.add(author) #ERROR
contlist.append(author)
# qcp = Queue.Queue()
# qpc = Queue.Queue()
thread1 = threading.Thread(target=commstream,args=["subreddit to stream comments"])
thread2 = threading.Thread(target=poststream,args=["subreddit to stream post"])
thread1.start()
thread2.start()
# while True:
# try:
# commstream()
# except praw.errors.HTTPException as error:
# if error._raw.status_code == 503:
# time.sleep(30)
# print("503 ERROR")
# continue
# else:
# raise SystemExit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment