|
|
| import requests |
| import json |
|
|
| def scraping_instagram(ambiente, json_query): |
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
|
|
| if ambiente == 'testing': |
| url = "https://test.scrapper.qsocialnow.com/meta/api/v1/scrapings/instagram" |
| else: |
| url = "https://scrapper.qsocialnow.com/meta/api/v1/scrapings/instagram" |
|
|
| |
| headers = { |
| "api-key": api_key, |
| "Content-Type": "application/json" |
| } |
|
|
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| |
| if response.status_code == 200 or response.status_code == 201: |
| print("Scraping iniciado correctamente.") |
| print(response.json()) |
| else: |
| print(f"Error {response.status_code}: {response.text}") |
|
|
| def scraping_tiktok(ambiente, json_query): |
|
|
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
|
|
| if ambiente == 'testing': |
| url = "https://test.scrapper.qsocialnow.com/meta/api/v1/scrapings/tiktok" |
| else: |
| url = "https://scrapper.qsocialnow.com/meta/api/v1/scrapings/tiktok" |
|
|
| |
| headers = { |
| "api-key": api_key, |
| "Content-Type": "application/json" |
| } |
|
|
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| |
| if response.status_code == 200 or response.status_code == 201: |
| print("Scraping iniciado correctamente.") |
| print(response.json()) |
| else: |
| print(f"Error {response.status_code}: {response.text}") |
|
|
| def scraping_youtube(ambiente, json_query): |
| |
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
|
|
| if ambiente == 'testing': |
| url = "https://test.scrapper.qsocialnow.com/meta/api/v1/scrapings/youtube" |
| else: |
| url = "https://scrapper.qsocialnow.com/meta/api/v1/scrapings/youtube" |
|
|
| |
| headers = { |
| "api-key": api_key, |
| "Content-Type": "application/json" |
| } |
|
|
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| |
| if response.status_code == 200 or response.status_code == 201: |
| print("Scraping iniciado correctamente.") |
| print(response.json()) |
| else: |
| print(f"Error {response.status_code}: {response.text}") |
|
|
| def scraping_facebook(ambiente, json_query): |
|
|
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
|
|
| if ambiente == 'testing': |
| url = "https://test.scrapper.qsocialnow.com/meta/api/v1/scrapings/facebook" |
| else: |
| url = "https://scrapper.qsocialnow.com/meta/api/v1/scrapings/facebook" |
|
|
| |
| headers = { |
| "api-key": api_key, |
| "Content-Type": "application/json" |
| } |
|
|
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| |
| if response.status_code == 200 or response.status_code == 201: |
| print("Scraping iniciado correctamente.") |
| print(response.json()) |
| else: |
| print(f"Error {response.status_code}: {response.text}") |
|
|
| def scraping_twitter(ambiente, json_query): |
| import json |
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
|
|
| url = ( |
| "https://test.scrapper.qsocialnow.com/twitter/api/v1/scrapings" |
| if ambiente == 'testing' |
| else "https://scrapper.qsocialnow.com/twitter/api/v1/scrapings" |
| ) |
|
|
| headers = { |
| "api-key": api_key, |
| "Content-Type": "application/json" |
| } |
|
|
| |
| response = requests.post(url, json=json_query, headers=headers) |
|
|
| if response.status_code in [200, 201]: |
| print("✅ Scraping de Twitter iniciado con éxito.") |
| print(response.json()) |
| else: |
| print(f"❌ Error {response.status_code}: {response.text}") |
|
|
| def list_scraping(ambiente, red, status): |
| import requests |
| import pandas as pd |
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
|
|
| headers = { |
| "api-key": api_key, |
| "Content-Type": "application/json" |
| } |
|
|
| if red.upper() == "TWITTER": |
| url = ( |
| "https://test.scrapper.qsocialnow.com/twitter/api/v1/scrapings" |
| if ambiente == "test" |
| else "https://scrapper.qsocialnow.com/twitter/api/v1/scrapings" |
| ) |
| params = { |
| "status": status, |
| "page": 1, |
| "pageSize": 1000 |
| } |
| else: |
| url = ( |
| "https://test.scrapper.qsocialnow.com/meta/api/v1/scrapings" |
| if ambiente == "test" |
| else "https://scrapper.qsocialnow.com/meta/api/v1/scrapings" |
| ) |
| params = { |
| "socialNetwork": red.upper(), |
| "status": status, |
| "page": 1, |
| "pageSize": 1000 |
| } |
|
|
| response = requests.get(url, headers=headers, params=params) |
|
|
| if response.status_code == 200: |
| print(f"✅ Datos obtenidos correctamente para {red} ({status})") |
| data = response.json() |
| df = pd.DataFrame(data['data']['data']) |
| df["red"] = red.upper() |
| df["status"] = status |
| return df |
| else: |
| print(f"❌ Error {response.status_code} en {red} ({status}): {response.text}") |
| return pd.DataFrame() |
|
|
| def scraper_base(ambiente, uri): |
| from pymongo import MongoClient |
| import pandas as pd |
|
|
| client = MongoClient(uri) |
| db = client[ambiente] |
| collection = db['scrapings'] |
|
|
| documentos = collection.find() |
| documentos_lista_scrapings = list(documentos) |
| df_scrapings = pd.DataFrame(documentos_lista_scrapings) |
| return df_scrapings |
|
|
| def auth(ambiente): |
| import ast |
| import requests |
| import os |
| from dotenv import load_dotenv |
| |
| api_key = os.getenv('api_key') |
| load_dotenv() |
|
|
| headers = {"Content-Type": "application/json"} |
|
|
| if ambiente == 'testing': |
| url = "https://test.listening.api.qsocialnow.com/auth/api/v1/login" |
| payload = { |
| "user": os.getenv("auth_test_user"), |
| "password": os.getenv("auth_test_password") |
| } |
|
|
| elif ambiente == 'production': |
| url = "https://listening.api.qsocialnow.com/auth/api/v1/login" |
| payload = { |
| "user": os.getenv("auth_prod_user"), |
| "password": os.getenv("auth_prod_password") |
| } |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| if response.status_code == 200: |
| token = response.json().get("data", {}).get("token") |
| return token |
| else: |
| print(f"❌ Error {response.status_code}: {response.text}") |
| return None |
|
|
| def series_api(ambiente, red): |
| from features.api_calls import auth |
| import requests |
| import pandas as pd |
|
|
| token = auth(ambiente) |
| if not token: |
| return None |
|
|
| red = red.upper() |
|
|
| |
| if ambiente == 'testing': |
| base_url = "https://test.listening.api.qsocialnow.com" |
| elif ambiente == 'production': |
| base_url = "https://listening.api.qsocialnow.com" |
| else: |
| raise ValueError("Ambiente inválido") |
|
|
| |
| url = f"{base_url}/products/api/v1/{red}/series" |
|
|
| |
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| response = requests.get(url, headers=headers) |
|
|
| if response.status_code == 200: |
| data = response.json().get("data", []) |
| print(f"📦 Se encontraron {len(data)} series para {red}.") |
| return pd.DataFrame(data) if data else pd.DataFrame() |
| else: |
| print(f"❌ Error al obtener series ({response.status_code}): {response.text}") |
| return None |
|
|
| def series_base(ambiente, uri): |
| from pymongo import MongoClient |
| import pandas as pd |
|
|
| client = MongoClient(uri) |
| db = client[ambiente] |
| collection = db['series'] |
|
|
| documentos = collection.find() |
| documentos_lista_series = list(documentos) |
| df_series = pd.DataFrame(documentos_lista_series) |
| return df_series |
|
|
| def create_series(ambiente, red,json_query): |
| from features.api_calls import auth |
| import requests |
| import pandas as pd |
|
|
| token = auth(ambiente) |
| if not token: |
| return None |
|
|
| red = red.upper() |
|
|
| |
| if ambiente == 'testing': |
| base_url = "https://test.listening.api.qsocialnow.com" |
| elif ambiente == 'production': |
| base_url = "https://listening.api.qsocialnow.com" |
| else: |
| raise ValueError("Ambiente inválido") |
|
|
| |
| url = f"{base_url}/products/api/v1/{red}/series" |
|
|
| |
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| if response.status_code == 200: |
| data = response.json().get("data", []) |
| print(f"📦 Se creo la serie para {red}.") |
| return data |
| else: |
| print(f"❌ Error al obtener series ({response.status_code}): {response.text}") |
| return None |
|
|
| def create_client(ambiente, json_query): |
| from features.api_calls import auth |
| import requests |
| import pandas as pd |
|
|
| token = auth(ambiente) |
| if not token: |
| return None |
|
|
| |
| if ambiente == 'testing': |
| base_url = "https://test.listening.api.qsocialnow.com" |
| elif ambiente == 'production': |
| base_url = "https://listening.api.qsocialnow.com" |
| else: |
| raise ValueError("Ambiente inválido") |
|
|
| |
| url = f"{base_url}/products/api/v1/clients" |
|
|
| |
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| if response.status_code == 200: |
| data = response.json().get("data", []) |
| print(f"📦 Se creo el cliente.") |
| return data |
| else: |
| print(f"❌ Error al obtener series ({response.status_code}): {response.text}") |
| return None |
| |
| def clients(ambiente): |
| import requests |
| import pandas as pd |
| from features.api_calls import auth |
|
|
| token = auth(ambiente) |
| if not token: |
| return pd.DataFrame() |
|
|
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
|
|
| base_url = "https://test.listening.api.qsocialnow.com" if ambiente == 'testing' else "https://listening.api.qsocialnow.com" |
| url = f"{base_url}/products/api/v1/clients" |
|
|
| response = requests.get(url, headers=headers) |
| print("estoy obteniendo cliente") |
| if response.status_code == 200: |
| print("✅ Datos obtenidos correctamente") |
| data = response.json().get("data", []) |
| filtrado = [{"name": item["name"], "_id": item["_id"]} for item in data] |
| return pd.DataFrame(filtrado) |
| else: |
| print(f"❌ Error {response.status_code}: {response.text}") |
| print("no pude obtener cliente") |
| return pd.DataFrame() |
|
|
| def create_user(ambiente, json_query): |
| import requests |
| from features.api_calls import auth |
|
|
| |
| token = auth(ambiente) |
| if not token: |
| return None |
|
|
| |
| if ambiente == 'testing': |
| base_url = "https://test.listening.api.qsocialnow.com" |
| elif ambiente == 'production': |
| base_url = "https://listening.api.qsocialnow.com" |
| else: |
| raise ValueError("Ambiente inválido") |
|
|
| |
| url = f"{base_url}/auth/api-docs/#/CRUD%20Users/post_register" |
| |
|
|
| |
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| print("📦 Payload a enviar:") |
| print(json_query) |
|
|
| |
| try: |
| response = requests.post(url, json=json_query, headers=headers) |
| except Exception as e: |
| print(f"❌ Error de conexión: {e}") |
| return None |
|
|
| |
| if response.status_code in (200, 201): |
| print("✅ Usuario registrado correctamente.") |
| print("📥 Respuesta:") |
| print(response.json()) |
| return response.json() |
| else: |
| try: |
| print(f"❌ Error al registrar usuario ({response.status_code}): {response.json()}") |
| except Exception: |
| print(f"❌ Error al registrar usuario ({response.status_code}): {response.text}") |
| return None |
|
|
| def user_base(ambiente, uri): |
| from pymongo import MongoClient |
| import pandas as pd |
|
|
| client = MongoClient(uri) |
| db = client[ambiente] |
| collection = db['series'] |
|
|
| documentos = collection.find() |
| documentos_lista_series = list(documentos) |
| df_series = pd.DataFrame(documentos_lista_series) |
| return df_series |
|
|
| def product_asignation(ambiente,action,id,json_query): |
| from features.api_calls import auth |
| import requests |
| import pandas as pd |
|
|
| token = auth(ambiente) |
| if not token: |
| return None |
|
|
| red = red.upper() |
|
|
| |
| if ambiente == 'testing': |
| base_url = "https://test.listening.api.qsocialnow.com/products/api-docs/#/Clients" |
| elif ambiente == 'production': |
| base_url = "https://test.listening.api.qsocialnow.com/products/api-docs/#/Clients" |
| else: |
| raise ValueError("Ambiente inválido") |
|
|
| |
| url = f"{base_url}/update-client-with-product/api/v1/{id}/products/{action}" |
|
|
| |
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| payload = json_query |
|
|
| |
| response = requests.post(url, json=payload, headers=headers) |
|
|
| if response.status_code == 200: |
| data = response.json().get("data", []) |
| print(f"📦 Cambio realizado.") |
| return data |
| else: |
| print(f"❌ Error al obtener series ({response.status_code}): {response.text}") |
| return None |
|
|
| def asignar_producto_a_cliente(ambiente, client_id, action, json_query): |
| import json |
| """ |
| Asigna o remueve productos y tableros a un cliente. |
| |
| Parámetros: |
| - ambiente: 'testing' o 'production' |
| - client_id: string, ID del cliente |
| - action: 'ADD' o 'REMOVE' |
| - products_ids: lista de strings |
| - boards_ids: lista de strings |
| """ |
|
|
| |
| base_url = "https://test.listening.api.qsocialnow.com" if ambiente == "testing" \ |
| else "https://listening.api.qsocialnow.com" |
|
|
| url = f"{base_url}/products/api-docs/#/Clients/update-client-with-product/{client_id}/products/{action}" |
|
|
| |
| from features.api_calls import auth |
| token = auth(ambiente) |
|
|
| headers = { |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json" |
| } |
| |
|
|
| payload = json.loads(json_query) |
| |
| |
|
|
| response = requests.patch(url, headers=headers, json=payload) |
|
|
| if response.status_code in [200, 204]: |
| print("✅ Acción ejecutada correctamente.") |
| return response.json() if response.content else {"status": "No content"} |
| else: |
| print(f"❌ Error {response.status_code}: {response.text}") |
| return None |
|
|
|
|
|
|