[GH-ISSUE #103] AI tool generator for OpenGraph Tags #82

Closed
opened 2026-03-02 23:33:30 +03:00 by kerem · 2 comments
Owner

Originally created by @AJaySi on GitHub (Aug 7, 2024).
Original GitHub issue: https://github.com/AJaySi/ALwrity/issues/103

Originally assigned to: @AJaySi on GitHub.

AI tool generator for OpenGraph Tags. This will help in SEO of the social media posts.

Originally created by @AJaySi on GitHub (Aug 7, 2024). Original GitHub issue: https://github.com/AJaySi/ALwrity/issues/103 Originally assigned to: @AJaySi on GitHub. AI tool generator for OpenGraph Tags. This will help in SEO of the social media posts.
kerem 2026-03-02 23:33:30 +03:00
Author
Owner

@AJaySi commented on GitHub (Aug 7, 2024):

import streamlit as st
from bs4 import BeautifulSoup
import requests
import spacy

nlp = spacy.load("en_core_web_sm")  # Load a small English language model

st.title("Open Graph Tag Generator")

url = st.text_input("Enter URL:")
title = st.text_input("Override Title (optional):")
description = st.text_area("Override Description (optional):")
image_url = st.text_input("Override Image URL (optional):")

if st.button("Generate Open Graph Tags"):
    if url:
        try:
            response = requests.get(url)
            soup = BeautifulSoup(response.content, 'html.parser')
            extracted_title = soup.find('title').text
            extracted_description = soup.find('meta', attrs={'name': 'description'})['content'] if soup.find('meta', attrs={'name': 'description'}) else None
            extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content'] if soup.find('meta', attrs={'property': 'og:image'}) else None
        except:
            extracted_title = None
            extracted_description = None
            extracted_image_url = None

        og_tags = """
        <meta property="og:title" content="{title}">
        <meta property="og:type" content="website">
        <meta property="og:url" content="{url}">
        <meta property="og:image" content="{image_url}">
        <meta property="og:description" content="{description}">
        """.format(
            title=title or extracted_title,
            url=url,
            image_url=image_url or extracted_image_url,
            description=description or extracted_description
        )

        st.code(og_tags, language='html')
    else:
        st.warning("Please enter a valid URL.")

Explanation:

  1. Import Libraries: Import necessary libraries: streamlit, BeautifulSoup, requests, and spacy.
  2. Set Up Streamlit App:
    • st.title("Open Graph Tag Generator") - Set the title of your app.
  3. Input Fields:
    • st.text_input("Enter URL:") - Create a text input field for the user to enter the URL.
    • st.text_input("Override Title (optional):") - Create a text input field for the user to optionally override the title.
    • st.text_area("Override Description (optional):") - Create a text area for the user to optionally override the description.
    • st.text_input("Override Image URL (optional):") - Create a text input field for the user to optionally override the image URL.
  4. Generate Button:
    • st.button("Generate Open Graph Tags") - Create a button that triggers the tag generation when clicked.
  5. Generate Open Graph Tags (Inside the Button's Logic):
    • if url: - Check if the user has entered a URL.
    • Fetch Content:
      • response = requests.get(url) - Use requests to get the HTML content of the URL.
      • soup = BeautifulSoup(response.content, 'html.parser') - Parse the HTML content with BeautifulSoup.
      • Extract Information:
        • extracted_title = soup.find('title').text - Try to extract the title from the HTML.
        • extracted_description = soup.find('meta', attrs={'name': 'description'})['content'] - Try to extract the description from the HTML.
        • extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content'] - Try to extract the og:image URL from the HTML.
    • Handle Errors:
      • except: - If any errors occur while fetching or parsing the content, set the extracted values to None.
    • Format OG Tags:
      • Use f-strings or format() to create the HTML code for the OG tags, using the provided values or the extracted values.
    • Display Results:
      • st.code(og_tags, language='html') - Display the generated HTML code using st.code().
  6. Error Handling:
    • st.warning("Please enter a valid URL.") - Display a warning message if the user has not entered a valid URL.

To run this code:

  1. Install Requirements:
    pip install streamlit beautifulsoup4 requests spacy en_core_web_sm
    
  2. Save the code as app.py.
  3. Run:
    streamlit run app.py
    
    This will open the app in your web browser.

Enhancements:

  • Error Handling: Improve error handling to provide more specific messages to the user if extraction fails.
  • Advanced Extraction: Implement more robust scraping techniques or use NLP models like SpaCy to extract more comprehensive information.
  • Social Media Preview: Use Streamlit's st.image() to display the extracted image in a preview.
  • Validation: Add validation to ensure that the provided URL, title, description, and image URL are valid.

This Streamlit app will give you a good starting point for building your Open Graph tag generator. Feel free to customize and extend it further.

<!-- gh-comment-id:2273726946 --> @AJaySi commented on GitHub (Aug 7, 2024): ```python import streamlit as st from bs4 import BeautifulSoup import requests import spacy nlp = spacy.load("en_core_web_sm") # Load a small English language model st.title("Open Graph Tag Generator") url = st.text_input("Enter URL:") title = st.text_input("Override Title (optional):") description = st.text_area("Override Description (optional):") image_url = st.text_input("Override Image URL (optional):") if st.button("Generate Open Graph Tags"): if url: try: response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') extracted_title = soup.find('title').text extracted_description = soup.find('meta', attrs={'name': 'description'})['content'] if soup.find('meta', attrs={'name': 'description'}) else None extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content'] if soup.find('meta', attrs={'property': 'og:image'}) else None except: extracted_title = None extracted_description = None extracted_image_url = None og_tags = """ <meta property="og:title" content="{title}"> <meta property="og:type" content="website"> <meta property="og:url" content="{url}"> <meta property="og:image" content="{image_url}"> <meta property="og:description" content="{description}"> """.format( title=title or extracted_title, url=url, image_url=image_url or extracted_image_url, description=description or extracted_description ) st.code(og_tags, language='html') else: st.warning("Please enter a valid URL.") ``` **Explanation:** 1. **Import Libraries:** Import necessary libraries: `streamlit`, `BeautifulSoup`, `requests`, and `spacy`. 2. **Set Up Streamlit App:** * `st.title("Open Graph Tag Generator")` - Set the title of your app. 3. **Input Fields:** * `st.text_input("Enter URL:")` - Create a text input field for the user to enter the URL. * `st.text_input("Override Title (optional):")` - Create a text input field for the user to optionally override the title. * `st.text_area("Override Description (optional):")` - Create a text area for the user to optionally override the description. * `st.text_input("Override Image URL (optional):")` - Create a text input field for the user to optionally override the image URL. 4. **Generate Button:** * `st.button("Generate Open Graph Tags")` - Create a button that triggers the tag generation when clicked. 5. **Generate Open Graph Tags (Inside the Button's Logic):** * `if url:` - Check if the user has entered a URL. * **Fetch Content:** * `response = requests.get(url)` - Use `requests` to get the HTML content of the URL. * `soup = BeautifulSoup(response.content, 'html.parser')` - Parse the HTML content with `BeautifulSoup`. * **Extract Information:** * `extracted_title = soup.find('title').text` - Try to extract the title from the HTML. * `extracted_description = soup.find('meta', attrs={'name': 'description'})['content']` - Try to extract the description from the HTML. * `extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content']` - Try to extract the `og:image` URL from the HTML. * **Handle Errors:** * `except:` - If any errors occur while fetching or parsing the content, set the extracted values to `None`. * **Format OG Tags:** * Use f-strings or `format()` to create the HTML code for the OG tags, using the provided values or the extracted values. * **Display Results:** * `st.code(og_tags, language='html')` - Display the generated HTML code using `st.code()`. 6. **Error Handling:** * `st.warning("Please enter a valid URL.")` - Display a warning message if the user has not entered a valid URL. **To run this code:** 1. **Install Requirements:** ```bash pip install streamlit beautifulsoup4 requests spacy en_core_web_sm ``` 2. **Save the code as `app.py`.** 3. **Run:** ```bash streamlit run app.py ``` This will open the app in your web browser. **Enhancements:** * **Error Handling:** Improve error handling to provide more specific messages to the user if extraction fails. * **Advanced Extraction:** Implement more robust scraping techniques or use NLP models like SpaCy to extract more comprehensive information. * **Social Media Preview:** Use Streamlit's `st.image()` to display the extracted image in a preview. * **Validation:** Add validation to ensure that the provided URL, title, description, and image URL are valid. This Streamlit app will give you a good starting point for building your Open Graph tag generator. Feel free to customize and extend it further.
Author
Owner

@AJaySi commented on GitHub (Aug 14, 2024):

Committed changes.

<!-- gh-comment-id:2288540373 --> @AJaySi commented on GitHub (Aug 14, 2024): Committed changes.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#82
No description provided.