Esempio n. 1
0
def __test__(dp, model, fbid):
    "Using for quick test a crawled account"

    # Create a convertor and convert files in a account to vector
    convertor = Convertor("data")
    profile = convertor.read_profile(fbid)
    profile = pd.DataFrame([profile])

    # Load datapreprocessing object and nomalizing vector
    datapreprocessing = load(dp)
    profile = datapreprocessing.convert(profile)

    # Load model and predict result
    randomforest = load(model)
    result = randomforest.predict_proba(profile)[0]

    print(result)
Esempio n. 2
0
def __solver__(conn, addr, data, **kwargs):
    "Resolve a package 'data' come from client"

    # Change data from string to json
    data = json.loads(data)

    # Get parameter server
    if "server" not in kwargs:
        raise Exception("Expected server parameter")
    server = kwargs["server"]

    if "uid" in data:
        uid = data["uid"]
    else:
        uid = None

    # Solve data have key fb_id
    if "fb_id" in data:
        fb_id = scrape_utils.__create_original_link__("https://",
                                                      data["fb_id"])
        while True:
            # Get email and password from server object
            email = server.__email__[server.__current_account__]
            password = server.__password__[server.__current_account__]

            # Create scraper and start scraping facebook account
            try:
                scraper = Scraper2(email, password, verbose="file", sender=uid)
                bSuccess = scraper(fb_id)
            except Exception as e:
                print(str(e))
                scraper.__driver__.close()
                conn.close()
                return

            # Not success if the crawler account is banned
            if bSuccess is not False:
                break

            content = json.dumps({
                "kind": "notify",
                "data": "Error in crawling, restart crawling...",
                "level": None,
                "end": "\n"
            })
            __print__(content, verbose=server.__verbose__, file=uid)

            # Switch account
            server.__current_account__ = (server.__current_account__ +
                                          1) % len(server.__email__)

        content = json.dumps({
            "kind": "notify",
            "data": "Converting crawled data to vector......",
            "level": 0,
            "end": ""
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        # Create convertor and convert crawled data to vector
        convertor = Convertor("data")
        profile = convertor.read_profile(fb_id.split("/")[-1])
        profile = pd.DataFrame([profile])

        content = json.dumps({
            "kind": "notify",
            "data": "Done",
            "level": None,
            "end": "\n"
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        content = json.dumps({
            "kind": "notify",
            "data": "Preprocessing data......",
            "level": 0,
            "end": ""
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        # Load datapreprocessing object and normalizing vector
        datapreprocessing = load("pkg/DataPreprocessingremove.dp")
        profile = datapreprocessing.convert(profile)

        content = json.dumps({
            "kind": "notify",
            "data": "Done",
            "level": None,
            "end": "\n"
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        content = json.dumps({
            "kind": "notify",
            "data": "Predicting using Random forest......",
            "level": 0,
            "end": ""
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        # Load model and predict result
        randomforest = load("pkg/overRandomForestremove.model")
        result = randomforest.predict_proba(profile)[0][0] > 0.6

        content = json.dumps({
            "kind": "notify",
            "data": "Done",
            "level": None,
            "end": "\n"
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        result = "real" if result == True else "fake"

        folder = os.path.join(os.getcwd(), "data")
        target_dir = os.path.join(folder, fb_id.split("/")[-1])
        filename = os.path.join(target_dir, "result.txt")
        with open(filename, mode="w") as f:
            f.write(result)

        content = json.dumps({
            "kind": "result",
            "data": result,
            "level": None,
            "end": "\n"
        })
        __print__(content, verbose=server.__verbose__, file=uid)

        conn.close()
Esempio n. 3
0
def __solver__(conn, addr, data, **kwargs):
    "Resolve a package 'data' come from client"

    # Change data from string to json
    data = json.loads(data)

    # Get parameter server
    if "server" not in kwargs:
        raise Exception("Expected server parameter")
    server = kwargs["server"]

    # Solve data have key fb_id
    if "fb_id" in data:
        while True:
            # Get email and password from server object
            email = server.__email__[server.__current_account__]
            password = server.__password__[server.__current_account__]

            # Create scraper and start scraping facebook account
            try:
                scraper = Scraper2(email, password, verbose= "send", sender= conn)
                bSuccess = scraper(data["fb_id"])
            except Exception as e:
                print(str(e))
                scraper.__driver__.close()
                conn.close()
                return

            # Not success if the crawler account is banned
            if bSuccess:
                break
            content = json.dumps({
                "kind": "notify", 
                "data": "Error in crawling, restart crawling...", 
                "level": None, 
                "end": "\n"
                })
            conn.send(content.encode())

            # Switch account
            server.__current_account__ = (server.__current_account__ + 1) % len(server.__email__) 

    
        content = json.dumps({"kind": "notify", "data": "Converting crawled data to vector......", "level": 0, "end": ""})
        conn.send(content.encode())

        # Create convertor and convert crawled data to vector
        convertor = Convertor("data")
        profile = convertor.read_profile(data["fb_id"].split("/")[-1])
        profile = pd.DataFrame([profile])

        content = json.dumps({"kind": "notify", "data": "Done", "level": None, "end": "\n"})
        conn.send(content.encode())
        
        content = json.dumps({"kind": "notify", "data": "Preprocessing data......", "level": 0, "end": ""})
        conn.send(content.encode())
        
        # Load datapreprocessing object and normalizing vector
        datapreprocessing = load("pkg/DataPreprocessingremove.dp")
        profile = datapreprocessing.convert(profile)

        content = json.dumps({"kind": "notify", "data": "Done", "level": None, "end": "\n"})
        conn.send(content.encode())
        
        content = json.dumps({"kind": "notify", "data": "Predicting using Random forest......", "level": 0, "end": ""})
        conn.send(content.encode())
        
        # Load model and predict result
        randomforest = load("pkg/overRandomForestremove.model")
        result = randomforest.predict_proba(profile)[0][0] > 0.6

        content = json.dumps({"kind": "notify", "data": "Done", "level": None, "end": "\n"})
        conn.send(content.encode())

        result = "real" if result == True else "fake"

        content = json.dumps({"kind": "result", "data": result, "level": None, "end": "\n"})
        conn.send(content.encode())
        
        conn.close()