import csv
import codecs
import os

artists_300 = []
with open('artists_300.txt', 'r', encoding='utf-8') as f:
    all_data = f.readlines()
    for i in range(len(all_data)):
        datas = all_data[i].split(" 20\n")
        artists_300.append(datas[0])

csv_filename = 'wikiart.csv'
artists = {}
paths = {}
with codecs.open(csv_filename,encoding = 'utf-8-sig') as f:
	reader = csv.DictReader(f)
	for row in reader:
		if row['Artist'] in artists_300:
			if row['Artist'] not in artists:

				artists[row['Artist']] = 0 
				paths[row['Artist']] = []
			else:
				if artists[row['Artist']] <= 19:
					artists[row['Artist']] = artists[row['Artist']] +1
					paths[row['Artist']].append(row['Link'])
del_artists = []
for artist in paths.keys():
	if artists[artist] <= 19:
		del_artists.append(artist)
		del artists[artist]


for artist in del_artists:
	del paths[artist]

with open('paths.txt', 'w', encoding='utf-8') as f:
	for artist in paths.keys() :
		for path in paths[artist]:
			i = artist+' '+path + '\n'
			f.write(i)

with open('artists.txt', 'w', encoding='utf-8') as f:
	for artist in artists.keys() :
		i = artist+' '+str(artists[artist])+ '\n'
		f.write(i)