66import argparse
77import dateutil
88import feedparser
9+ import random
10+ import time
911import requests
1012
1113from bs4 import BeautifulSoup
@@ -24,6 +26,8 @@ def main():
2426 parser .add_argument ("-c" , "--config" ,
2527 help = "config file to use" ,
2628 default = os .path .expanduser (DEFAULT_CONFIG_FILE ))
29+ parser .add_argument ("-d" , "--delay" , action = "store_true" ,
30+ help = "delay randomly from 10 to 30 seconds between each post" )
2731
2832 args = parser .parse_args ()
2933 config_file = args .config
@@ -51,19 +55,28 @@ def main():
5155 newest_post = max (newest_post , entry ['updated' ])
5256 if args .verbose :
5357 print (entry )
58+
5459 if args .dry_run :
5560 print ("trial run, not tooting " , entry ["title" ][:50 ])
5661 continue
5762
5863 image_medias = []
5964 if feed ['include_images' ] and entry ['images' ]:
6065 for image in entry ['images' ][:4 ]:
61- try :
62- image_response = requests .get (image )
63- image_medias .append (masto .media_post (image_response .content , mime_type = image_response .headers ['Content-Type' ]))
64- except :
65- print ('There was an error uploading a file' )
66- masto .status_post (feed ['template' ].format (** entry )[:499 ], media_ids = image_medias )
66+ # TODO: handle image fetch and upload exceptions
67+ image_response = requests .get (image )
68+ image_medias .append (masto .media_post (image_response .content , mime_type = image_response .headers ['Content-Type' ]))
69+
70+ if not args .dry_run :
71+ masto .status_post (
72+ feed ['template' ].format (** entry )[:499 ],
73+ media_ids = image_medias
74+ )
75+
76+ if args .delay :
77+ delay = random .randrange (10 ,30 )
78+ print ("Delaying..." + str (delay ) + " seconds..." )
79+ time .sleep (delay )
6780
6881 if not args .dry_run :
6982 config ['updated' ] = newest_post .isoformat ()
0 commit comments