print('remember to change USERNAME and PASSWORD')
        exit(1)
    # Save cookies so we won't have to log in again later
    reddit.save_cookies('cookies.txt')

print('logged in as %s' % reddit.user)

uinfo = reddit.user_info()
print('\nlink karma:    %d' % uinfo.link_karma)
print('comment karma: %d' % uinfo.comment_karma)

# Retrieve posts in a subreddit
posts = reddit.get('/r/%s' % MOD_SUB)
print('posts in subreddit /r/%s:' % MOD_SUB)
for post in posts:
    if (post.clicked is False):
        reddit.fetch_comments(post)
        for comment in post.comments:
            words = re.split("\?|\ |,|!|\n", comment.body)
            for word in words:
                if "http://www.tobaccoreviews.com" in word:
                    linkURL = word
                    if "/blend/" in word:
                        webtry = urllib2.urlopen(word)
                        html = webtry.read()
                        parser = MyHTMLParser()
                        parser.feed(html)
                        replyMessage = "[" + tobaccoName + "](" + word + ")" + "\n\n"
                        reddit.reply(comment, replyMessage)
        post.clicked = True
		exit(1)
	# Save cookies so we won't have to log in again later
	reddit.save_cookies('cookies.txt')

print('logged in as %s' % reddit.user)

uinfo = reddit.user_info()
print('\nlink karma:    %d' % uinfo.link_karma)
print('comment karma: %d' % uinfo.comment_karma)
	

# Retrieve posts in a subreddit
posts = reddit.get('/r/%s' % MOD_SUB)
print('posts in subreddit /r/%s:' % MOD_SUB)
for post in posts:
	if(post.clicked is False):
		reddit.fetch_comments(post)
		for comment in post.comments:
			words = re.split("\?|\ |,|!|\n",comment.body)
			for word in words:
				if "http://www.tobaccoreviews.com" in word:
					linkURL = word
					if "/blend/" in word:
						webtry = urllib2.urlopen(word)
						html = webtry.read()
						parser = MyHTMLParser()
						parser.feed(html)
						replyMessage = "["+tobaccoName+"]("+word+")"+"\n\n"
						reddit.reply(comment,replyMessage)
		post.clicked = True
Beispiel #3
0
	subreddit = {}

	#Get data for each post
	post_count = -1
	month = timedelta(days=31)
	curr_date = date.today()
	for i in range(len(posts)):
		print('Getting post %d of %d' % (i, len(posts)))
		#XML
		dn = ET.SubElement(srn, 'document')
		p = posts[i]
		time_since_post = curr_date - date.fromtimestamp(p.created)
		if time_since_post <= month:
			print('days before now: %s' % time_since_post.days)
			post_count += 1
			reddit.fetch_comments(p)
			time.sleep(2)
			#title info
			title = ET.SubElement(dn, 'title')
			title.text = p.title
			#link info
			url = ET.SubElement(dn, 'url')
			url.text = p.url
			#snippet, self and comment text
			snippet_str = ""
			if (p.is_self):
				snippet_str += p.selftext
			for c in range(len(p.comments)):
				snippet_str += " " + get_nested_comments(p.comments[c])
				p.comments[c] = p.comments[c].__dict__
				p.comments[c]['children'] = []