from django.core.management.base import BaseCommand

from core.models import ConveneTime
from legislators.models import Chamber

import requests


class Command(BaseCommand):
    help = u'Scrape TLO for convening times.'

    def handle(self, *args, **kwargs):
        self.update_time('House')
        self.update_time('Senate')

    def update_time(self, chamber):
        self.stdout.write(u'Updating {} convening time...'.format(chamber))

        page = requests.get('http://www.capitol.state.tx.us/tlodocs'
                            '/SessionTime/{}SessTime.js'.format(chamber))

        time_string = page.text.strip()[16:-3]

        ConveneTime.objects.update_or_create(
            chamber=Chamber.objects.get(name='Texas {}'.format(chamber)),
            defaults={
                'time_string': time_string,
            }
        )

        self.stdout.write(u'Now set to: {}'.format(time_string))

@@ -44,16 +44,50 @@
 eCommand
+%0Afrom django.utils import timezone
 %0A%0Afrom c
@@ -156,16 +156,50 @@
 hamber%0A%0A
+from dateutil.parser import parse%0A
 import r
@@ -695,16 +695,183 @@
 )%5B16:-3%5D
+%0A        status, time = time_string.split(' until ')%0A        time = timezone.make_aware(%0A            parse(time.replace(' noon', '')), timezone.get_default_timezone())
 %0A%0A      
@@ -1007,16 +1007,16 @@
 aults=%7B%0A
-
         
@@ -1051,16 +1051,80 @@
 string,%0A
+                'status': status,%0A                'time': time,%0A
         

Update scraper to account for new fields