Created
February 1, 2021 05:08
-
-
Save lifeparticle/48d8f328689a7d2a9390eefef6804c3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import sys | |
import json | |
import pathlib | |
import requests | |
def compare_data(oldData, newData): | |
data = [] | |
last_pub_date = oldData[-1]['pubDate'] | |
for nD in newData: | |
if nD['pubDate'] > last_pub_date: | |
data.insert(0, {"title": nD['title'], "link": nD['link'], "pubDate": nD['pubDate']}) | |
else: | |
break | |
return data | |
def read_json_file(filename): | |
jsonFile = open(filename, "r") | |
data = json.load(jsonFile) | |
jsonFile.close() | |
return data | |
def modify_json_file(filename, oldData, data): | |
for d in data: | |
oldData.append(d) | |
jsonFile = open(filename, "w+") | |
jsonFile.write(json.dumps(oldData, indent=4)) | |
jsonFile.close() | |
def fetch_blog_posts(link): | |
result = [] | |
response = requests.get(link) | |
if response.status_code == 200: | |
posts = json.loads(response.text)["items"] | |
for post in posts: | |
# skip the comments | |
if len(post["categories"]) != 0: | |
result.append(post) | |
elif response.status_code == 404: | |
print('Not Found: ') + link | |
return result | |
if __name__ == "__main__": | |
filename = "blog_links.json" | |
username = "@lifeparticle" | |
blog_link = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/"+username | |
newData = fetch_blog_posts(blog_link) | |
oldData = read_json_file(filename) | |
data = compare_data(oldData, newData) | |
modify_json_file(filename, oldData, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment