<commit_before>#!/usr/bin/env python
import logging
import os
import requests
import shutil
import urllib
from PIL import Image

HOME = os.path.expanduser("~")
LOGDIR = HOME + "/usr/var/log"
APPDIR = HOME + "/.mySocial"
CONFIGDIR = APPDIR + "/config"
DATADIR = APPDIR + "/data"
TMPDIR = "/tmp"
WWWDIR = "/var/www/html/img/"
WWWADDRESS = 'https://elmundoesimperfecto.com/img/'
NAMEIMG = 'instagram.jpg'

FAIL = 'Fail!'
OK = 'OK'


def fileNamePath(url, socialNetwork=()):
    if not socialNetwork: 
        theName = (DATADIR  + '/' 
               + urllib.parse.urlparse(url).netloc + ".last")
    else: 
        theName = os.path.expanduser(DATADIR + '/' 
                    + urllib.parse.urlparse(url).netloc 
                    + '_' 
                    + socialNetwork[0] + '_' + socialNetwork[1])
    return(theName)

def getLastLink(fileName):        
    try: 
        with open(fileName, "rb") as f: 
            linkLast = f.read().decode().split()  # Last published
    except:
        # File does not exist, we need to create it.
        with open(fileName, "wb") as f:
            logging.warning("File %s does not exist. Creating it."
                    % fileName) 
            linkLast = ''  
            # None published, or non-existent file
    if len(linkLast) == 1: 
        return(linkLast[0], os.path.getmtime(fileName))
    else:
        return(linkLast, os.path.getmtime(fileName))

def checkLastLink(url, socialNetwork=()):
    # Redundant with moduleCache
    if not socialNetwork: 
        fileNameL = (DATADIR  + '/' 
               + urllib.parse.urlparse(url).netloc + ".last")
    else:
        fileNameL = fileNamePath(url, socialNetwork)+".last"
    logging.debug("Checking last link: %s" % fileNameL)
    (linkLast, timeLast) = getLastLink(fileNameL)
    return(linkLast, timeLast)

def updateLastLink(url, link, socialNetwork=()):
    if not socialNetwork: 
        fileName = (DATADIR  + '/' 
               + urllib.parse.urlparse(url).netloc + ".last")
    else: 
        fileName = fileNamePath(url, socialNetwork) + ".last"

    with open(fileName, "w") as f: 
        if isinstance(link, bytes): 
            f.write(link.decode())
        elif isinstance(link, str): 
            f.write(link)
        else:
            f.write(link[0])

def resizeImage(imgUrl):
    print(imgUrl)
    response = requests.get(imgUrl, stream=True)

    fileName = '{}/{}'.format(TMPDIR,
            urllib.parse.urlparse(response.url)[2].split('/')[-1])
    with open(fileName,'wb') as f: 
        shutil.copyfileobj(response.raw, f)

    im = Image.open(fileName)
    size = im.size

    if size[0] < size[1]: 
       dif = size[1]-size[0]
       box = (0, dif/2 , size[0], dif/2+size[0])
    else:
       dif = size[0]-size[1]
       box = (dif/2, 0 , dif/2 + size[1], size[1])
    region = im.crop(box)
    fileNameOutput = WWWDIR + NAMEIMG
    try: 
        region.save(fileNameOutput)
    except: 
        fileNameOutput = '/tmp/' + NAMEIMG
        region.save(fileNameOutput)
    address = '{}{}'.format(WWWADDRESS,NAMEIMG)
    return(address)


<commit_msg>Change in the import of the module<commit_after>#!/usr/bin/env python
import logging
import os
import requests
import shutil
import urllib

HOME = os.path.expanduser("~")
LOGDIR = HOME + "/usr/var/log"
APPDIR = HOME + "/.mySocial"
CONFIGDIR = APPDIR + "/config"
DATADIR = APPDIR + "/data"
TMPDIR = "/tmp"
WWWDIR = "/var/www/html/img/"
WWWADDRESS = 'https://elmundoesimperfecto.com/img/'
NAMEIMG = 'instagram.jpg'

FAIL = 'Fail!'
OK = 'OK'


def fileNamePath(url, socialNetwork=()):
    if not socialNetwork: 
        theName = (DATADIR  + '/' 
               + urllib.parse.urlparse(url).netloc + ".last")
    else: 
        theName = os.path.expanduser(DATADIR + '/' 
                    + urllib.parse.urlparse(url).netloc 
                    + '_' 
                    + socialNetwork[0] + '_' + socialNetwork[1])
    return(theName)

def getLastLink(fileName):        
    try: 
        with open(fileName, "rb") as f: 
            linkLast = f.read().decode().split()  # Last published
    except:
        # File does not exist, we need to create it.
        with open(fileName, "wb") as f:
            logging.warning("File %s does not exist. Creating it."
                    % fileName) 
            linkLast = ''  
            # None published, or non-existent file
    if len(linkLast) == 1: 
        return(linkLast[0], os.path.getmtime(fileName))
    else:
        return(linkLast, os.path.getmtime(fileName))

def checkLastLink(url, socialNetwork=()):
    # Redundant with moduleCache
    if not socialNetwork: 
        fileNameL = (DATADIR  + '/' 
               + urllib.parse.urlparse(url).netloc + ".last")
    else:
        fileNameL = fileNamePath(url, socialNetwork)+".last"
    logging.debug("Checking last link: %s" % fileNameL)
    (linkLast, timeLast) = getLastLink(fileNameL)
    return(linkLast, timeLast)

def updateLastLink(url, link, socialNetwork=()):
    if not socialNetwork: 
        fileName = (DATADIR  + '/' 
               + urllib.parse.urlparse(url).netloc + ".last")
    else: 
        fileName = fileNamePath(url, socialNetwork) + ".last"

    with open(fileName, "w") as f: 
        if isinstance(link, bytes): 
            f.write(link.decode())
        elif isinstance(link, str): 
            f.write(link)
        else:
            f.write(link[0])

def resizeImage(imgUrl):
    from PIL import Image
    print(imgUrl)
    response = requests.get(imgUrl, stream=True)

    fileName = '{}/{}'.format(TMPDIR,
            urllib.parse.urlparse(response.url)[2].split('/')[-1])
    with open(fileName,'wb') as f: 
        shutil.copyfileobj(response.raw, f)

    im = Image.open(fileName)
    size = im.size

    if size[0] < size[1]: 
       dif = size[1]-size[0]
       box = (0, dif/2 , size[0], dif/2+size[0])
    else:
       dif = size[0]-size[1]
       box = (dif/2, 0 , dif/2 + size[1], size[1])
    region = im.crop(box)
    fileNameOutput = WWWDIR + NAMEIMG
    try: 
        region.save(fileNameOutput)
    except: 
        fileNameOutput = '/tmp/' + NAMEIMG
        region.save(fileNameOutput)
    address = '{}{}'.format(WWWADDRESS,NAMEIMG)
    return(address)



{'fraction_non_alphanumeric': 0.1429261226460647, 'fraction_numerical': 0.00740383067761146, 'mean_word_length': 2.275698471270427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1141048', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2062, 'n_words': 444}