def importMissingPersons(file):
    with open(file, 'r') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=' ', quotechar='"')
        id_old = "1"
        mp = {}
        for row in spamreader:
            id = row[0].replace(
                "<http://www.desaparecidos.ufjf.br/desaparecidos/", "")
            id = id.replace(">", "")
            property = row[1].replace("<http://www.desaparecidos.com.br/rdf/",
                                      "")
            property = property.replace("<http://xmlns.com/foaf/0.1/", "")
            property = property.replace("<http://dbpedia.org/property/", "")
            property = property.replace(
                "<http://www.w3.org/1999/02/22-rdf-syntax-ns#", "")
            property = property.replace(">", "")
            val = row[2].lower()
            if (id == id_old):
                mp[property] = val
            else:
                TwitterBD.MissingPerson(id_old, mp)
                id_old = id
                mp.clear()
                mp[property] = val

        TwitterBD.MissingPerson(id, mp)
def verify_follow():  # verify is user foolow us back  use after delet old Selected_Users
    try:
        selected_users = TwitterBD.getAll_Selected_Users()
        followers = get_followers("PjDesaparecidos")
        for selected_user in selected_users:
            if (selected_user['follower'] == "0"):
                for follower in followers:
                    if (str(follower) == selected_user['id_str']):  # if selected user follow us
                        TwitterBD.set_Selected_User(selected_user['id_str'], 'follower','1')  # change status of selected user
                        Log.logging.info("follow_back:"+ str(selected_user['screen_name']))
    except Exception:
        Log.logging.exception("Erro fazer a verificação dos seguidores")
def send_mensage_Selected_Users():
    selected_users = TwitterBD.getAll_Selected_Users()
    try:
        for selected_user in selected_users:
            if(TwitterBD.count_Selected_User_MENTIONS(selected_user['id_str']) == 0):
                send_mensage("Olá, @"+selected_user['screen_name']+". Somos um projeto que auxilia com divulgação de pessoas desaparecidas. Pela sua influência no Twitter, selecionamos você para participar do projeto. Podemos contar com sua sua ajuda na divulgação? Siga-nos de volta! Mais informações em nossa conta")
                follow_back(selected_user['screen_name'])
                tweets = TwitterEntry.api_twitter.user_timeline(count=1)#save tweet and mention
                tweet = TwitterBD.Tweet(tweets[0].id_str,tweets[0].text,tweets[0].favorite_count,'Text',tweets[0].retweet_count,str(tweets[0].created_at))
                Neo4jConnection.Save(tweet)
                mention = TwitterBD.MENTIONS(selected_user,tweet)
                Neo4jConnection.Save(mention)
    except Exception:
        Log.logging.exception("Erro ao mandar mensagem para os seguidores")
def verify_retweets(recommendation):#follow recommendations and save on DB
    if (recommendation != None):
        try:
            retweets = TwitterEntry.api_twitter.retweets(recommendation['id_str'])
            for retweet in retweets:
                selected_user = TwitterBD.find_Selected_User(retweet.user.id_str)
                if (selected_user != None):#if selected user
                    print(retweet.id_str)
                    data = TwitterBD.find_Dissemination_RETWEETED_Selected_User(recommendation['id_str'],selected_user['id_str'])

                    count = len(data)
                    if(count == 0):
                        rel = TwitterBD.RETWEETED(selected_user, recommendation,retweet.id_str)
                        Neo4jConnection.Save(rel)
                        Log.logging.info("retweet_selected_user:"******"retweet_selected_user:"******"Erro fazer a verificação dos retweets")
def importProfiles(file):
    with open(file, 'r') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=';', quoting=csv.QUOTE_NONE)
        for row in spamreader:
            try:
                TwitterBD.Selected_User(screen_name=row[0],
                                        id_str=row[1],
                                        follower="0",
                                        selected_at="ini_proj")
            except Exception:
                Log.logging.exception("Erro ao inserir: " + row)
        try:
            TwitterBD.delete_Duplicates_Users()
        except Exception:
            Log.logging.fatal("Erro ao deletar duplicatas")
def send_missingPerson_Selected_User(id_str):
    try:
        selected_user = TwitterBD.find_Selected_User(id_str)
        missing_persons =  TwitterBD.getAll_Missing_Persons() ##Colocar para pegas as pessoas desaparecidas
        #if(selected_user['follower']=='true' and TwitterBD.count_User_RECOMMENDER(selected_user['id_str'])==0):# if selected user follow back
        if(TwitterBD.count_User_DISSEMINATE(selected_user['id_str'])==0):# if selected user follow back
            for person in missing_persons:
                if (person != None):
                    messenge = 'Olá @'+selected_user['screen_name']+". Testando o envio de msng . Id:" + person["name"]
                    Log.logging.info("Difusion:{User:"******","+messenge+"}")
                    send_mensage(messenge)
                    tweets = TwitterEntry.api_twitter.user_timeline(count=1)  # save tweet and mention
                    diffusion = TwitterBD.find_Dissemination(tweets[0].id_str)
                    if(diffusion == None):
                        diffusion = TwitterBD.Dissemination(tweets[0].id_str, tweets[0].text, str(tweets[0].created_at),person["mp_id"])
                        Neo4jConnection.Save(diffusion)
                        mention = TwitterBD.DISSEMINATE(selected_user, diffusion)
                        Neo4jConnection.Save(mention)
                        mention.clear()
                        mention = TwitterBD.MENTIONS(person, diffusion)
                        Neo4jConnection.Save(mention)

                break

    except Exception:
        Log.logging.exception("Erro ao enviar mensagem com a pessoa desaparecida")
def send_missingPerson_Selected_Users():
    selected_users = TwitterBD.getAll_Selected_Users()
    for selected_user in selected_users:
        send_missingPerson_Selected_User(selected_user['id_str'])
def verify_retweets_Dissemination():
    recommendations = TwitterBD.getAll_Dissemination()
    for recommendation in recommendations:
        verify_retweets(recommendation)