<commit_before>#!/usr/bin/env python
# encoding: utf-8

# This module tries to replicate moduleCache and moduleBuffer but with mails
# stored as Drafts in a Gmail account

import configparser, os
import pickle
from bs4 import BeautifulSoup
import logging
import importlib
import pprint
import time
import sys
import urllib
importlib.reload(sys)
#sys.setdefaultencoding("UTF-8")
import moduleBlog
import moduleSocial

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

from configMod import *

def API(Acc, pp):
    # based on get_credentials from 
    # Code from
    # https://developers.google.com/gmail/api/v1/reference/users/messages/list
    # and
    # http://stackoverflow.com/questions/30742943/create-a-desktop-application-using-gmail-api

    SCOPES = 'https://www.googleapis.com/auth/gmail.modify'
    api = {}

    config = configparser.ConfigParser() 
    config.read(CONFIGDIR + '/.oauthG.cfg')
    
    fileStore = confName(api, 
            (config.get(Acc,'server'), config.get(Acc,'user'))) 

    store = file.Storage(fileStore)
    credentials = store.get()

    service = build('gmail', 'v1', http=credentials.authorize(Http()))

    return(service)

def getPostsCache(api):        
    drafts = api.users().drafts().list(userId='me').execute()
    print("drafts", drafts)
    if drafts:
        if 'drafts' in drafts:
            drafts = drafts['drafts']
        else:
            drafts = []

    listP = []
    for draft in reversed(drafts): 
        message = api.users().drafts().get(userId="me", id=draft['id']).execute()
        listP.append(message)

    return(listP)

def listPosts(api, pp, service=""):    
    outputData = {}
    files = []

    serviceName = 'Mail'+service

    outputData[serviceName] = {'sent': [], 'pending': []}
    listDrafts = getPostsCache(api)

    listP = []
    for draft in listDrafts: 
        for header in draft['message']['payload']['headers']: 
            if header['name'] == 'Subject': 
                listP.append((header['value'], '', '', '', '', '', '', '', draft['id'], ''))


    logging.info("-Posts %s"% listP)

    if len(listP) > 0: 
        for element in listP: 
            outputData[serviceName]['pending'].append(element) 

    #logging.info("Service posts profiles %s" % profiles)
    profiles = None
    return(outputData, profiles)

def confName(api, acc):
    theName = os.path.expanduser(CONFIGDIR + '/' 
                    + '.' + acc[0]+ '_' 
                    + acc[1]+ '.json')
    return(theName)

def updatePostsCache(blog, listPosts, socialNetwork=()):
    pass

def publishPost(cache, pp, posts, toPublish):
    logging.info("To publish %s" % pp.pformat(toPublish))

    profMov = toPublish[0]
    j = toPublish[1]
    logging.info("Profile %s position %d" % (profMov, j))

    update = ""
    logging.info("Cache antes %s" % pp.pformat(cache))
    profiles = cache #['profiles']
    logging.info("Cache profiles antes %s" % pp.pformat(profiles))
    accC = 0
    for profile in profiles: 
        logging.info("Social Network %s" % profile)
        if 'gmail' in profile._baseUrl:
            serviceName = 'Mail'
            #nick = profile['socialNetwork'][1]
            if (serviceName[0] in profMov) or toPublish[0]=='*': 
                if (len(toPublish) == 3):
                    logging.info("Which one?") 
                    acc = toPublish[2]
                    if int(acc) != accC: 
                        logging.info("Not this one %s" % profile)
                        accC = accC + 1
                        continue
                    else:
                        # We are in the adequate account, we can drop de qualifier
                        # for the publishing method
                        posts = posts[serviceName+str(accC)]
                else:
                    posts = posts[serviceName+str(accC)]

                logging.info("In %s" % pp.pformat(serviceName))
                logging.info("Profile %s" % pp.pformat(profile))
                logging.info("Profile posts %s" % pp.pformat(posts))
                logging.info("Service name %s" % serviceName)
                numPosts = len(posts['pending'])
                (title, link, firstLink, image, summary, summaryHtml, summaryLinks, content, links, comment) = (posts['pending'][j])
                logging.info(title, link, firstLink, image, summary, summaryHtml, summaryLinks, content, links, comment) 
                publishMethod = getattr(moduleSocial, 
                        'publish'+ method)
                logging.info("Publishing title: %s" % title)
                logging.info("Social network: %s Nick: (pending)"  % profile)
                logging.info(cache, title, link, summary, summaryHtml, summaryLinks, image, content , links )
                update = publishMethod(cache, title, link, summary, summaryHtml, summaryLinks, image, content, links)
                if update:
                        if 'text' in update: 
                            update = update['text']

    return(update)

def deletePost(cache, pp, posts, toPublish):
    logging.info("To publish %s" % pp.pformat(toPublish))
    logging.info(pp.pformat(toPublish))

    profMov = toPublish[0]
    j = toPublish[1]

    update = ""
    logging.info("Cache antes %s" % pp.pformat(cache))
    profiles = cache<commit_msg>Now we can delete drafts and publish posts<commit_after>#!/usr/bin/env python
# encoding: utf-8

# This module tries to replicate moduleCache and moduleBuffer but with mails
# stored as Drafts in a Gmail account

import configparser, os
import pickle
from bs4 import BeautifulSoup
import logging
import importlib
import pprint
import time
import sys
import urllib
importlib.reload(sys)
#sys.setdefaultencoding("UTF-8")
import moduleBlog
import moduleSocial

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

from configMod import *

def API(Acc, pp):
    # based on get_credentials from 
    # Code from
    # https://developers.google.com/gmail/api/v1/reference/users/messages/list
    # and
    # http://stackoverflow.com/questions/30742943/create-a-desktop-application-using-gmail-api

    SCOPES = 'https://www.googleapis.com/auth/gmail.modify'
    api = {}

    config = configparser.ConfigParser() 
    config.read(CONFIGDIR + '/.oauthG.cfg')
    
    fileStore = confName(api, 
            (config.get(Acc,'server'), config.get(Acc,'user'))) 

    store = file.Storage(fileStore)
    credentials = store.get()

    service = build('gmail', 'v1', http=credentials.authorize(Http()))

    return(service)

def getPostsCache(api):        
    drafts = api.users().drafts().list(userId='me').execute()
    print("drafts", drafts)
    if drafts:
        if 'drafts' in drafts:
            drafts = drafts['drafts']
        else:
            drafts = []

    listP = []
    for draft in reversed(drafts): 
        message = api.users().drafts().get(userId="me", id=draft['id']).execute()
        listP.append(message)

    return(listP)

def listPosts(api, pp, service=""):    
    outputData = {}
    files = []

    serviceName = 'Mail'+service

    outputData[serviceName] = {'sent': [], 'pending': []}
    listDrafts = getPostsCache(api)

    listP = []
    for draft in listDrafts: 
        for header in draft['message']['payload']['headers']: 
            if header['name'] == 'Subject': 
                listP.append((header['value'], '', '', '', '', '', '', '', draft['id'], ''))


    logging.info("-Posts %s"% listP)

    if len(listP) > 0: 
        for element in listP: 
            outputData[serviceName]['pending'].append(element) 

    #logging.info("Service posts profiles %s" % profiles)
    profiles = None
    return(outputData, profiles)

def confName(api, acc):
    theName = os.path.expanduser(CONFIGDIR + '/' 
                    + '.' + acc[0]+ '_' 
                    + acc[1]+ '.json')
    return(theName)

def updatePostsCache(blog, listPosts, socialNetwork=()):
    pass

def publishPost(cache, pp, posts, toPublish):
    logging.info("To publish %s" % pp.pformat(toPublish))

    profMov = toPublish[0]
    j = toPublish[1]
    logging.info("Profile %s position %d" % (profMov, j))

    update = ""
    logging.info("Cache antes %s" % pp.pformat(cache))
    profiles = cache #['profiles']
    logging.info("Cache profiles antes %s" % pp.pformat(profiles))
    accC = 0
    for profile in profiles: 
        logging.info("Social Network %s" % profile)
        if 'gmail' in profile._baseUrl:
            serviceName = 'Mail'
            #nick = profile['socialNetwork'][1]
            if (serviceName[0] in profMov) or toPublish[0]=='*': 
                if (len(toPublish) == 3):
                    logging.info("Which one?") 
                    acc = toPublish[2]
                    if int(acc) != accC: 
                        logging.info("Not this one %s" % profile)
                        accC = accC + 1
                        continue
                    else:
                        # We are in the adequate account, we can drop de qualifier
                        # for the publishing method
                        posts = posts[serviceName+str(accC)]
                else:
                    posts = posts[serviceName+str(accC)]

                logging.info("In %s" % pp.pformat(serviceName))
                logging.info("Profile %s" % pp.pformat(profile))
                logging.info("Profile posts %s" % pp.pformat(posts))
                logging.info("Service name %s" % serviceName)
                numPosts = len(posts['pending'])
                (title, link, firstLink, image, summary, summaryHtml, summaryLinks, content, links, comment) = (posts['pending'][j])
                logging.info(title, link, firstLink, image, summary, summaryHtml, summaryLinks, content, links, comment) 
                publishMethod = getattr(moduleSocial, 
                        'publish'+ serviceName)
                logging.info("Publishing title: %s" % title)
                logging.info("Social network: %s Nick: (pending)"  % profile)
                logging.info(cache, title, link, summary, summaryHtml, summaryLinks, image, content , links )
                update = publishMethod(profile, title, link, summary, summaryHtml, summaryLinks, image, content, links)
                if update:
                        if 'text' in update: 
                            update = update['text']

    return(update)

def deletePost(cache, pp, posts, toPublish):
    logging.info("To publish %s" % pp.pformat(toPublish))
    logging.info(pp.pformat(toPublish))

    profMov = toPublish[0]
    j = toPublish[1]

    update = ""
    logging.info("Cache antes %s" % pp.pformat(cache))
    profiles = cache
{'fraction_non_alphanumeric': 0.1297016861219196, 'fraction_numerical': 0.005373355567908097, 'mean_word_length': 2.301223241590214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '55570', 'n_tokens_mistral': 3264, 'n_tokens_neox': 3064, 'n_words': 768}