#!{{ python_executable }}

from __future__ import print_function
import os
try:
  from urllib2 import HTTPError, urlopen
except ImportError:
  from urllib.error import HTTPError
  from urllib.request import urlopen
import sys

input_feed_directory = '{{ input_feed_directory }}'
monitor_feed_directory = '{{ monitor_feed_directory }}'
base_url = "{{ base_url }}"
feed_file_list = os.listdir(input_feed_directory)

rss_ok = True

for feed_file_name in feed_file_list:
  print("Getting", feed_file_name)
  url = base_url + feed_file_name
  try:
    feed = urlopen(url)
    body = feed.read()
    with open(os.path.join(monitor_feed_directory, feed_file_name + '.rss'),
              'wb') as f:
      f.write(body)
    print("FEED is ok")
  except HTTPError as e:
    sys.exit("%s is unvailable: %s" % (feed_file_name, e))
