Rac*_*acu 2 cron google-app-engine python-3.x google-cloud-platform google-cloud-firestore
我正在寻找一款可以提供非常基本的骨架以使用Google App Engine运行python脚本的优秀撒玛利亚人。我已经阅读了文档,检查了相关的SO问题,但是我对WebApp格式迷失了。我要做的就是每周运行6次运行一个接受参数的python脚本或几个python脚本,以监听网站中的更改,然后将其发布到Firestore。
我了解cron格式和大多数配置文件。我被困在如何为项目安排文件以及url的工作方式。
我要问的是一个关于如何有效运行python脚本的非常基本的示例。 到目前为止,这是我找到的最好的资源,但是我无法真正理解该站点中此代码的运行情况:
`#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import feedparser
import time
class Item(db.Model):
title = db.StringProperty(required=False)
link = db.StringProperty(required=False)
date = db.StringProperty(required=False) class Scrawler(webapp.RequestHandler):
def get(self):
self.read_feed()
self.response.out.write(self.print_items())
def read_feed(self):
feeds = feedparser.parse( "http://www.techrepublic.com/search?t=14&o=1&mode=rss" )
for feed in feeds[ "items" ]:
query = Item.gql("WHERE link = :1", feed[ "link" ])
if(query.count() == 0):
item = Item()
item.title = feed[ "title" ]
item.link = feed[ "link" ]
item.date = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()))
item.put()
def print_items(self):
s = "All items:<br>"
for item in Item.all():
s += item.date + " - <a href='" + item.link + "'>" + item.title + "</a><br>"
return s application = webapp.WSGIApplication([('/', Scrawler)], debug=True) def main():
run_wsgi_app(application) if __name__ == "__main__":
main() `
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用python3.7进行测试的python脚本:
import sys
from datetime import datetime
import firebase_admin
from firebase_admin import firestore
app = firebase_admin.initialize_app()
db = firestore.client()
def hello_firestore(user_name):
db.collection('firestore_test').document('test').set({
'time': str(datetime.now()),
'user_name': user_name
})
if __name__ == "__main__":
try:
user_name = sys.argv[1]
except:
print('Error with the argument', file=sys.stderr)
try:
hello_firestore(user_name)
except:
print('Error accessing the database', file=sys.stderr)
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)
对于我了解的内容,我必须使用Flask或类似的东西才能使其工作,但我并不太了解它的工作方式,我要问的只是一个小样本和简短的说明,然后我将添加两个和两个。
最好的祝福
终于我的孩子们会再次爱我。原来我正在查看错误的GCP资源,正如@Dan_Cornilescu指出的那样,这可能是一种解决方法,但是最简单的方法是与“ Cloud Scheduler”结合使用“ Cloud Functions”,我发现它只是通过只是机会。
这篇文章是第一个提到它的文章,由于我的需求和缺乏技术上的got昧,我暂时无法使用它,因为授课者再次使用Web应用程序来说明该案例。但这实际上和您想象的一样简单,在您的Google Cloud Console中:
要为您的Python函数使用一个或多个参数,您需要使用有效负载并在其初始函数中使用以下参数:
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
这个pubsub_message你可以使用它作为你的Python函数的参数。
那就是所有人,轻松,超级轻松,最后,我认为这就像没有可视页面的GAE一样,正是我所需要的,我知道必须有一个更好的方法。
编辑:我在这里提到的文章描述了如何使用gcloud直接从您的计算机上载您的函数。
| 归档时间: |
|
| 查看次数: |
1168 次 |
| 最近记录: |