Reddit to Twitter bot与keydic字典调用有问题

2024-03-29 15:59:04 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在学习一个reddit到twitter的机器人教程,这个机器人是用python编写的,使用PRAW编写的,我遇到了一些错误。在

在命令控制台中运行此代码会在第74行中显示错误

import praw
import json
import requests
import tweepy
import time

access_token = 'secret'
access_token_secret = ' secret'
consumer_key = 'secret'
consumer_secret = 'secret'

def strip_title(title):
    if len(title) < 94:
        return title
    else:
        return title[:93] + "..."

def tweet_creator(subreddit_info):
    post_dict = {}
    post_ids = []
    print "[bot] Getting posts from Reddit"
    for submission in subreddit_info.get_hot(limit=20):
        post_dict[strip_title(submission.title)] = submission.url
        post_ids.append(submission.id)
    print "[bot] Generating short link using goo.gl"
    mini_post_dict = {}
    for post in post_dict:
        post_title = post
        post_link = post_dict[post]         
        short_link = shorten(post_link)
        mini_post_dict[post_title] = short_link 
    return mini_post_dict, post_ids

def setup_connection_reddit(subreddit):
    print "[bot] setting up connection with Reddit"
    r = praw.Reddit('yasoob_python reddit twitter bot '
                'monitoring %s' %(subreddit)) 
    subreddit = r.get_subreddit(subreddit)
    return subreddit

def shorten(url):
    headers = {'content-type': 'application/json'}
    payload = {"longUrl": url}
    url = "https://www.googleapis.com/urlshortener/v1/url"
    r = requests.post(url, data=json.dumps(payload), headers=headers)
    link = json.loads(r.text)
    return link

def duplicate_check(id):
    found = 0
    with open('posted_posts.txt', 'r') as file:
        for line in file:
            if id in line:
                found = 1
    return found

def add_id_to_file(id):
    with open('posted_posts.txt', 'a') as file:
        file.write(str(id) + "\n")

def main():
    subreddit = setup_connection_reddit('showerthoughts')
    post_dict, post_ids = tweet_creator(subreddit)
    tweeter(post_dict, post_ids)

def tweeter(post_dict, post_ids):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    for post, post_id in zip(post_dict, post_ids):
        found = duplicate_check(post_id)
        if found == 0:
            print "[bot] Posting this link on twitter"
            print post+" "+post_dict[post]+" #Python #reddit #bot"
            api.update_status(post+" "+post_dict[post]+" #Python #reddit #bot")
            add_id_to_file(post_id)
            time.sleep(30)
        else:
            print "[bot] Already posted" 

if __name__ == '__main__':
    main()

错误:

^{pr2}$

我对代码和错误的理解是,它需要一个字符串来发送,但不知何故得到了整个密钥字典集。我认为通过将[post]参数发送到post_dict,它将能够为bot获取特定的post进行utalize,但是,它正在获取字典!在

有两行,74和75都调用post_dict[post],并且在调用post key时不初始化字典的值。在


Tags: ididsurlsecretreturntitledefbot
1条回答
网友
1楼 · 发布于 2024-03-29 15:59:04

在调用tweeter函数For循环中的串联之前,请尝试打印post和{}。这将向您展示这些结构的外观,并使解决方案显而易见。在

相关问题 更多 >