在fb状态上发布多行文本[Python] [Facebook API]

时间:2019-01-02 23:57:15

标签: python facebook random

我在学校放假时就开始了这个项目,以此来不断练习和增强我的python知识。简而言之,代码是一个Facebook机器人,它可以随机生成NBA球队,球员和职位,而这些球队在跑步时应该像这样。

Houston Rockets

[PG] Ryan Anderson
[PF] Michael Curry
[SF] Marcus Morris
[C] Bob Royer
[SF] Brian Heaney

在将我的代码发布到我的Facebook页面时,我目前遇到麻烦,该程序不会像这样发布1个团队和5个玩家/位置,而只会发布1个这样的单人游戏

Ryan Anderson

这是我的代码

import os
import random
import facebook

token = "...."
fb = facebook.GraphAPI(access_token = token)

parent_dir = "../NBAbot"
os.chdir(parent_dir)
file_name = "nba_players.txt"

def random_position():
    """Random Team from list"""
    position = ['[PG]','[SG]','[SF]','[PF]','[C]',]
    random.shuffle(position)
    position = position.pop()
    return(position)

def random_team():
    """Random Team from list"""
    Team = ['Los Angeles Lakers','Golden State Warriors','Toronto Raptors','Boston Celtics','Cleveland Cavaliers','Houston Rockets','San Antonio Spurs','New York Knicks','Chicago Bulls','Minnesota Timberwolves','Philadelphia 76ers','Miami Heat','Milwaukee','Portland Trail Blazers','Dallas Mavericks','Phoenix Suns','Denver Nuggets','Utah Jazz','Indiana Pacers','Los Angeles Clippers','Washington Wizards','Brooklyn Nets','New Orleans Pelicans','Sacramento Kings','Atlanta Hawks','Detroit Pistons','Memphis Grizzlies','Charlotte Hornets','Orlando Magic']
    random.shuffle(Team)
    Team = Team.pop()
    return(Team)

def random_player(datafile):
    read_mode = "r"
    with open (datafile, read_mode) as read_file:
        the_line = read_file.readlines()
        return(random.choice(the_line))

def main():
    return(
        random_team(),
        random_position(),
        random_player(file_name),
        random_position(),
        random_player(file_name),
        random_position(),
        random_player(file_name),
        random_position(),
        random_player(file_name),
        random_position(),
        random_player(file_name))

fb.put_object(parent_object="me", connection_name='feed', message=main())

感谢您的帮助。

0 个答案:

没有答案