python - 如何从 Heroku 应用程序下载最新更改的源代码和文件?
问题描述
我的问题:
我有一个使用 python的heroku应用程序,它基本上只是一个twitter 机器人,人们在其中提到帐户和机器人自动回复。我已经使用Heroku CLI部署了它。现在为了确保我不会多次处理同一个查询,我将每个查询保存在一个json
文件中,然后每周将其重置为一个空字典。
with open("weekly_tweets.json", "r") as f:
tweets_dict = json.load(f)
if matched_name in tweets_dict: ## already processed query for player that week
api.update_status("blah blah") ##do something
else: ##new query
tweet = api.update_with_media("other blah blah") ##do something else
## Here's where I update the json file for every new query weekly
tweets_dict[matched_name] = tweet.id ##update dict
with open("weekly_tweets.json", "w") as f:
json.dump(tweets_dict, f) ##save dict
问题:
我想稍后更改应用程序中的内容。为此,我想从 heroku 应用程序克隆
heroku git:clone -a APP-NAME
会给我最新版本的json
文件。
情况并非如此,我实际上得到了与json
我在第一次部署期间推送的相同的空文件。
这是一个问题,因为如果我现在进行更改并推送,我将处理我上周已经处理的所有请求。
实际问题:如何获取包含更新json
文件的存储库的当前版本,以便与我的机器人保持同步?如果有的话,我在这里有什么选择(最好是免费的)?
提前致谢!万一这很重要,完整的代码在这里
解决方案
Heroku 文件系统是短暂的:应用程序保存的文件是临时的)并且在每次 Dyno 重新启动和应用程序重新部署时被删除。
Dyno 重启至少每 24 小时发生一次。
在Heroku 上的文件中,您可以看到有关如何将文件保存在外部存储上的几个选项。
可以使用源代码存储文件:使用 PyGithub 应用程序可以将文件提交/推送到您的存储库(即使它是私有的)
github = Github('personal_access_token')
# get repo by name
repository = github.get_user().get_repo('my_repo')
# define path in the repository
filename = 'files/file.json'
# JSON content
content = '{}'
# create with commit message
f = repository.create_file(filename, "create_file via PyGithub", content)
推荐阅读
- git - Git 路径模式 - 搜索文件忽略文件夹和扩展名
- java - javax @Transactional 在使用支持事务的关系数据库驱动程序时是否使用数据库级事务?
- d3.js - 我收到一个未捕获的错误:不匹配的匿名 define() 模块:
- c# - Selenium 不安全的 gmail
- angularjs - 如何在angularjs中使用ng-repeat将数据推送到对象数组时阻止重复项
- swift - 将函数泛型参数转发到泛型类类型
- regex - 匹配到第一个字符
- laravel - 如何在laravel中的关系中设置查询?
- r - 根据输出值列表创建表
- python - Matplotlib savefig 方法保存空图像