Coder Social home page Coder Social logo

notion2velog's Introduction

Notion2Velog

๐Ÿ’ฑ Notion 2 Velog

์ฒ˜์Œ ์ด ๋งคํฌ๋กœ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ธฐ๋กœ ๋งˆ์Œ ๋จน์€ ๊ฒƒ์€ DX ์œ„ํ‚ค์— ์ž๋ฃŒ๋ฅผ ์˜ฌ๋ฆฌ๊ณ  ๋ณด๋‹ˆ velog์—๋„ ๋”ฐ๋กœ ์˜ฌ๋ ค์„œ ์ž๋ฃŒ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์„ ๋•Œ์ž…๋‹ˆ๋‹ค.

๋‘ ๊ฐœ์˜ ์œ„ํ‚ค๋ฅผ ์ž‘์„ฑํ•˜๊ณ  velog์— ์˜ฎ๊ธฐ๋ ค๊ณ  ํ•˜๋‹ˆ ๋ฌผ๋ก  ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ๊ฐ€ ๋์ด์ง€๋งŒ ๊ทธ ๊ณผ์ • ์—ญ์‹œ ๊ท€์ฐฎ๊ฒŒ ๋Š๊ปด์กŒ์Šต๋‹ˆ๋‹ค. python ์œผ๋กœ ๋งคํฌ๋กœ ๋ฐ ์›น ์Šคํฌ๋žฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ˆ ๋งŒ๋“ค์–ด ๋ณผ ์ˆ˜ ์žˆ๊ฒ ๋‹ค ๋ผ๋Š” ์ƒ๊ฐ๊ณผ ํ•จ๊ป˜ ๋ฐ”๋กœ ์ž‘์—…์„ ์‹œ์ž‘ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿฑ ํ”„๋กœ์ ํŠธ ๋งํฌ

๐ŸŽจ ์„ค๊ณ„

python ์œผ๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฑด ์•Œ์•˜์ง€๋งŒ python ์„ ์‚ฌ์šฉํ•ด๋ณธ ๊ฑด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€ ๋•Œ ๋ฟ์ด๋ผ ๊ตฌ๊ธ€์„ ๋งŽ์ด ์ฐพ์•„๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘์— ๋ˆˆ์— ๋“ค์–ด์˜จ ๊ฒƒ์€ ์–ด๋””์„œ ๋“ค์–ด๋ณธ ์ ์ด ์žˆ๋Š” selenium ์ž…๋‹ˆ๋‹ค.

selenium์„ ์ด์šฉํ•ด notion์„ ์—ด์–ด์„œ ๋‚ด์šฉ์„ ๊ธ์–ด์˜ค๊ณ  ๋‹ค์‹œ velog๋ฅผ ์—ด์–ด ๋‚ด์šฉ์„ ๋ถ™์—ฌ๋„ฃ๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ selenium์˜ ๊ฒฝ์šฐ ๊ฐ€์ƒ์˜ ์›น์„ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๊ทธ์ธ๋ถ€ํ„ฐ ๋‹ค์‹œํ•ด์•ผ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๊ณ ,

๋กœ์ง์˜ ์ˆœ์„œ๋กœ๋Š”

  • notion ๋ฐ velog ๋กœ๊ทธ์ธ์„ ์ง„ํ–‰ํ•œ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์™„๋ฃŒ์‹œ *.pkl ์ด๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ ๋˜๋Š”๋ฐ ์ด ํŒŒ์ผ์—๋Š” ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์งํ›„์˜ cookie ๊ฐ’์ด ๋“ค์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ† ํฐ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ( ๋•Œ๋ฌธ์— .exe ํŒŒ์ผ๊ณผ *.pkl ํŒŒ์ผ์€ ๊ฐ™์€ ๊ฒฝ๋กœ(ํด๋”) ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)
  • ํ›„์—๋Š” GUI์— ๋‚˜์˜จ๋ฐ๋กœ notin url ์ž…๋ ฅ ํ›„ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ notion ๋ฐ์ดํ„ฐ ๋ณต์ œ๊ฐ€ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • ์™„๋ฃŒ๊ฐ€ ๋˜๋ฉด .exe ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋กœ๊ทธ์ธํ•œ ๊ณ„์ •์˜ velog์— ์ ‘์†ํ•ด ์ž„์‹œ์ €์žฅ ํƒญ์„ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐ŸŒฟ Selenium

selenium์„ ์ด์šฉํ•ด ์ž…๋ ฅ๋œ notion url ์— ์ ‘์†ํ•ด ๋ณธ๋ฌธ์˜ div์— ์ ‘๊ทผํ•˜๊ณ  ํ•ด๋‹น div ๋ฅผ ์ „์ฒด ๋ณต์‚ฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ „์— title ๊ฐ’์„ ์ฝ์–ด์™€ ๋”ฐ๋กœ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

๋ณธ๋ฌธ์˜ ๋‚ด์šฉ์„ ๊ธ์–ด์™€์„œ ์ง„ํ–‰ํ•˜๋ ค ํ–ˆ์ง€๋งŒ ๊ทธ๋Ÿด ๊ฒฝ์šฐ markdown์˜ ๋ฌธ๋ฒ•์ด ์ ์šฉ๋˜์ง€ ์•Š์•„. ๋งŽ์€ ์–ด๋ ค์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์—ฌ ๋ณธ๋ฌธ์„ ๋ณต์‚ฌํ•œ๋’ค

velog.io/write ์— ์ ‘์†ํ•˜์—ฌ title ๋ณ€์ˆ˜์˜ ๋‚ด์šฉ์„ ์ œ๋ชฉ์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ ,

selenium ์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ณธ๋ฌธ์˜ div ๋ฅผ click() ํ›„์— actions ์˜ chain์„ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ  ๋ถ™์—ฌ๋„ฃ๊ธฐ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ ๋’ค selenium์„ ์ด์šฉํ•ด ๊ฐ€์ ธ์˜จ ์ž„์‹œ์ €์žฅ ๋ฒ„ํŠผ์„ click() ์„ ์ด์šฉํ•ด ๋ˆŒ๋Ÿฌ์ค€ ๋’ค ์ฐฝ์„ ๋‹ซ์Šต๋‹ˆ๋‹ค.

selenium์˜ ์›น ์Šคํฌ๋žฉํ•‘ ๋Šฅ๋ ฅ์ด ๋งค์šฐ ๊ฐ•๋ ฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋œ ์ž‘์—…์ด์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒŒ ๊ฐ€๋Šฅํ•œ๊ฐ€ ํ•˜๊ณ  ์ƒ์ƒ์œผ๋กœ ์ƒ๊ฐํ•œ ๊ธฐ๋Šฅ์ด ๋Œ€๋ถ€๋ถ„ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด ๋†€๋ผ์› ์Šต๋‹ˆ๋‹ค.

๐Ÿšง ํ•œ๊ณ„์ 

  • ~~selenium์„ ์ด์šฉํ•ด ๋ณต์‚ฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ™์—ฌ๋„ฃ๊ธฐํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜์ง€๋Š” ์•Š์•„ ๋ณด์ž…๋‹ˆ๋‹ค.

๋ฌผ๋ก  ์ด๋ฏธ์ง€ url ์„ ์ œ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค์ง€๋งŒ notion์˜ ๋ณด์•ˆ ์ •์ฑ…์ธ์ง€ notion์˜ s3 ์— ์ œ๋Œ€๋กœ ์ ‘๊ทผ์„ ๋ชปํ•˜์—ฌ ์ด๋ฏธ์ง€๊ฐ€ ์•ˆ ๋‚˜์˜จ๋‹ค๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.~~

  • ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค์ง€๋งŒ ์ •ํ™•ํ•œ ์œ„์น˜์— ์‚ฝ์ž…์€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ณ ์น˜๋ ค ๋…ธ๋ ฅ์€ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • Velog์˜ ๊ฒฝ์šฐ์—๋Š” git ๋งŒ notion ์˜ ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ ์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ์—๋งŒ ๋กœ๊ทธ์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. selenium์˜ ํ•œ๊ณ„์ ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค๋งŒ ์ž๋ฃŒ๋ฅผ ๋” ์ฐพ์•„๋ณด์•„์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Code

notionCookies = None
velogCookies = None
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = Service(executable_path=ChromeDriverManager().install())
file_paths = []
notionMainLink = "https://www.notion.so/"

def notion():
    global file_paths
    if entryNotionURL.get() != "" and notionMainLink in entryNotionURL.get():
        urlCautionContent.set("")
        driver = webdriver.Chrome(service=service, options=options)
        
        file = "notion.pkl"
        fileVelog = "velogGitLogin.pkl"
        
        if os.path.exists(file):
            notionCookies = pickle.load(open(file, "rb"))
            driver.get(notionMainLink)
            driver.delete_all_cookies()
            
            for cookie in notionCookies:
                # cookie.pop("domain")
                driver.add_cookie(cookie)
        # https://www.notion.so/ryudomain/c4828705de9e41e2b35bae5691cb7a81?pvs=4 
        # https://www.notion.so/mirimdxlab/Sentry-73f05aa15fdb454a9290f4b5fc6e6f47?pvs=4
        driver.get(entryNotionURL.get())
        
        driver.implicitly_wait(120)
        
        if velogTitleEntry.get() == "":
            title = driver.find_element(By.CSS_SELECTOR, '[placeholder="์ œ๋ชฉ ์—†์Œ"]').text
        
        else:
            title = velogTitleEntry.get()
        
        contents = driver.find_elements(By.CLASS_NAME, 'notion-page-content')

        for c in contents:
            for c2 in c.find_elements(By.CLASS_NAME, "notion-selectable"):
                c2.send_keys(Keys.CONTROL + 'a')
                c2.send_keys(Keys.CONTROL + 'c')
                
        for c in contents:
            for c2 in c.find_elements(By.TAG_NAME, "img"):
                if "notion.so" in c2.get_attribute("src"):
                    file_paths.append(c2.get_attribute("src"))
        
        driver.close()
        
        driver = webdriver.Chrome(service=service, options=options)

        if os.path.exists(fileVelog):
            velogCookies = pickle.load(open(fileVelog, "rb"))
            driver.get("https://velog.io")
            driver.delete_all_cookies()
            
            for cookie in velogCookies:
                driver.add_cookie(cookie)
        
        driver.get("https://velog.io/write")
        
        driver.implicitly_wait(120)
        
        velogTitle = driver.find_element(By.CSS_SELECTOR, '[placeholder="์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”"]')
        velogTitle.send_keys(title)
        
        velogTag = driver.find_element(By.CSS_SELECTOR, '[placeholder="ํƒœ๊ทธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"]')
        velogTag.send_keys("Notion2Velog")
        velogTag.send_keys(Keys.ENTER)
        
        velogContent = driver.find_elements(By.CLASS_NAME, "CodeMirror")

        for v in velogContent:
            v.click()
            actions = ActionChains(driver)
            actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL)
            actions.key_down(Keys.ENTER).key_up(Keys.ENTER)
            actions.perform()
            
        for f in file_paths:
            driver2 = webdriver.Chrome(service=service, options=options)
            if os.path.exists(file):
                notionCookies = pickle.load(open(file, "rb"))
                driver2.get(notionMainLink)
                driver2.delete_all_cookies()
                
                for cookie in notionCookies:
                    # cookie.pop("domain")
                    driver2.add_cookie(cookie)
            
            driver2.get(f)
            c = driver2.find_element(By.TAG_NAME, "body")
            c.send_keys(Keys.CONTROL + 'c')
            actions.click()
            actions.perform()
            actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL)
            actions.perform()
            
            driver2.close()
            
        
        velogContent2 = driver.find_element(By.CLASS_NAME, "CodeMirror")
        wait = WebDriverWait(driver, 10)  # ์ตœ๋Œ€ 10์ดˆ ๋™์•ˆ ๊ธฐ๋‹ค๋ฆผ (ํ•„์š”์— ๋”ฐ๋ผ ์กฐ์ ˆ)
        def check_url(driver):
            return "์—…๋กœ๋“œ์ค‘.." not in velogContent2.text
        # ์กฐ๊ฑด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ
        wait.until(check_url)
        
        testSave = driver.find_elements(By.CLASS_NAME, "icODNG")
        for btn in testSave:
            btn.click()
            
        time.sleep(0.5)
        
        driver.close()
        driver.quit()
        driver2.quit()
        file_paths = []
        successCheck.set(" ์ž„์‹œ์ €์žฅ ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.")
        
    elif entryNotionURL.get() == "" or "https://www.notion.so/" not in entryNotionURL.get():
        successCheck.set("")
        urlCautionContent.set("notion url ์„ ์ž…๋ ฅํ•˜์„ธ์š”.")
    # pyperclip.copy(content2)
    

def velogGitHubLogin():
    driver = webdriver.Chrome(service=service, options=options)
    try:
        driver.get("https://v2.velog.io/api/v2/auth/social/redirect/github?next=/&isIntegrate=0")
        
        # ๋กœ๊ทธ์ธ ํ›„ ๋ฆฌ๋””๋ ‰ํŠธ๋  ์˜ˆ์ƒ URL
        redirected_url = "https://velog.io/"
        wait = WebDriverWait(driver, 120)
        
        # ๋ฆฌ๋””๋ ‰ํŠธ๋˜๋Š” ๋™์•ˆ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ
        wait.until(EC.url_to_be(redirected_url))
        
        if driver.current_url == redirected_url:
            pickle.dump(driver.get_cookies(), open("velogGitLogin.pkl", "wb"))
            checkVelogLogin.set("True")

    finally:
        # ์ž‘์—… ์™„๋ฃŒ ํ›„ ๋“œ๋ผ์ด๋ฒ„ ์ข…๋ฃŒ
        driver.quit()
    
def notionLogin():
    print(entryNotionURL.get() == "")
    driver = webdriver.Chrome(service=service, options=options)
    notionLoginUrl = "https://www.notion.so/login"
    try:
        driver.get(notionLoginUrl)
        
        # ๋กœ๊ทธ์ธ ํ›„ ๋ฆฌ๋””๋ ‰ํŠธ๋  ์˜ˆ์ƒ URL
        redirected_url = "https://www.notion.so/"
        wait = WebDriverWait(driver, 120)
        def check_url(driver):
            return redirected_url in driver.current_url and notionLoginUrl not in driver.current_url
        
        # ์กฐ๊ฑด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ
        wait.until(check_url)
        
        if redirected_url in driver.current_url and driver.current_url != notionLoginUrl :
            pickle.dump(driver.get_cookies(), open("notion.pkl", "wb"))
            checkNotionLogin.set("True")
            
    finally:
        # ์ž‘์—… ์™„๋ฃŒ ํ›„ ๋“œ๋ผ์ด๋ฒ„ ์ข…๋ฃŒ
        driver.quit()

๐Ÿ–Š๏ธ To Do

  • ๋ช…ํ™•ํ•œ ํ•œ๊ณ„์ ์ด ์žˆ์œผ๋‹ˆ ๊ฐœ์„ ํ•ด ๋‚˜๊ฐˆ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” ๋ฌผ๋ก  ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์˜ ํ•œ๊ณ„์ ๊นŒ์ง€ ๊ฐœ์„ ํ•ด๋ณผ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค.
  • ์ง€๊ธˆ ์ƒ๊ฐํ•˜๋Š” ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์€
    • ์ œ๋ชฉ ์ž…๋ ฅ๋ฐ›์•„ velog ์ž„์‹œ์ €์žฅ ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ( ๋นˆ ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ notion์˜ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ)
    • ๋ธŒ๋ผ์šฐ์ € ์‹ค์ œ๋กœ ๋„์šฐ์ง€ ์•Š๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ• ์•Œ์•„๋ณด๊ณ ( chrome add argument ์‹คํŒจ )
    • ์†๋„ ๊ฐœ์„ 
    • ๊นƒ readme ์—๋„ ์˜ฎ๊ธฐ๊ธฐ..?
    • etcโ€ฆ

๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ–ผ๏ธ ๊ฒฐ๊ณผ

image

notion2velog's People

Contributors

dbwpghks56 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.