示例#1
0
def init_socialWatcher_and_check_credentials():
    try:
        dataCollector = watcherAPI()
        dataCollector.load_credentials_file(FB_CREDENTIALS_FILE)
        dataCollector.check_tokens_account_valid()
        return dataCollector
    except Exception as Error:
        send_email_error("Error: Credentials invalid")
        raise Error
def query_facebook_audience(access_token,
                            user_id,
                            query_file,
                            extra_auth_data=[],
                            response_file=None):
    """
    Build manual query and execute request.
    
    access_token :: FB access token
    user_id :: FB user ID
    query_file :: JSON file containing query
    extra_auth_data :: List of auth data pairs.
    response_file :: Name of existing response file, if needed.
    
    response :: DataFrame with query response(s) => one response per row
    """
    watcher = watcherAPI()
    if (not (access_token, user_id) in TOKENS):
        watcher.add_token_and_account_number(access_token, user_id)
    for (access_token_i, user_id_i) in extra_auth_data:
        if (not (access_token_i, user_id_i) in TOKENS):
            watcher.add_token_and_account_number(access_token_i, user_id_i)
    print('%d FB tokens' % (len(TOKENS)))

    ## execute data collection
    if (response_file is not None and os.path.exists(response_file)):
        print('using response file %s' % (response_file))
        response = watcher.load_data_and_continue_collection(response_file)
    else:
        response = watcher.run_data_collection(query_file)

    ## clean up temporary dataframes
    file_matcher = re.compile('dataframe_.*.csv')
    tmp_files = filter(lambda f: file_matcher.search(f) is not None,
                       os.listdir('.'))
    for f in tmp_files:
        os.remove(f)

    return response
import os

from pysocialwatcher import watcherAPI
from time import time, localtime
from lib import PersistentSet
from string import ascii_lowercase

COLS = ('id', 'audience_size', 'name', 'path', 'type')

watcher = watcherAPI()
watcher.load_credentials_file('credentials.txt')

autosave_every_N_seconds = 60


def explore():
    def save():
        next_set.save()
        running_set.save()
        visited_set.save()
        saved_set.save()

    last_update = time()

    working_folder = 'CACHE_interest_discovery'

    visited_set = PersistentSet(working_folder + '/visited.txt')
    saved_set = PersistentSet(working_folder + '/saved.txt')
    running_set = PersistentSet(working_folder + '/running.txt',
                                set(ascii_lowercase))
    next_set = PersistentSet(working_folder + '/next.txt')