import requests
import pymongo
from pymongo import MongoClient
# Set up MongoDB connection
client = MongoClient('mongodb://localhost:27017/')
db = client['feedly']
collection = db['popular_stories']
# Feedly API endpoint
url = 'https://cloud.feedly.com/v3/streams/contents?streamId=user/{user_id}/category/global.all'
# Feedly API authentication token
token = 'YOUR_FEEDLY_API_TOKEN'
# User ID for the Feedly API
user_id = 'YOUR_FEEDLY_USER_ID'
# Make request to Feedly API
try:
response = requests.get(url.format(user_id=user_id), headers={'Authorization': 'Bearer ' + token})
except requests.exceptions.RequestException as e:
print(f'Error making request to Feedly API: {e}')
exit()
# Check if request was successful
if response.status_code == 200:
# Parse response JSON
try:
data = response.json()
except ValueError as e:
print(f'Error parsing JSON response from Feedly API: {e}')
exit()
# Get most popular stories
popular_stories = [entry for entry in data['items'] if entry.get('engagement') and entry['engagement']['popularity'] >= 100]
# Insert popular stories into MongoDB
try:
collection.insert_many(popular_stories)
except pymongo.errors.PyMongoError as e:
print(f'Error inserting data into MongoDB: {e}')
exit()
print('Successfully saved popular stories to MongoDB.')
else:
print(f'Error fetching data from Feedly API: {response.status_code}')
print(response.text)