lin_ / app.py
Zelyanoth's picture
Upload 11 files
c9329ee verified
raw
history blame
6.38 kB
from taipy import Gui
from taipy.gui import navigate # type: ignore
import taipy.gui.builder as tgb
from functi import *
from Source_manage import source_page, menu_page
from Post_management import Post_manag
from flask import Flask, request
from urllib.parse import urlparse, parse_qs
import time
import logging
logging.getLogger('requests_oauthlib').setLevel(logging.DEBUG)
import requests
def obtenir_access_token(code, client_id, client_secret, redirect_uri):
url = "https://www.linkedin.com/oauth/v2/accessToken"
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri,
"client_id": client_id,
"client_secret": client_secret
}
response = requests.post(url, headers=headers, data=data)
response.raise_for_status() # lève une exception si le statut HTTP n’est pas 2xx
return response.json()
# Presentation text
presentation_text = """
Découvrez Lin votre assistant community manager sur Linkedin
"""
def on_navigate(state, page_name: str):
if page_name == "callback":
raw = request.environ.get("REQUEST_URI", "")
full = request.scheme + "://" + request.host + raw
state.authorization_url = full
state.authorization_url = state.authorization_url.replace("/taipy-jsx", "")
state.authorization_url = state.authorization_url.rsplit("&v", 1)[0]
state.authorization_url = state.authorization_url.replace("http://", "https://")
print(state.authorization_url,flush = True)
parsed = urlparse(state.authorization_url)
# 2. Extrait les paramètres dans un dict (valeur encapsulée dans une liste)
params = parse_qs(parsed.query)
# 3. Récupère la valeur de state
state_value = params.get("state", [""])[0]
code = params.get("code", [""])[0]
print(state_value,flush = True)
print(state.states,flush = True)
print(code,flush = True)
if state_value.endswith('?'):
state_value = state_value[:-1]
else:
state_value = state_value
if state_value == state.states :
if state.Linked_social_network == "Linkedin" :
response = obtenir_access_token(code, state.client_id, state.client_secret, state.redirect_url)
state.token = response["access_token"]
url = "https://api.linkedin.com/v2/userinfo"
headers = {
"Authorization": f"Bearer {state.token}"
}
response_open = requests.get(url, headers=headers)
response_open =response_open.json()
db_manager.add_token_network(state.token,state.Linked_social_network,state.Linked_account_name,state.user_inf.user.id,response_open)
navigate(state,"Source_Management" )
print(state.token,flush = True)
else :
print("mkjgfzmfvhsmehk")
return page_name
with tgb.Page(class_name="bodyp") as callb:
tgb.text("attendez un moment")
# Create pages
with tgb.Page(class_name="bodyp") as page_appart:
# Show login/register forms if not logged in
with tgb.part(render="{not is_logged_in}"): # type: ignore
with tgb.part(class_name="presentation", height="100vh"):
tgb.text("Lin", class_name="App_name")
with tgb.part(class_name="presentation_text"):
tgb.text(presentation_text, id="presentation_text_t")
with tgb.part(class_name="auth-container"):
# Login form (default)
with tgb.part(render="{not show_register}"): # type: ignore
tgb.text("Login to Lin")
tgb.input("{login_email}", label="Email", type="email",change_delay = -1,action_on_blur = True)
tgb.input("{login_password}", label="Password", password=True,change_delay = -1,action_on_blur = True)
tgb.button("Login", on_action=on_login )
tgb.button("Need an account? Register", on_action=toggle_register)
# Register form
with tgb.part(render="{show_register}"): # type: ignore
tgb.text("Register for Lin", )
tgb.input("{register_email}", label="Email", type="email",change_delay = -1,action_on_blur = True)
tgb.input("{register_password}", label="Password", password=True,change_delay = -1,action_on_blur = True)
tgb.input("{confirm_password}", label="Confirm Password", password=True,change_delay = -1,action_on_blur = True)
tgb.button("Register", on_action=on_register, class_name="auth-button")
tgb.button("Already have an account? Login", on_action=toggle_register, )
# Message display
with tgb.part(render="{message}"): # type: ignore
tgb.text("{message}", class_name="message")
# Show main content if logged in
with tgb.part(render="{is_logged_in}"): # type: ignore
with tgb.part(class_name="presentation", height="100vh"):
tgb.text("Lin", class_name="App_name")
tgb.text(f"Welcome, {'{current_user}'}!", class_name="welcome-message")
tgb.button("Logout", on_action=on_logout, class_name="logout-button")
with tgb.part(class_name="presentation_text"):
tgb.text(presentation_text, id="presentation_text_t")
# Define pages
pages = {
"Accueil": page_appart,
"/": menu_page,
"Source_Management" : source_page,
"callback" : callb,
"Post" : Post_manag
}
stylekit = {
"color_primary" : "#910029",
}
if __name__ == "__main__":
planning()
gui = Gui(pages=pages)
gui.run(
debug=True,
port=7860,host = "0.0.0.0",
stylekit=stylekit,
title="Lin",
dark_mode=False,
use_reloader=True
)