Data-Collection-China / manual_upload.py
gavinzli's picture
chore: Add script descriptions and improve code readability
39fe3d1
raw
history blame
2.59 kB
"""
This script allows the user to manually upload an article to a database. It prompts the user to enter various details about the article, such as the title, content, subtitle, publish date, link, and site. It then computes the sentiment of the article's translated content and constructs a dictionary representing the article. Finally, it inserts or updates the article in the database.
Dependencies:
- decimal
- utils (custom module)
- datetime
- uuid
Usage:
1. Run the script.
2. Enter the required details about the article when prompted.
3. The script will compute the sentiment of the translated content and construct a dictionary representing the article.
4. The article will be inserted or updated in the database.
5. The article dictionary and the response from the database operation will be printed.
Note: Make sure to configure the database connection and table name before running the script.
"""
import uuid
from datetime import datetime
from decimal import Decimal
from utils import get_db_connection, sentiment_computation, translate
# User input for the article content
article_titleCN = input("Enter the title of the article: ")
article_contentCN = input("Paste the article content (CN version) here: ")
article_content = input("Paste the content of the article (EN version) here: ")
article_subtitle = input("Enter the subtitle of the article: ")
article_publish_date = input("Enter the publish date of the article (YYYY-MM-DD): ")
article_link = input("Enter the link to the article: ")
article_siteCN = input("Enter the site of the article: ")
# Compute sentiment of the translated content
sentiment_score, sentiment_label = sentiment_computation(article_contentCN)
# Construct the article dictionary for database insertion
article= {
'id': str(uuid.uuid5(uuid.NAMESPACE_OID, article_titleCN + article_publish_date)),
'site': translate(article_siteCN),
'title': translate(article_titleCN),
'titleCN': article_titleCN,
'contentCN': article_contentCN,
'category': "Knowledge Center",
'author': '',
'content': article_content,
'subtitle': article_subtitle,
'publishDate': article_publish_date,
'link': article_link,
'attachment': '',
'sentimentScore': Decimal(str(sentiment_score)).quantize(Decimal('0.01')),
'sentimentLabel': sentiment_label,
'LastModifiedDate': datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
}
# Insert or update the article in the database
dynamodb = get_db_connection()
table = dynamodb.Table('article_test')
response = table.put_item(Item=article)
print(article)
print(response)