#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: binary -*-

import os
import sys
import random
import requests
import datetime

class utilities(object):

    # fsociety.
    # Because facebook continuously tries to beat me.
    FacebookAccessTokens = [
    '?access_token=137163128098|X26ze3coBeVGJdHD1Q5m4U3MgLM&limit=1000000000',
    '?access_token=1598446293705741|EIDHYriNzwT4_JMWg9E6Q8U8gOQ&limit=1000000000',
    '?access_token=154704804686328|yX8I3QTwMOldvBc_kzGv4xkWEm8&limit=1000000000',
    '?access_token=281601425200348|YnR0SL-rl6zJ4t-H6qWjhM3N9Wg&limit=1000000000',
    '?access_token=157316684375130|jheWRFQgoQtStQ5HAyWRpjx1Tj4&limit=1000000000',
    '?access_token=317612934941212|mg6s_5QZhxadDVG7dTROsHFrkl4&limit=1000000000',
    '?access_token=129897467024184|ZDCyItzdjtsCCcRvuMxrVU80VdQ&limit=1000000000',
    '?access_token=354465981257924|KQUR4t27h-5OJPZKrc98F89jIJs&limit=1000000000',
    '?access_token=335199219915441|LKiVZrT4hgo52GX-3NsHfupvXrM&limit=1000000000',
    '?access_token=1047959755220833|5NVvXLhdfZcdNcYPAW6WJ4oVcAE&limit=1000000000',
    '?access_token=1494383577483665|sZztrz_KY0M1HB3UJVGPcSmGWJU&limit=1000000000',
    '?access_token=261932090591763|kibMJr9cMrQOn96Gi81BhcC3Vlk&limit=1000000000',
    '?access_token=624772170989405|h39XCF3z-tSdiA8Q5OBTNaY6Ua4&limit=1000000000'
    ]

    #Get Request
    def GetHTTPRequest(self, url):
        loat = len(utilities.FacebookAccessTokens) # Length of access tokens
        token = random.randrange(0, loat)
        url = url + utilities.FacebookAccessTokens[token]
        response = requests.get(url, headers=self.user_agent())
        return response # Just return the object, not the content.

    #Post Request
    def GetHTTPostRequest(self, url, params):
        # We'll most likely never need to to a post request.
        response = requests.post(url, headers=self.user_agent(), data=params)
        return response # Just return the object, not the content.

    # Print data w/one print statement.
    def pi(self, pdata=''):
        print pdata

    # sys.argv bool checks for assurance.
    #def sabc(self, argnum):
    #    try:
    #        if sys.argv[argnum]:
    #            return True
    #    except Exception:
    #        return False

    # String boolean self-check.
    def string_bool_check(self, fn, s): # Check if string is in a file.
        #return a NoneType if the string is not in the file.
        #.readlines() May be a problem is database files get too large.

        if os.path.exists(fn) is False:
            MakeFile = open(fn, 'a').close()

        if os.path.exists(fn) is True:
            pass

        f = open(fn, 'r')
        f = f.readlines()
        for i in f:
            i = i.replace("\n", '')
            if s.decode('utf-8').encode('utf-8') in i:
                return True

    # Generate a 'random' browser user-agent.
    def user_agent(self):

        platform = random.choice(
        ['Macintosh',
        'Windows',
        'X11']
        )

        if platform == 'Macintosh':
            os  = random.choice(
            ['68K',
            'PPC']
            )

        elif platform == 'Windows':
            os  = random.choice(
            ['Win3.11',
            'WinNT3.51',
            'WinNT4.0',
            'Windows NT 5.0',
            'Windows NT 5.1',
            'Windows NT 5.2',
            'Windows NT 6.0',
            'Windows NT 6.1',
            'Windows NT 6.2',
            'Win 9x 4.90',
            'Win95',
            'Win98',
            'WindowsCE']
            )

        elif platform == 'X11':
            os  = random.choice(['Linux i686', 'Linux x86_64'])

        browser = random.choice(['chrome', 'firefox', 'ie'])

        if browser == 'chrome':
            webkit = str(random.randint(500, 599))
            version = str(random.randint(10, 100)) + '.0' + str(random.randint(0, 1500)) + '.' + str(random.randint(0, 999))
            return {"User-Agent":('Mozilla/5.0 (' + str(os) + ') AppleWebKit/' + webkit + '.0 (KHTML, like Gecko) Chrome/' + version + ' Safari/' + webkit)}

        elif browser == 'firefox':
            currentYear = datetime.date.today().year
            year = str(random.randint(2000, currentYear))
            month = random.randint(1, 12)

            if month < 10:
                month = '0' + str(month)
            else:
                month = str(month)

            day = random.randint(1, 30)

            if day < 10:
                day = '0' + str(day)

            else:
                day = str(day)

            gecko = year + month + day
            version = str(random.randint(1, 21)) + '.0'
            return {"User-Agent":str('Mozilla/5.0 (' + os + '; rv:' + version + ') Gecko/' + gecko + ' Firefox/' + version)}

        elif browser == 'ie':

            version = str(random.randint(10, 100)) + '.0'
            engine = str(random.randint(10, 100)) + '.0'
            option = random.choice(
            [True,
            False]
            )

            if option == True:
                token = random.choice(['.NET CLR', 'SV1', 'Tablet PC', 'Win64; IA64', 'Win64; x64', 'WOW64']) + '; '
            else:
                token = ''
            return {"User-Agent":str('Mozilla/5.0 (compatible; MSIE ' + version + '; ' + os + '; ' + token + 'Trident/' + engine + ')')}
