from manage_db import Period, db
from duma import token, app_token
import requests
from datetime import datetime


def add_period():
    url = "http://api.duma.gov.ru/api/" + token + "/periods.json"
    r = requests.get(url, {'app_token': app_token})
    data = r.json()
    for conv_data in data:
        print('1111111111111')
        name = conv_data['name']
        start_date = conv_data['startDate'][2:].split(' ')[0]
        start_date = datetime.strptime(start_date, "%y-%m-%d")
        end_date = conv_data['endDate'][2:].split(' ')[0]
        end_date = datetime.strptime(end_date, "%y-%m-%d")
        print(start_date, end_date)

        conv = Period(name=name, start_date=start_date,
                                    end_date=end_date)
        try:
            db.session.add(conv)
            db.session.commit()
            print('ok')
        except:
            print('rollback')
            db.session.rollback()

if __name__ == '__main__':
    add_period()
from manage_db import Period, db
from duma import token, app_token
import requests
from datetime import datetime


def add_period():
    url = "http://api.duma.gov.ru/api/" + token + "/periods.json"
    r = requests.get(url, {'app_token': app_token})
    data = r.json()
    for conv_data in data:
        print('1111111111111')
        name = conv_data['name']
        start_date = conv_data['startDate'][2:].split(' ')[0]
        start_date = datetime.strptime(start_date, "%y-%m-%d")
        end_date = conv_data['endDate'][2:].split(' ')[0]
        end_date = datetime.strptime(end_date, "%y-%m-%d")
        print(start_date, end_date)

        conv = Period(name=name, start_date=start_date,
                                    end_date=end_date)
        try:
            db.session.add(conv)
            db.session.commit()
            print('ok')
        except:
            print('rollback')
            db.session.rollback()

if __name__ == '__main__':
    add_period()