|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import numpy as np\n", |
| 10 | + "import pandas as pd\n", |
| 11 | + "# import json, requests\n", |
| 12 | + "from pandas.io.json import json_normalize\n", |
| 13 | + "from datetime import datetime, timedelta\n", |
| 14 | + "import smtplib as smtp\n", |
| 15 | + "from getpass import getpass" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 2, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "name": "stdout", |
| 25 | + "output_type": "stream", |
| 26 | + "text": [ |
| 27 | + "20181116000000 20181116200000\n", |
| 28 | + "2018-11-16 00:00 2018-11-16 20:00\n" |
| 29 | + ] |
| 30 | + } |
| 31 | + ], |
| 32 | + "source": [ |
| 33 | + "today = datetime.now()\n", |
| 34 | + "delta = 1\n", |
| 35 | + "\n", |
| 36 | + "if today.hour <= 9 or delta > 1:\n", |
| 37 | + " day_before = today - timedelta(days = delta)\n", |
| 38 | + " day_before = day_before.replace(hour = 0, minute = 0, second = 0)\n", |
| 39 | + "else:\n", |
| 40 | + " day_before = today.replace(hour = 0, minute = 0, second = 0)\n", |
| 41 | + "\n", |
| 42 | + "hour_start = '000000'\n", |
| 43 | + "hour_end = '080000'\n", |
| 44 | + "\n", |
| 45 | + "def format_str_zeros(x):\n", |
| 46 | + " if x < 10:\n", |
| 47 | + " out = '0' + str(x)\n", |
| 48 | + " else:\n", |
| 49 | + " out = str(x)\n", |
| 50 | + " return out\n", |
| 51 | + "\n", |
| 52 | + "hour_start = format_str_zeros(day_before.hour) + '0000'\n", |
| 53 | + "day_before_str = format_str_zeros(day_before.day)\n", |
| 54 | + "month_before_str = format_str_zeros(day_before.month)\n", |
| 55 | + "\n", |
| 56 | + "# print(day_before)\n", |
| 57 | + "\n", |
| 58 | + "if today.hour < 22:\n", |
| 59 | + " today = today.replace(hour = today.hour + 2)\n", |
| 60 | + "\n", |
| 61 | + "hour_end = format_str_zeros(today.hour) + '0000'\n", |
| 62 | + "day = format_str_zeros(today.day)\n", |
| 63 | + "month = format_str_zeros(today.month)\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + "start = str(today.year) + month_before_str + day_before_str + hour_start\n", |
| 67 | + "end = str(today.year) + month + day + hour_end\n", |
| 68 | + "print(start, end)\n", |
| 69 | + "\n", |
| 70 | + "start_formatted = datetime.strptime(start, '%Y%m%d%H%M%S').strftime('%Y-%m-%d %H:%M')\n", |
| 71 | + "end_formatted = datetime.strptime(end, '%Y%m%d%H%M%S').strftime('%Y-%m-%d %H:%M')\n", |
| 72 | + "print(start_formatted, end_formatted)\n", |
| 73 | + "\n", |
| 74 | + "url = \"https://oplata.mtsbank.ru/payment/rest/getLastOrdersForMerchants.do?userName=lvlup_school-api&password=S1o173S95%21%40f&language=ru&page=0&size=100&from={0}&to={1}&transactionStates=DEPOSITED&merchants=lvlup_school&searchByCreatedDate=false\".format(start, end)\n", |
| 75 | + "# print(url)" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 3, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "name": "stdout", |
| 85 | + "output_type": "stream", |
| 86 | + "text": [ |
| 87 | + "No completed payments from 2018-11-16 00:00 to 2018-11-16 20:00.\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "response = pd.read_json(url, orient='columns')\n", |
| 93 | + "\n", |
| 94 | + "if response['orderStatuses'].empty:\n", |
| 95 | + " text_to_send = 'No completed payments from {} to {}.'.format(start_formatted, end_formatted)\n", |
| 96 | + " print(text_to_send)\n", |
| 97 | + "else:\n", |
| 98 | + " text_to_send = 'List of payments from {} to {}: \\n\\n'.format(start_formatted, end_formatted)\n", |
| 99 | + " # print(response['orderStatuses'])\n", |
| 100 | + " \n", |
| 101 | + " response = json_normalize(response['orderStatuses'])\n", |
| 102 | + " # response.to_excel('/Users/stellarnode/Desktop/response.xls')\n", |
| 103 | + " # response.head()\n", |
| 104 | + " \n", |
| 105 | + " report = response[['amount', 'bankInfo.bankName', 'orderDescription', 'cardAuthInfo.cardholderName', 'cardAuthInfo.paymentSystem', 'ip']].copy()\n", |
| 106 | + " report['Date'] = response['authDateTime'].apply(lambda x: datetime.fromtimestamp(x // 1000).isoformat(sep='T'))\n", |
| 107 | + " report['customterEmail'] = response['merchantOrderParams'].apply(lambda x: x[0]['value'])\n", |
| 108 | + " report['amount'] = report['amount'].apply(lambda x: float(x) / 100)\n", |
| 109 | + " \n", |
| 110 | + " report.rename(index=str, columns={'amount': 'AMOUNT',\n", |
| 111 | + " 'bankInfo.bankName': 'BANK_NAME',\n", |
| 112 | + " 'orderDescription': 'ORDER_NUMBER',\n", |
| 113 | + " 'cardAuthInfo.cardholderName': 'CARDHOLDER_NAME',\n", |
| 114 | + " 'cardAuthInfo.paymentSystem': 'PAYMENT_SYSTEM',\n", |
| 115 | + " 'ip': 'IP_ADDRESS',\n", |
| 116 | + " 'Date': 'DATE',\n", |
| 117 | + " 'customterEmail': 'CUSTOMER_EMAIL'},\n", |
| 118 | + " inplace = True)\n", |
| 119 | + " \n", |
| 120 | + " text_to_send = text_to_send + report.to_string()\n", |
| 121 | + " print(text_to_send)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 4, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "name": "stderr", |
| 131 | + "output_type": "stream", |
| 132 | + "text": [ |
| 133 | + "send: 'ehlo [email protected]\\r\\n'\n", |
| 134 | + "reply: b'250-smtp4p.mail.yandex.net\\r\\n'\n", |
| 135 | + "reply: b'250-8BITMIME\\r\\n'\n", |
| 136 | + "reply: b'250-PIPELINING\\r\\n'\n", |
| 137 | + "reply: b'250-SIZE 42991616\\r\\n'\n", |
| 138 | + "reply: b'250-AUTH LOGIN PLAIN XOAUTH2\\r\\n'\n", |
| 139 | + "reply: b'250-DSN\\r\\n'\n", |
| 140 | + "reply: b'250 ENHANCEDSTATUSCODES\\r\\n'\n", |
| 141 | + "reply: retcode (250); Msg: b'smtp4p.mail.yandex.net\\n8BITMIME\\nPIPELINING\\nSIZE 42991616\\nAUTH LOGIN PLAIN XOAUTH2\\nDSN\\nENHANCEDSTATUSCODES'\n", |
| 142 | + "send: 'AUTH PLAIN AHNtYXJ0LnVuaXZlcnNpdHkubWFpbGVyQHlhbmRleC5ydQBTbWFydEVkdWNhdGlvbg==\\r\\n'\n", |
| 143 | + "reply: b'235 2.7.0 Authentication successful.\\r\\n'\n", |
| 144 | + "reply: retcode (235); Msg: b'2.7.0 Authentication successful.'\n", |
| 145 | + "send: 'mail FROM:<[email protected]> size=246\\r\\n'\n", |
| 146 | + "reply: b'250 2.1.0 <[email protected]> ok\\r\\n'\n", |
| 147 | + "reply: retcode (250); Msg: b'2.1.0 <[email protected]> ok'\n", |
| 148 | + "send: 'rcpt TO:<[email protected]>\\r\\n'\n", |
| 149 | + "reply: b'250 2.1.5 <[email protected]> recipient ok\\r\\n'\n", |
| 150 | + "reply: retcode (250); Msg: b'2.1.5 <[email protected]> recipient ok'\n", |
| 151 | + "send: 'rcpt TO:<[email protected]>\\r\\n'\n", |
| 152 | + "reply: b'250 2.1.5 <[email protected]> recipient ok\\r\\n'\n", |
| 153 | + "reply: retcode (250); Msg: b'2.1.5 <[email protected]> recipient ok'\n", |
| 154 | + "send: 'data\\r\\n'\n", |
| 155 | + "reply: b'354 Enter mail, end with \".\" on a line by itself\\r\\n'\n", |
| 156 | + "reply: retcode (354); Msg: b'Enter mail, end with \".\" on a line by itself'\n", |
| 157 | + "data: (354, b'Enter mail, end with \".\" on a line by itself')\n", |
| 158 | + "send: b\"From: SmartU Mailer <[email protected]>\\r\\nBCC: ['[email protected]', '[email protected]']\\r\\nSubject: SmartU Payments from 2018-11-16 00:00 to 2018-11-16 20:00\\r\\n\\r\\nNo completed payments from 2018-11-16 00:00 to 2018-11-16 20:00.\\r\\n.\\r\\n\"\n", |
| 159 | + "reply: b'250 2.0.0 Ok: queued on smtp4p.mail.yandex.net as 1542382880-uCrACe5fTn-fJQWKYvk\\r\\n'\n", |
| 160 | + "reply: retcode (250); Msg: b'2.0.0 Ok: queued on smtp4p.mail.yandex.net as 1542382880-uCrACe5fTn-fJQWKYvk'\n", |
| 161 | + "data: (250, b'2.0.0 Ok: queued on smtp4p.mail.yandex.net as 1542382880-uCrACe5fTn-fJQWKYvk')\n", |
| 162 | + "send: 'quit\\r\\n'\n", |
| 163 | + "reply: b'221 2.0.0 Closing connection.\\r\\n'\n", |
| 164 | + "reply: retcode (221); Msg: b'2.0.0 Closing connection.'\n" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "data": { |
| 169 | + "text/plain": [ |
| 170 | + "(221, b'2.0.0 Closing connection.')" |
| 171 | + ] |
| 172 | + }, |
| 173 | + "execution_count": 4, |
| 174 | + "metadata": {}, |
| 175 | + "output_type": "execute_result" |
| 176 | + } |
| 177 | + ], |
| 178 | + "source": [ |
| 179 | + |
| 180 | + "password = 'SmartEducation'\n", |
| 181 | + |
| 182 | + "subject = 'SmartU Payments from {} to {}'.format(start_formatted, end_formatted)\n", |
| 183 | + "email_text = text_to_send\n", |
| 184 | + "\n", |
| 185 | + "message = 'From: {}\\nTo: {}\\nSubject: {}\\n\\n{}'.format('SmartU Mailer <' + email + '>',\n", |
| 186 | + " dest_email, \n", |
| 187 | + " subject, \n", |
| 188 | + " email_text)\n", |
| 189 | + "\n", |
| 190 | + "server = smtp.SMTP_SSL('smtp.yandex.com')\n", |
| 191 | + "server.set_debuglevel(1)\n", |
| 192 | + "server.ehlo(email)\n", |
| 193 | + "server.login(email, password)\n", |
| 194 | + "server.auth_plain()\n", |
| 195 | + "server.sendmail(email, dest_email, message)\n", |
| 196 | + "server.quit()" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "code", |
| 201 | + "execution_count": null, |
| 202 | + "metadata": {}, |
| 203 | + "outputs": [], |
| 204 | + "source": [] |
| 205 | + } |
| 206 | + ], |
| 207 | + "metadata": { |
| 208 | + "kernelspec": { |
| 209 | + "display_name": "Python 3", |
| 210 | + "language": "python", |
| 211 | + "name": "python3" |
| 212 | + }, |
| 213 | + "language_info": { |
| 214 | + "codemirror_mode": { |
| 215 | + "name": "ipython", |
| 216 | + "version": 3 |
| 217 | + }, |
| 218 | + "file_extension": ".py", |
| 219 | + "mimetype": "text/x-python", |
| 220 | + "name": "python", |
| 221 | + "nbconvert_exporter": "python", |
| 222 | + "pygments_lexer": "ipython3", |
| 223 | + "version": "3.7.0" |
| 224 | + } |
| 225 | + }, |
| 226 | + "nbformat": 4, |
| 227 | + "nbformat_minor": 2 |
| 228 | +} |
0 commit comments