import scrapy
from scrapy_splash import SplashRequest 


class CoinSpider(scrapy.Spider):
    name = 'coin'
    allowed_domains = ['web.archive.org/web/20200116052415/https://www.livecoin.net/en/']

    script = '''
    function main(splash, args)
        splash.private_mode_enabled = false
        url = args.url
        assert(splash:go(url))
        assert(splash:wait(1))
        rur_tab = assert(splash:select_all(".filterPanelItem___2z5Gb"))
        rur_tab[5]:mouse_click()
        assert(splash:wait(1))
        splash:set_viewport_full()

        return {
            png = splash:png(),
            html = splash:html()
        }
    end
    '''

    def start_requests(self):
        yield SplashRequest(url="https://web.archive.org/web/20200116052415/https://www.livecoin.net/en/", callback=self.parse, endpoint='execute', args={
            'lua_source':self.script
        })

    def parse(self, response):
        for currency in response.xpath("//div[contains(@class,'ReactVirtualized__Table__row tableRow___3EtiS')] "):
            yield{
                'Currency_pair':currency.xpath(".//div[1]/div/text()").get(),
                'Volume':currency.xpath(".//div[2]/span/text()").get(),
                'Change':currency.xpath(".//div[4]/span/span/text()").get()
            }
