An API for publishing articles to NewsBreak. This script handles the complete publishing workflow including draft creation, image upload, content updates, NLP metrics calculation, and final publication.
- Python 3.9+(?) newer is better
- Required Python packages (install via pip):
pip install -r requirements.txt
The script can be configured in three ways, in order of precedence:
- Command-line arguments (highest priority)
- JSON configuration file
- Default values (lowest priority)
The script requires authentication cookies which should be set as environment variables. To set these up, create a .env file based on the provided .env.example file. You can do this by copying the example file and filling in the correct values. Find the auth cookies/headers you need for your specific organizations by:
- Logging into your NewsBreak Contributor Platform.
- Navigate to "Write an Article".
- Open the browser console and navigate to "Network".
- Type anything into the empty article content, then watch a
POSTrequest calleddraftbe sent out. - Copy the headers and cookies directly, or you can also copy the cURL request that is sent out.
- All done!
python newsbreak_api.py --title "My Article Title" --content-file "article.txt"Create a JSON configuration file (e.g., config.json):
{
"title": "My Article Title",
"author_name": "John Doe",
"author_url": "example.com",
"article_credit": "Special Report",
"content_file": "article.txt",
"image_file": "image.jpg",
"image_link": "https://example.com/image.jpg",
"image_credit": "Photo by John Doe"
}Then run:
python newsbreak_api.py --config config.json| Argument | Description | Default Value |
|---|---|---|
--title |
Article title | "Draft content..." |
--author-name |
Author's name | "Temp author" |
--author-url |
Author's URL | "harvard.edu" |
--article-credit |
Article byline/credit | "Temp byline..." |
--image-link |
URL of the image | Default NewsBreak image |
--image-credit |
Image credit text | "testing_credit!" |
--content-file |
Path to article content file | "./fake-content.txt" |
--image-file |
Path to image file to upload | "./crimson.jpg" |
--config |
Path to JSON config file | None |
- Plain text file containing the article content (markdown support incoming)
- UTF-8 encoded
- Must exist and not be empty
- Supported formats: JPEG, PNG
- Must exist for upload purposes!! and be readable
The script performs the following steps:
- Creates a draft article
- Uploads the specified image
- Updates the article content
- Calculates NLP metrics
- Publishes the article
- Basic usage with minimal parameters:
python newsbreak_api.py --title "Breaking News" --content-file "news.txt"- Full command-line configuration:
python newsbreak_api.py \
--title "Breaking News" \
--author-name "Jane Smith" \
--author-url "newsorg.com" \
--article-credit "Special Report" \
--content-file "article.txt" \
--image-file "photo.jpg" \
--image-link "https://newsorg.com/photo.jpg" \
--image-credit "Photo by Jane Smith"- Using a config file:
python newsbreak_api.py --config my_article_config.jsonCommon issues and solutions:
-
Session Expired
- Error: "Session expired - received HTML login page"
- Solution: Refresh your authentication cookies
-
File Not Found
- Error: "Content file not found" or "Could not find image file"
- Solution: Verify file paths and permissions
-
Empty Content
- Error: "Content file is empty"
- Solution: Ensure your content file contains article text