import requests
import os
import json
from google.cloud import pubsub_v1


##Token
bearer_token = r'AAAAAAAAAAAAAAAAAAAAAN6yUAEAAAAAKuVoq0kNmLPK2XWVG9nv4WTbd38%3DYYmajyFpf39qIl8KDeMYrlTj1ptFqcTbZRlC0KIObuPMlEvAqS'
consumer_key = '3XTmUu99cyte9ZoWkxpAtx4bD'
consumer_secret = '<KEY>'
access_token_key = '<KEY>'
access_token_secret = '<KEY>'

##Paramater
query_params = { 'expansions': 'geo.place_id',
                'tweet.fields': 'id,text,author_id,created_at,in_reply_to_user_id,lang,public_metrics,source,possibly_sensitive,geo',
                'user.fields': 'id,name,username,created_at,description,location,public_metrics',
                'place.fields': 'full_name,id,country,country_code,geo,place_type'}
search_url = 'https://api.twitter.com/2/tweets/search/stream'

# You can adjust the rules if needed
# filter guidebook: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/build-a-rule
# sample code: https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/filtered_stream.py
filter_rules = [
#    {"value": " (của OR Một) lang:vi", "tag": "language"},
#    {"value": "(Vietnamese OR Vietnam) lang:en", "tag": "Keyword"},
    {"value": "entity:Vietnam", "tag": "entity"},
]

def bearer_oauth(r):
    """
    Method required by bearer token authentication.
    """

    r.headers["Authorization"] = f"Bearer {bearer_token}"
    r.headers["User-Agent"] = "v2SampledStreamPython"
    return r

def get_rules():
    response = requests.get(
        "https://api.twitter.com/2/tweets/search/stream/rules", auth=bearer_oauth
    )
    if response.status_code != 200:
        raise Exception(
            "Cannot get rules (HTTP {}): {}".format(response.status_code, response.text)
        )
    print(json.dumps(response.json()))
    return response.json()

def delete_all_rules(rules):
    if rules is None or "data" not in rules:
        return None

    ids = list(map(lambda rule: rule["id"], rules["data"]))
    payload = {"delete": {"ids": ids}}
    response = requests.post(
        "https://api.twitter.com/2/tweets/search/stream/rules",
        auth=bearer_oauth,
        json=payload
    )
    if response.status_code != 200:
        raise Exception(
            "Cannot delete rules (HTTP {}): {}".format(
                response.status_code, response.text
            )
        )
    print(json.dumps(response.json()))

def set_rules(filter_rules):
    if filter_rules is None:
            return None
    payload = {"add": filter_rules}
    response = requests.post(
        "https://api.twitter.com/2/tweets/search/stream/rules",
        auth=bearer_oauth,
        json=payload,
    )
    if response.status_code != 201:
        raise Exception(
            "Cannot add rules (HTTP {}): {}".format(response.status_code, response.text)
        )
    print(json.dumps(response.json()))

# Configure the connection
publisher = pubsub_v1.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
    project_id='spring-index-327605',
    topic='TwitterStream',  # Set this to something appropriate.
)

def Twitter2Pub(set):
    response = requests.get(
        "https://api.twitter.com/2/tweets/search/stream", auth=bearer_oauth, params=query_params, stream=True,
    )
    print(response.status_code)
    if response.status_code != 200:
        raise Exception(
            "Cannot get stream (HTTP {}): {}".format(
                response.status_code, response.text
            )
        )
    for response_line in response.iter_lines():
        if response_line:
            json_response = json.loads(response_line)
            data = json.dumps(json_response, indent=4, sort_keys=True).encode('utf-8')
            future = publisher.publish(topic_name, data)
            future.result()

def filterstream(request):
    rules = get_rules()
    delete = delete_all_rules(rules)
    set = set_rules(filter_rules)
    Twitter2Pub(set)

import requests
import os
import json
from google.cloud import pubsub_v1


##Token
bearer_token = r'AAAAAAAAAAAAAAAAAAAAAN6yUAEAAAAAKuVoq0kNmLPK2XWVG9nv4WTbd38%3DYYmajyFpf39qIl8KDeMYrlTj1ptFqcTbZRlC0KIObuPMlEvAqS'
consumer_key = '3XTmUu99cyte9ZoWkxpAtx4bD'
consumer_secret = '<KEY>'
access_token_key = '<KEY>'
access_token_secret = '<KEY>'

##Paramater
query_params = { 'expansions': 'geo.place_id',
                'tweet.fields': 'id,text,author_id,created_at,in_reply_to_user_id,lang,public_metrics,source,possibly_sensitive,geo',
                'user.fields': 'id,name,username,created_at,description,location,public_metrics',
                'place.fields': 'full_name,id,country,country_code,geo,place_type'}
search_url = 'https://api.twitter.com/2/tweets/search/stream'

# You can adjust the rules if needed
# filter guidebook: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/build-a-rule
# sample code: https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/filtered_stream.py
filter_rules = [
#    {"value": " (của OR Một) lang:vi", "tag": "language"},
#    {"value": "(Vietnamese OR Vietnam) lang:en", "tag": "Keyword"},
    {"value": "entity:Vietnam", "tag": "entity"},
]

def bearer_oauth(r):
    """
    Method required by bearer token authentication.
    """

    r.headers["Authorization"] = f"Bearer {bearer_token}"
    r.headers["User-Agent"] = "v2SampledStreamPython"
    return r

def get_rules():
    response = requests.get(
        "https://api.twitter.com/2/tweets/search/stream/rules", auth=bearer_oauth
    )
    if response.status_code != 200:
        raise Exception(
            "Cannot get rules (HTTP {}): {}".format(response.status_code, response.text)
        )
    print(json.dumps(response.json()))
    return response.json()

def delete_all_rules(rules):
    if rules is None or "data" not in rules:
        return None

    ids = list(map(lambda rule: rule["id"], rules["data"]))
    payload = {"delete": {"ids": ids}}
    response = requests.post(
        "https://api.twitter.com/2/tweets/search/stream/rules",
        auth=bearer_oauth,
        json=payload
    )
    if response.status_code != 200:
        raise Exception(
            "Cannot delete rules (HTTP {}): {}".format(
                response.status_code, response.text
            )
        )
    print(json.dumps(response.json()))

def set_rules(filter_rules):
    if filter_rules is None:
            return None
    payload = {"add": filter_rules}
    response = requests.post(
        "https://api.twitter.com/2/tweets/search/stream/rules",
        auth=bearer_oauth,
        json=payload,
    )
    if response.status_code != 201:
        raise Exception(
            "Cannot add rules (HTTP {}): {}".format(response.status_code, response.text)
        )
    print(json.dumps(response.json()))

# Configure the connection
publisher = pubsub_v1.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
    project_id='spring-index-327605',
    topic='TwitterStream',  # Set this to something appropriate.
)

def Twitter2Pub(set):
    response = requests.get(
        "https://api.twitter.com/2/tweets/search/stream", auth=bearer_oauth, params=query_params, stream=True,
    )
    print(response.status_code)
    if response.status_code != 200:
        raise Exception(
            "Cannot get stream (HTTP {}): {}".format(
                response.status_code, response.text
            )
        )
    for response_line in response.iter_lines():
        if response_line:
            json_response = json.loads(response_line)
            data = json.dumps(json_response, indent=4, sort_keys=True).encode('utf-8')
            future = publisher.publish(topic_name, data)
            future.result()

def filterstream(request):
    rules = get_rules()
    delete = delete_all_rules(rules)
    set = set_rules(filter_rules)
    Twitter2Pub(set)

##Token ##Paramater # You can adjust the rules if needed # filter guidebook: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/build-a-rule # sample code: https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/filtered_stream.py #    {"value": " (của OR Một) lang:vi", "tag": "language"}, #    {"value": "(Vietnamese OR Vietnam) lang:en", "tag": "Keyword"}, Method required by bearer token authentication. # Configure the connection # Set this to something appropriate.