<commit_before>﻿# -*- coding: UTF-8 -*-
from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.view import view_config
from pyramid.response import Response
from c2cgeoportal_commons.models import DBSessions, DBSession
import logging
import owncloud
import shutil
import os
import smtplib
import urllib.request
from email.mime.text import MIMEText
import time
import datetime
import sys

_ = TranslationStringFactory("geoportailv3_geoportal-server")
log = logging.getLogger(__name__)


class Casipo(object):

    def __init__(self, request):
        self.request = request
        self.config = self.request.registry.settings
        self.localizer = get_localizer(self.request)

    def __download(self, num):
        if self.staging:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["staging_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        else:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["prod_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        db_ecadastre = DBSessions['ecadastre']
        cnt = 0
        try:
            sql = "select nextval_daily ('casipo_seq')"
            results = DBSession.execute(sql)
            for res in results:
                cnt = res[0]
        except Exception as e:
            log.exception(e)
        try:
            f = urllib.request.urlopen(url, None, 1800)
            data = f
            # YYYYMMJJ_Commune_Extrait_CASIPO_nn.pdf
            commune = ""
            sql = "select commune_administrative FROM DIFFDATA.communes_adm_cad_sections WHERE code_commune = " + str(int(num[0:3])) + " GROUP BY commune_administrative"
            results = db_ecadastre.execute(sql)
            for res in results:
                commune = res['commune_administrative']

            self.filename = '/tmp/%s_%s_Extrait_CASIPO_%s.pdf' % (str(datetime.datetime.now().strftime("%Y%m%d")), commune, str(cnt))
            with open(self.filename, 'wb') as fp:
                shutil.copyfileobj(data, fp)
        except Exception as e:
            log.exception(e)
            data = None
            log.debug(url)
        return

    def __upload2owncloud(self):
        oc = owncloud.Client(self.config["casipo"]["owncloud_internal_url"])
        oc.login(self.config["casipo"]["owncloud_user"],
                 self.config["casipo"]["owncloud_password"])
        oc.put_file(os.path.basename(self.filename), self.filename)
        link_info = oc.share_file_with_link(os.path.basename(self.filename))<commit_msg>Update casipo.py<commit_after>﻿# -*- coding: UTF-8 -*-
from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.view import view_config
from pyramid.response import Response
from c2cgeoportal_commons.models import DBSessions, DBSession
import logging
import owncloud
import shutil
import os
import smtplib
import urllib.request
from email.mime.text import MIMEText
import time
import datetime
import sys

_ = TranslationStringFactory("geoportailv3_geoportal-server")
log = logging.getLogger(__name__)


class Casipo(object):

    def __init__(self, request):
        self.request = request
        self.config = self.request.registry.settings
        self.localizer = get_localizer(self.request)

    def __download(self, num):
        if self.staging:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["staging_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        else:
            url = "%s?ids=%s&token=%s" % (
                 self.config["casipo"]["prod_url"],
                 num,
                 self.config["casipo"]["fme_token"])
        db_ecadastre = DBSessions['ecadastre']
        cnt = 0
        try:
            sql = "select nextval_daily ('casipo_seq')"
            results = DBSession.execute(sql)
            for res in results:
                cnt = res[0]
        except Exception as e:
            log.exception(e)
        try:
            f = urllib.request.urlopen(url, None, 1800)
            data = f
            # YYYYMMJJ_Commune_Extrait_CASIPO_nn.pdf
            commune = ""
            sql = "select replace(commune_administrative , '/', '_') as commune_administrative FROM DIFFDATA.communes_adm_cad_sections WHERE code_commune = " + str(int(num[0:3])) + " GROUP BY commune_administrative"
            results = db_ecadastre.execute(sql)
            for res in results:
                commune = res['commune_administrative']

            self.filename = '/tmp/%s_%s_Extrait_CASIPO_%s.pdf' % (str(datetime.datetime.now().strftime("%Y%m%d")), commune, str(cnt))
            with open(self.filename, 'wb') as fp:
                shutil.copyfileobj(data, fp)
        except Exception as e:
            log.exception(e)
            data = None
            log.debug(url)
        return

    def __upload2owncloud(self):
        oc = owncloud.Client(self.config["casipo"]["owncloud_internal_url"])
        oc.login(self.config["casipo"]["owncloud_user"],
                 self.config["casipo"]["owncloud_password"])
        oc.put_file(os.path.basename(self.filename), self.filename)
        link_info = oc.share_file_with_link(os.path.basename(self.filename))
{'fraction_non_alphanumeric': 0.1138119464857735, 'fraction_numerical': 0.0052760504993404934, 'mean_word_length': 2.3916932907348243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2883362', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1614, 'n_words': 310}