import requests
import timeit
import mysql.connector

config = {
      'user': 'root',
      'password': 'Gorsel.2537',
      'host': 'localhost',
      'database': 'SportsMonkSoccer',
      'port': '3306',
      'raise_on_warnings': True,
    }

cnx = mysql.connector.connect(**config)
mycursor = cnx.cursor()

start = timeit.default_timer()
pager = 1
pageCount = 2
while pager < pageCount:

    url = 'https://soccer.sportmonks.com/api/v2.0/countries?api_token=diXewd0MVN020Z7IzEwF8DApOO3EJ1gpjCuAvcVbNU93SdEV4xBAjwxUxmmd&page=' + str(pager)

    import requests
    js = requests.get(url).json()

    js = json.dumps(js, indent = 4, sort_keys=True)

    for p in js['data']:

        if p['extra']:

            if p['extra']['flag']:
                sql = "INSERT INTO TablePython (text1,text2) VALUES (%s, %s)"
                val = (p['extra']['flag'], p['extra']['flag'])
                #print(val)
                mycursor.execute(sql, val)

                cnx.commit()



    pageCount = int(js['meta']['pagination']['total_pages'])

    pager = pager + 1





stop = timeit.default_timer()

print('Time: ', stop - start)
