

{"id":8013,"date":"2017-03-06T23:07:01","date_gmt":"2017-03-06T21:07:01","guid":{"rendered":"http:\/\/catalinx.ro\/wordpress\/?p=8013"},"modified":"2017-03-06T23:07:21","modified_gmt":"2017-03-06T21:07:21","slug":"indexul-de-la-contor-pentru-ca-python","status":"publish","type":"post","link":"https:\/\/catalinx.ro\/wordpress\/?p=8013","title":{"rendered":"Indexul de la contor, pentru ca Python"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Cred ca am mai scris despre asta. Nevasta plateste curentul la Enel, pentru ca&#8230; facturi. Si le pune si indexul de contor in site. Pentru ca altfel citesc ei o data la 3 luni, sau ceva, si pe langa plata aiuristica nici nu am un control a ceea ce consum. E important sa-mi dau seama cand am defectiuni sau las chestii aiurea pornite. Am observat ca e o problema la rezervorul e apa de la WCul unei bai cand am citit apometrul si am vazut 11m3 de apa rece intr-o luna. Doar la baia aia. Bine, ca a mai durat 1 luna pana am intreprins ceva, asta e alta treaba.<\/p>\n<p>Eu citesc indexul de la contorul de pe holul blocului, evident in alta zi decat cand il vor aia de la ENEL, ca nu poti trimite tu asa oricand, si trebuie sa i-l transmit sotiei la data stabilita.<\/p>\n<p>Nu merge cu \u00a0&#8221; noteaza-ti&#8230; sau intri tu in aplicatia pe care cu mandrie am dezvoltat-o si verifici indexul , sau rulezi un query&#8221;.<\/p>\n<p>Merge cu &#8220;da-mi indexul , sau trimite-mi mail.&#8221; \u00a0 Dar nu oricand. Ca eu citesc indexul \u00a0cand citesc si apometrele, pentru ca optimizare. Adica pe 1-3 ale lunii. \u00a0Mail trebuie trimis pe 7-8 . Iti dai seama ca uit pana atunci si cum ma cheama.<\/p>\n<p>Aveam un script in perl care trimitea mail si care s-a dus \u00a0cand am sters linuxul. Si nu-l aveam nicaieri salvat, ca asa-s eu, imi place munca , asa ca am facut altul in\u00a0<em>pitong,\u00a0<\/em>ca tot trebuie sa ma leg de asta pentru niste <em>machine learning<\/em>.<\/p>\n<p>Nu e mare lucru, am pus cap la cap niste exemple de pe net, tot ce fac e sa rulez un query , iau ce-mi trebuie si ma conectez la casuta de gmail , pentru ca sunt incapabil sa configurez un amarat de server de mail (muie suse), \u00a0trimit si aia e. Bagat in crontab.<\/p>\n<p>Daca va trebuie, ideea e moca mai jos.<\/p>\n<p>Ce ar fi mai \u00a0bun, ar fi sa inlatur &#8220;middle woman&#8221; din treaba asta. Nu, nu. Nu sa platesc eu facturile.<\/p>\n<p>Sa trimit asta cumva automat la Enel. \u00a0A auzit careva de vreun api ?<\/p>\n<p>Daca nu, trebuie sa fac un script care sa le piloteze \u00a0siteul remote. Stiu sa fac si asta , insa doar \u00a0din Windows cu AutoIT, si chiar nu as vrea sa ajung acolo, si mi-e si cam scarba de genul ala de control, ca daca schimba UI-ul se \u00a0beleste si scriptul meu.<\/p>\n<p>Bine, ar mai fi o chestie, sa citeasca cei de la Enel singuri, iar eu sa fac retrofit din factura de pe mail pentru contabilizare, dar mai dureaza. Stiu ca aveau un proiect in lucru.<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><code> #!\/usr\/bin\/python <\/code><br \/>\n<code> <\/code><br \/>\n<code> import smtplib <\/code><br \/>\n<code> from email.MIMEMultipart import MIMEMultipart <\/code><br \/>\n<code> from email.MIMEText import MIMEText <\/code><br \/>\n<code> import MySQLdb <\/code><br \/>\n<code> import datetime <\/code><br \/>\n<code> <\/code><br \/>\n<code> <\/code><br \/>\n<code> db = MySQLdb.connect(host=\"localhost\", # your host, usually localhost <\/code><br \/>\n<code> user=\"userul\u0103lmare\", # your username <\/code><br \/>\n<code> passwd=\"epulavreisiparola\", # your password <\/code><br \/>\n<code> db=\"ERM0\") # name of the data base <\/code><br \/>\n<code> <\/code><br \/>\n<code> # you must create a Cursor object. It will let <\/code><br \/>\n<code> # you execute all the queries you need <\/code><br \/>\n<code> cur = db.cursor() <\/code><br \/>\n<code> <\/code><br \/>\n<code> # Use all the SQL you like <\/code><br \/>\n<code> query = \"SELECT `index` FROM `ZRM_cons_entries` WHERE `ccid` = '205' AND `matnr` = '6001' \" <\/code><br \/>\n<code> query += \"AND `Zmark` = 'X' ORDER BY `timestamp` DESC LIMIT 0 , 1\" <\/code><br \/>\n<code> cur.execute(query) <\/code><br \/>\n<code> enelindex = '0' <\/code><br \/>\n<code> # print all the first cell of all the rows <\/code><br \/>\n<code> for row in cur.fetchall(): <\/code><br \/>\n<code> print row[0] <\/code><br \/>\n<code> enelindex = row[0] <\/code><br \/>\n<code> db.close() <\/code><br \/>\n<code> # get ze month <\/code><br \/>\n<code> mydate = datetime.datetime.now() <\/code><br \/>\n<code> luna = mydate.strftime(\"%B\") <\/code><br \/>\n<code> <\/code><br \/>\n<code> msg = MIMEMultipart() <\/code><br \/>\n<code> msg['From'] = 'mailulmeu@gmail.com' <\/code><br \/>\n<code> msg['To'] = 'nevasta@yahoo.com' <\/code><br \/>\n<code> msg['Subject'] = 'Index enel-'+ str(luna)+'-:' + str(enelindex) <\/code><br \/>\n<code> <\/code><br \/>\n<code> message = 'Indexul enel este:' + str(enelindex) <\/code><br \/>\n<code> msg.attach(MIMEText(message)) <\/code><br \/>\n<code> <\/code><br \/>\n<code> mailserver = smtplib.SMTP('smtp.gmail.com',587) <\/code><br \/>\n<code> # identify ourselves to smtp gmail client <\/code><br \/>\n<code> # secure our email with tls encryption <\/code><br \/>\n<code> mailserver.starttls() <\/code><br \/>\n<code> # re-identify ourselves as an encrypted connection <\/code><br \/>\n<code> mailserver.ehlo() <\/code><br \/>\n<code> mailserver.login('mailulmeu@gmail.com', 'nu-\u021bidauparola') <\/code><br \/>\n<code> <\/code><br \/>\n<code> mailserver.sendmail('mailulmeu@gmail.com','mailulnevestei@yahoo.com',msg.as_string()) <\/code><br \/>\n<code> <\/code><br \/>\n<code> mailserver.quit() <\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cred ca am mai scris despre asta. Nevasta plateste curentul la Enel, pentru ca&#8230; facturi. Si le pune si indexul de contor in site. Pentru&hellip;<\/p>\n","protected":false},"author":2,"featured_media":8017,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,229],"tags":[944,1704,1080],"class_list":["post-8013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrare-it","category-tech","tag-enel","tag-index-contor","tag-script-python"],"views":3476,"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/catalinx.ro\/wordpress\/wp-content\/uploads\/2017\/03\/contor.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4Eq23-25f","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8013"}],"version-history":[{"count":7,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8013\/revisions"}],"predecessor-version":[{"id":8048,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8013\/revisions\/8048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/8017"}],"wp:attachment":[{"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/catalinx.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}