Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aytvill/414e586194b074345ccd6da50d50fa31 to your computer and use it in GitHub Desktop.

Select an option

Save aytvill/414e586194b074345ccd6da50d50fa31 to your computer and use it in GitHub Desktop.

Revisions

  1. @edemnati edemnati renamed this gist Oct 1, 2019. 1 changed file with 0 additions and 0 deletions.
  2. @edemnati edemnati created this gist Oct 1, 2019.
    79 changes: 79 additions & 0 deletions Cloud Function: publish_rss_feed_search_message
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@

    # 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')