Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save edemnati/04f5559f7d10f054ef16d329b84d07bd to your computer and use it in GitHub Desktop.
Save edemnati/04f5559f7d10f054ef16d329b84d07bd to your computer and use it in GitHub Desktop.
# Public packages
from datetime import datetime, timezone
import pandas as pd
from flask import escape
from flask import jsonify
#Private dependency package
from mylib.gcp_cloud_util.gcp_cloud_util import gcp_pubsub
# Global (instance-wide) scope
# Ref: https://cloud.google.com/functions/docs/bestpractices/tips
topic='news_search_to_process'
my_pubsub=gcp_pubsub()
res_connect=my_pubsub.connect(topic)
def publish_rss_feed_search_message(request):
"""
args:
data_source="Google News"
data_url="https://news.google.com/rss/search?q=apple"
data_keyword="apple"
limit="2"
"""
print('===============res_connect: ',res_connect)
content_type = request.headers['content-type']
if content_type == 'application/json':
request_json = request.get_json(silent=True)
if request_json:
#Try to read source attribute
try:
data_source = request_json['source']
except:
print("Error: Missing attribute 'source' ")
raise
#Try to read url attribute
try:
data_url = request_json['url']
except:
print("Error: Missing attribute 'url' ")
raise
#Try to read keyword attribute
try:
data_keyword = request_json['keyword']
except:
print("Error: Missing attribute 'keyword' ")
raise
#Try to read limit attribute
try:
data_limit = str(request_json['limit'])
except:
data_limit="2"
pass
else:
raise ValueError("JSON is invalid, or missing one or more parameter")
return jsonify("Missing one or more parameter")
else:
return jsonify("JSON is invalid.")
try:
response = my_pubsub.publish_message(data=b'Publish search to be processed.', source=data_source,url=data_url,keyword=data_keyword,limit=data_limit)
print('Publish search to topic: ',topic)
except:
print("Error: Publication failed to topic: {}".format(topic))
raise
return jsonify("Error: Publication failed to topic")
return jsonify('OK')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment