Tive que subir meu Airflow via docker, pois utilizo do Windows e a forma como foi ensinada na aula não da certo via Windows Power Shell, após tudo estava ocorrendo como na video aula, porém ao tentar criar o twitter hook tive vários problemas (que fique como sugestão, poderiam ser abordados nas video aulas possíveis erros) o meu está como problema em puxar a biblioteca para trazer o HTTPHooks e dando erro no codigo. Segue o codigo que esta dando o erro:
from airflow.providers.http.hooks.http import HttpHook
import requests
import json
class TwitterHook(HttpHook):
def __init__(self, query, conn_id = None, start_time = None, end_time = None):
self.query = query
self.conn_id = conn_id or "twitter_default"
self.start_time = start_time
self.end_time = end_time
super().__init__(http_conn_id=self.conn_id)
def create_url(self):
query = self.query
# Tweet fields are adjustable.
# Options include:
# attachments, author_id, context_annotations,
# conversation_id, created_at, entities, geo, id,
# in_reply_to_user_id, lang, non_public_metrics, organic_metrics,
# possibly_sensitive, promoted_metrics, public_metrics, referenced_tweets,
# source, text, and withheld
tweet_fields = "tweet.fields=author_id,conversation_id,created_at,id,in_reply_to_user_id,public_metrics,text"
user_fields = "expansions=author_id&user.fields=id,name,username,created_at"
start_time = (
f"&start_time={self.start_time}"
if self.start_time
else ""
)
end_time = (
f"&end_time={self.end_time}"
if self.end_time
else ""
)
url = "{}/2/tweets/search/recent?query={}&{}&{}{}{}".format(
self.base_url, query, tweet_fields, user_fields, start_time, end_time
)
return url
def connect_to_endpoint(self, url, session):
response = requests.Request("GET", url)
prep = session.prepare_request(response)
self.log.info(f"URL: {url}")
return self.run_and_check(session, prep, {}).json()
def paginate(self, url, session, next_token=""):
if next_token:
full_url = f"{url}&next_token={next_token}"
else:
full_url = url
data = self.connect_to_endpoint(full_url, session)
yield data
if "next_token" in data.get("meta", {}):
yield from self.paginate(url, session, data['meta']['next_token'])
def run(self):
session = self.get_conn()
url = self.create_url()
yield from self.paginate(url, session)
if __name__ == "__main__":
for pg in TwitterHook("AluraOnline").run():
print(json.dumps(pg, indent=4, sort_keys=True))