I'm looking for a good samaritan that can provide with a very basic skeleton to run a python script using Google App Engine. I have read the documentation, check on related SO questions but I'm lost with the WebApp format. All I want to do is run one python script which accepts arguments or several python scripts, 6 times a week to listen to for changes in a website and then post them to Firestore.
I understand the cron format and most of the configurations files. I'm stuck on how to arrange my files for the project, and how the url's works.
All I'm asking is a very basic sample on how to effectively run the python scripts. This is by far the best resource that I have found, but I can't really understand what is going on with this code from that site:
`#!/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() `
This is the python script I tried to run for testing only, using python3.7:
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)
For what I understand I have to use Flask or something similar to make it work, but I don't really understand how it works, all I'm asking is a small sample and and brief explanation, and from there I'll add two and two.
Best Regards
