from __future__ import unicode_literals

import os
from datetime import datetime

from dateutil.relativedelta import relativedelta
import requests

login_url = 'http://bukkesprangetnatur.barnehage.no/LogOn'
ics_url = ('http://bukkesprangetnatur.barnehage.no/Ukeplan/'
           'PlanMonthAsICalendar/61?year={year}&month={month}')


def main():
    user, password = os.environ['USER'], os.environ['PASSWORD']
    session = requests.Session()
    response = session.post(
        login_url, data={'UserName': user, 'Password': password})
    response.raise_for_status()
    now = datetime.now()
    head = session.get(
        ics_url.format(year=now.year, month=now.month)).text
    tail = session.get(
        ics_url.format(
            year=now.year, month=(
                now + relativedelta(month=1)).month)).text

    # Stitch together the two ics files
    print(('\n'.join(head.split('\n')[:-3])
           + '\n'.join(tail.split('\n')[3:])).encode('utf-8'))

from __future__ import unicode_literals

from datetime import datetime
import os
import sys

from dateutil.relativedelta import relativedelta
import requests

bases = {'r\xf8sslyngen': 59,
         'myrulla': 61}

login_url = 'http://bukkesprangetnatur.barnehage.no/LogOn'
ics_url = ('http://bukkesprangetnatur.barnehage.no/Ukeplan/'
           'PlanMonthAsICalendar/{base}?year={year}&month={month}')


def stdin_decode(data):
    if sys.stdin.encoding is not None:
        return data.decode(sys.stdin.encoding)
    else:
        # Just assume we're modern if nothing else is specified
        return data.decode('utf-8')


def stdout_encode(data):
    if sys.stdout.encoding is not None:
        return data.encode(sys.stdout.encoding)
    else:
        # Just assume we're modern if nothing else is specified
        return data.encode('utf-8')


def main():
    base, user, password = (
        stdin_decode(os.environ['BASE']), os.environ['USER'],
        os.environ['PASSWORD'])
    session = requests.Session()
    response = session.post(
        login_url, data={'UserName': user, 'Password': password})
    response.raise_for_status()
    now = datetime.now()
    head = (
        session.get(ics_url.format(
            base=bases[base], year=now.year, month=now.month))
        .content.decode('utf-8'))
    tail = (
        session.get(
            ics_url.format(
                base=bases[base], year=now.year, month=(
                    now + relativedelta(month=1)).month))
        .content.decode('utf-8'))

    # Stitch together the two ics files
    print(stdout_encode('\n'.join(head.split('\n')[:-3])
                        + '\n'.join(tail.split('\n')[3:])))

Support iterating over the different bases
Support iterating over the different bases

And fix the encoding issue that suddenly became obvious.

  from __future__ import unicode_literals
  
+ from datetime import datetime
  import os
- from datetime import datetime
+ import sys
  
  from dateutil.relativedelta import relativedelta
  import requests
  
+ bases = {'r\xf8sslyngen': 59,
+          'myrulla': 61}
+ 
  login_url = 'http://bukkesprangetnatur.barnehage.no/LogOn'
  ics_url = ('http://bukkesprangetnatur.barnehage.no/Ukeplan/'
-            'PlanMonthAsICalendar/61?year={year}&month={month}')
+            'PlanMonthAsICalendar/{base}?year={year}&month={month}')
+ 
+ 
+ def stdin_decode(data):
+     if sys.stdin.encoding is not None:
+         return data.decode(sys.stdin.encoding)
+     else:
+         # Just assume we're modern if nothing else is specified
+         return data.decode('utf-8')
+ 
+ 
+ def stdout_encode(data):
+     if sys.stdout.encoding is not None:
+         return data.encode(sys.stdout.encoding)
+     else:
+         # Just assume we're modern if nothing else is specified
+         return data.encode('utf-8')
  
  
  def main():
-     user, password = os.environ['USER'], os.environ['PASSWORD']
+     base, user, password = (
+         stdin_decode(os.environ['BASE']), os.environ['USER'],
+         os.environ['PASSWORD'])
      session = requests.Session()
      response = session.post(
          login_url, data={'UserName': user, 'Password': password})
      response.raise_for_status()
      now = datetime.now()
+     head = (
+         session.get(ics_url.format(
+             base=bases[base], year=now.year, month=now.month))
+         .content.decode('utf-8'))
+     tail = (
-     head = session.get(
+         session.get(
-         ics_url.format(year=now.year, month=now.month)).text
-     tail = session.get(
-         ics_url.format(
+             ics_url.format(
-             year=now.year, month=(
+                 base=bases[base], year=now.year, month=(
-                 now + relativedelta(month=1)).month)).text
+                     now + relativedelta(month=1)).month))
+         .content.decode('utf-8'))
  
      # Stitch together the two ics files
-     print(('\n'.join(head.split('\n')[:-3])
+     print(stdout_encode('\n'.join(head.split('\n')[:-3])
-            + '\n'.join(tail.split('\n')[3:])).encode('utf-8'))
+                         + '\n'.join(tail.split('\n')[3:])))
  
Support iterating over the different bases
## Code Before:
from __future__ import unicode_literals

import os
from datetime import datetime

from dateutil.relativedelta import relativedelta
import requests

login_url = 'http://bukkesprangetnatur.barnehage.no/LogOn'
ics_url = ('http://bukkesprangetnatur.barnehage.no/Ukeplan/'
           'PlanMonthAsICalendar/61?year={year}&month={month}')


def main():
    user, password = os.environ['USER'], os.environ['PASSWORD']
    session = requests.Session()
    response = session.post(
        login_url, data={'UserName': user, 'Password': password})
    response.raise_for_status()
    now = datetime.now()
    head = session.get(
        ics_url.format(year=now.year, month=now.month)).text
    tail = session.get(
        ics_url.format(
            year=now.year, month=(
                now + relativedelta(month=1)).month)).text

    # Stitch together the two ics files
    print(('\n'.join(head.split('\n')[:-3])
           + '\n'.join(tail.split('\n')[3:])).encode('utf-8'))

## Instruction:
Support iterating over the different bases
## Code After:
from __future__ import unicode_literals

from datetime import datetime
import os
import sys

from dateutil.relativedelta import relativedelta
import requests

bases = {'r\xf8sslyngen': 59,
         'myrulla': 61}

login_url = 'http://bukkesprangetnatur.barnehage.no/LogOn'
ics_url = ('http://bukkesprangetnatur.barnehage.no/Ukeplan/'
           'PlanMonthAsICalendar/{base}?year={year}&month={month}')


def stdin_decode(data):
    if sys.stdin.encoding is not None:
        return data.decode(sys.stdin.encoding)
    else:
        # Just assume we're modern if nothing else is specified
        return data.decode('utf-8')


def stdout_encode(data):
    if sys.stdout.encoding is not None:
        return data.encode(sys.stdout.encoding)
    else:
        # Just assume we're modern if nothing else is specified
        return data.encode('utf-8')


def main():
    base, user, password = (
        stdin_decode(os.environ['BASE']), os.environ['USER'],
        os.environ['PASSWORD'])
    session = requests.Session()
    response = session.post(
        login_url, data={'UserName': user, 'Password': password})
    response.raise_for_status()
    now = datetime.now()
    head = (
        session.get(ics_url.format(
            base=bases[base], year=now.year, month=now.month))
        .content.decode('utf-8'))
    tail = (
        session.get(
            ics_url.format(
                base=bases[base], year=now.year, month=(
                    now + relativedelta(month=1)).month))
        .content.decode('utf-8'))

    # Stitch together the two ics files
    print(stdout_encode('\n'.join(head.split('\n')[:-3])
                        + '\n'.join(tail.split('\n')[3:])))
