#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import time

import sys

import os
from lxml import etree

from selenium import webdriver

reload(sys)
sys.setdefaultencoding( "utf-8" )

def scroll(url, roll_time):
    print("*************************************")
    # browser = webdriver.Firefox()
    browser = webdriver.PhantomJS()
    # browser = webdriver.Edge()
    browser.get(url)
    time.sleep(5)
    start = int(time.time())

    while 1:
        # browser.save_screenshot('facebook.png')
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        end = int(time.time())
        browser.save_screenshot(str(end) + '.png')
        if end - start >= roll_time:
            break

    html = browser.page_source
    with open('facebook.html', 'wb') as f:
        f.write(html)

    browser.close()
    print("------------------------------------------------")


def read_timest():
    f = open('facebook.html', 'rb')
    html = f.read()
    con = etree.HTML(html)
    divs = con.xpath('//div[@class="_4-u2 _4-u8"]')

    # try:
    #     id = con.xpath('//div[@id="u_0_f"]/a/text()')[0]
    # except:
    #     id = con.xpath('//div[@id="u_0_g"]/a/text()')[0]
    # name = con.xpath('//h1[@id="seo_h1_tag"]/a/text()')[0]
    i = 1
    path = './face_html/'
    for div in divs:
        html = etree.tostring(div)
        if not os.path.exists(path):
            os.makedirs(path)
        with open(path + str(i) + '.html', 'w') as f:
            f.write(html)
        i += 1
        # print(div)

    for x in range(1, i):
        f = open(path + str(x) + '.html', 'r')
        # print f
        html = f.read()
        # print html
        con = etree.HTML(html)
        date = ''.join(con.xpath('//*[@class="timestampContent"]/text()'))
        """div id="pagelet_timeline_main_column"""
        print "test",con.xpath('//div[@id="pagelet_timeline_main_column"]')
        # date = con.xpath('//div[@class="_1xnd"][last()]/div[last()]//a[@rel="theater"]/abbr/@title')
        print "date", date
        # now_ts = time.time()
        if '2017' not in date:
            date = ("2018-" + date.replace('月', '-').replace('日', '')).split(' ')[0]
        else:
            date = date.replace('年', '-').replace('月', '-').replace('日', '')
        try:
            Array = time.strptime(date, "%Y-%m-%d")
            timestamp = int(time.mktime(Array))
        except:
            continue
        return timestamp

if __name__ == '__main__':
    scroll("https://www.facebook.com/pg/MarineLePen/posts/", 10)
    time.sleep(5)
    timest = read_timest()
    print(timest)
