mirror of
https://github.com/developersIndia/deviras.git
synced 2025-01-03 14:01:46 +05:30
add empty list check
This commit is contained in:
parent
77af1679c8
commit
c26e85aa07
@ -8,16 +8,18 @@ client_id = os.environ["REDDIT_CLIENT_ID"]
|
|||||||
client_secret = os.environ["REDDIT_CLIENT_SECRET"]
|
client_secret = os.environ["REDDIT_CLIENT_SECRET"]
|
||||||
reddit_pass = os.environ["REDDIT_PASSWORD"]
|
reddit_pass = os.environ["REDDIT_PASSWORD"]
|
||||||
username = os.environ["REDDIT_USERNAME"]
|
username = os.environ["REDDIT_USERNAME"]
|
||||||
user_agent = 'Community Roundup Post'
|
user_agent = "Community Roundup Post"
|
||||||
token = os.environ["GIST_TOKEN"]
|
token = os.environ["GIST_TOKEN"]
|
||||||
gist_id = os.environ["GIST_ID"]
|
gist_id = os.environ["GIST_ID"]
|
||||||
sub = "developersIndia"
|
sub = "developersIndia"
|
||||||
|
|
||||||
|
|
||||||
def is_last_day_of_month():
|
def is_last_day_of_month():
|
||||||
today = datetime.date.today()
|
today = datetime.date.today()
|
||||||
tomorrow = today + datetime.timedelta(days=1)
|
tomorrow = today + datetime.timedelta(days=1)
|
||||||
return tomorrow.day == 1
|
return tomorrow.day == 1
|
||||||
|
|
||||||
|
|
||||||
def get_gist_content(gist_id):
|
def get_gist_content(gist_id):
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"token {token}",
|
"Authorization": f"token {token}",
|
||||||
@ -31,6 +33,16 @@ def get_gist_content(gist_id):
|
|||||||
|
|
||||||
def get_monthly_roundup():
|
def get_monthly_roundup():
|
||||||
saved_collection_posts = json.loads(get_gist_content(gist_id))
|
saved_collection_posts = json.loads(get_gist_content(gist_id))
|
||||||
|
# filter posts for this month
|
||||||
|
saved_collection_posts = list(
|
||||||
|
filter(
|
||||||
|
lambda post: datetime.datetime.strptime(
|
||||||
|
post["created_at"], "%Y-%m-%dT%H:%M:%S"
|
||||||
|
).month
|
||||||
|
== datetime.date.today().month,
|
||||||
|
saved_collection_posts,
|
||||||
|
)
|
||||||
|
)
|
||||||
return saved_collection_posts
|
return saved_collection_posts
|
||||||
|
|
||||||
|
|
||||||
@ -41,15 +53,19 @@ def create_community_roundup_post(subreddit, posts):
|
|||||||
subreddit.flair.link_templates.user_selectable(),
|
subreddit.flair.link_templates.user_selectable(),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
title = "Community Roundup: List of must read posts & discussions that happened this month - {month} {year}".format(month=datetime.date.today().strftime("%B"), year=datetime.date.today().year)
|
title = "Community Roundup: List of must read posts & discussions that happened this month - {month} {year}".format(
|
||||||
|
month=datetime.date.today().strftime("%B"), year=datetime.date.today().year
|
||||||
|
)
|
||||||
footer_text = """\n\n
|
footer_text = """\n\n
|
||||||
|
---
|
||||||
|
|
||||||
**Community Roundup is posted on the last day of every month. You can find the [schedule on our events calendar](https://developersindia.in/events-calendar). To find the list of all [interesting posts & community threads, checkout our wiki](https://www.reddit.com/r/developersIndia/wiki/community-threads/).**
|
**Community Roundup is posted on the last day of every month. You can find the [schedule on our events calendar](https://developersindia.in/events-calendar). To find the list of all [interesting posts & community threads, checkout our wiki](https://www.reddit.com/r/developersIndia/wiki/community-threads/).**
|
||||||
"""
|
"""
|
||||||
posts_counter = 0
|
posts_counter = 0
|
||||||
for post in posts:
|
for post in posts:
|
||||||
posts_counter += 1
|
posts_counter += 1
|
||||||
text += f"{posts_counter}. [{post['title']}]({post['url']})\n"
|
text += f"#### {posts_counter}. [{post['title']}]({post['url']})\n"
|
||||||
|
|
||||||
text = text + footer_text
|
text = text + footer_text
|
||||||
|
|
||||||
@ -64,23 +80,28 @@ def create_community_roundup_post(subreddit, posts):
|
|||||||
|
|
||||||
return submission
|
return submission
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
reddit = praw.Reddit(
|
reddit = praw.Reddit(
|
||||||
client_id=client_id,
|
client_id=client_id,
|
||||||
client_secret=client_secret,
|
client_secret=client_secret,
|
||||||
username=username,
|
username=username,
|
||||||
password=reddit_pass,
|
password=reddit_pass,
|
||||||
user_agent=user_agent
|
user_agent=user_agent,
|
||||||
)
|
)
|
||||||
|
|
||||||
subreddit = reddit.subreddit(sub)
|
subreddit = reddit.subreddit(sub)
|
||||||
|
|
||||||
if is_last_day_of_month():
|
if is_last_day_of_month():
|
||||||
posts = get_monthly_roundup()
|
posts = get_monthly_roundup()
|
||||||
|
if len(posts) == 0:
|
||||||
|
print("No posts found in the collection for this month. Skipping")
|
||||||
|
return
|
||||||
create_community_roundup_post(subreddit, posts)
|
create_community_roundup_post(subreddit, posts)
|
||||||
print("Community Roundup post created successfully!")
|
print("Community Roundup post created successfully!")
|
||||||
else:
|
else:
|
||||||
print("Skipping. Not the last day of the month")
|
print("Skipping. Not the last day of the month")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user