Sid*_*smg 3 python twitter google-app-engine yaml file-upload
我正在使用GAE制作一个项目,并且遇到了一个可怕的问题.
我想制作一个Twitter机器人,所以我开始发布推文的第一步.我在'dailybasic.py'所在的文件夹中创建了'tweets.txt'.
这是代码的一些部分.
#app.yaml
application: mathgirlna
version: 1
runtime: python
api_version: 1
handlers:
# - url: /static
# static_dir: static
- url: /dailybasic
script: dailybasic/dailybasic.py
- url: /.*
script: main.py
Run Code Online (Sandbox Code Playgroud)
main.py(工作正常,没有错误)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import wsgiref.handlers
class MainPage(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, None))
application = webapp.WSGIApplication([('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
dailybasic.py(每5分钟运行一次)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
import tweepy
import wsgiref.handlers
import time
def tweetit(tweet):
if len(tweet)<140:
api.update_status(tweet)
else:
diaryentries.append(tweet)
consumer_key = '******************'
consumer_secret = '*******************************************'
access_token = '**************************************************'
access_token_secret = '****************************************'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class dailybasic(webapp.RequestHandler):
def get(self):
now = time.localtime()
path = os.path.join(os.path.dirname(__file__), 'tweets.txt')
f_db = open(path, 'r')
db = f_db.readline()
while db != '':
todaynow = []
wday = now.tm_wday
if db[(wday+1)%7]=='1' and now.tm_hour * 60 + now.tm_min <= int(db[8:10]) * 60 + int(db[11:13]) and now.tm_hour * 60 + now.tm_min + 5 > int(db[8:10]) * 60 + int(db[11:13]) :
todaynow.append(db[14:])
if(len(todaynow) != 0):
import random
tweetit(todaynow[random.randrange(0,len(todaynow)-1)])
application = webapp.WSGIApplication([('/dailybasic', dailybasic)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
cron.yaml
cron:
- description: day process
url: /dailybasic
schedule: every 5 minutes from 06:00 to 01:30
timezone: Asia/Seoul
Run Code Online (Sandbox Code Playgroud)
我搜索了这个问题,并尝试了我可以放在'app.yaml'的'##'部分的所有内容,但它没有用(它可以部署,但是GAE警告为'由处理程序引用的文件'找不到:dailybasic.py').
这是一个文件树:
我想保持'index.html'只包含html代码,没有任何脚本.
我应该如何放置文件并编写app.yaml?
(抱歉英语不好)
*添加
问题是,open()不起作用,因为'tweets.txt'没有上传或在错误的目录中.
静态文件只能在app.yaml中指定的URL处直接提供给用户.它们无法被您的应用程序读取,因为它们被部署到仅提供静态文件的服务器,而不是部署到运行应用程序的基础结构.
如果您只需要从脚本中读取文件,只需将它们作为非静态文件上传即可.如果您需要将文件静态地直接提供给用户的浏览器并从脚本中读取它们,则需要在应用程序中包含2个文件副本(尽管非静态目录中的符号链接将计为第二个复制并部署).
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |