Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
scrapper-portabilidad
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Javier
scrapper-portabilidad
Commits
be2d2b5b
Commit
be2d2b5b
authored
May 05, 2022
by
Javier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Versión Actualizada
parent
71216407
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
37 deletions
+93
-37
__init__.py
a2billing/__init__.py
+0
-0
a2billing.py
a2billing/a2billing.py
+24
-0
cli.py
cli.py
+0
-0
functions.py
mbilling/functions.py
+69
-37
No files found.
a2billing/__init__.py
0 → 100644
View file @
be2d2b5b
a2billing/a2billing.py
0 → 100644
View file @
be2d2b5b
import
mariadb
DB_CREDENTIALS
=
{
'host'
:
'192.168.2.220'
,
'user'
:
'api-mbilling'
,
'password'
:
'mbillingmbilling'
,
'port'
:
3306
,
'database'
:
'mya2billing'
}
def
account_data
(
callerid
:
str
)
->
dict
:
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
cnx
.
cursor
()
as
cursor
:
headers
=
[
'account'
,
'secret'
,
'credit'
]
cursor
.
execute
(
'SELECT car.username, car.uipass, round(car.credit, 2) credit FROM cc_callerid cid
\
INNER JOIN cc_card car ON car.id=cid.id_cc_card WHERE cid.cid=?'
,
(
callerid
,)
)
data
=
cursor
.
fetchone
()
return
dict
(
zip
(
headers
,
data
))
if
data
else
None
cli.py
View file @
be2d2b5b
This diff is collapsed.
Click to expand it.
mbilling/functions.py
View file @
be2d2b5b
from
decimal
import
Decimal
import
subprocess
import
subprocess
import
pathlib
import
pathlib
import
re
import
re
...
@@ -23,6 +24,20 @@ DB_CREDENTIALS = {
...
@@ -23,6 +24,20 @@ DB_CREDENTIALS = {
}
}
class
SipAccount
:
def
__init__
(
self
,
callerid
:
str
,
account
:
str
,
secret
:
str
,
credit
:
float
,
plan
:
int
,
offer
:
int
,
active
:
bool
,
postpaid
:
bool
)
->
None
:
self
.
callerid
=
callerid
self
.
account
=
account
self
.
secret
=
secret
self
.
credit
=
credit
self
.
plan
=
plan
self
.
offer
=
offer
self
.
active
=
active
self
.
postpaid
=
postpaid
class
MagnusCommandError
(
Exception
):
class
MagnusCommandError
(
Exception
):
def
__init__
(
self
,
*
args
:
object
,
)
->
None
:
def
__init__
(
self
,
*
args
:
object
,
)
->
None
:
...
@@ -110,7 +125,7 @@ def _create_did(phone_number: str):
...
@@ -110,7 +125,7 @@ def _create_did(phone_number: str):
raise
MagnusCommandError
(
result
.
decode
(
'utf-8'
))
raise
MagnusCommandError
(
result
.
decode
(
'utf-8'
))
def
_update_payment
(
phone_number
:
str
,
postpay
:
bool
=
False
,
credit
:
float
=
0.0
):
def
_update_payment
(
phone_number
:
str
,
postpay
:
bool
=
False
,
credit
:
Decimal
=
Decimal
(
'0.0'
)
):
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
cnx
.
cursor
()
as
cursor
:
with
cnx
.
cursor
()
as
cursor
:
...
@@ -156,7 +171,7 @@ def create_user(phone_number: str, name: str, surname: str, password: str = None
...
@@ -156,7 +171,7 @@ def create_user(phone_number: str, name: str, surname: str, password: str = None
return
password
return
password
def
add_balance
(
phone_number
:
str
,
quantity
:
float
,
tax
:
float
=
0.21
,
description
:
str
=
'Recarga Automática'
):
def
add_balance
(
phone_number
:
str
,
quantity
:
Decimal
,
tax
:
Decimal
=
Decimal
(
'0.21'
)
,
description
:
str
=
'Recarga Automática'
):
"""Añade saldo a una cuenta SIP
"""Añade saldo a una cuenta SIP
Args:
Args:
...
@@ -193,14 +208,17 @@ def get_user(phone_number: str) -> dict:
...
@@ -193,14 +208,17 @@ def get_user(phone_number: str) -> dict:
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
cnx
.
cursor
()
as
cursor
:
with
cnx
.
cursor
()
as
cursor
:
headers
=
[
'username'
,
'password'
,
'host'
,
'credit'
,
headers
=
[
'sip'
,
'username'
,
'password'
,
'host'
,
'credit'
,
'firstname'
,
'lastname'
,
'plan'
,
'offer'
]
'firstname'
,
'lastname'
,
'plan'
,
'offer'
,
'active'
,
'postpaid'
]
cursor
.
execute
(
cursor
.
execute
(
"""SELECT u.username, s.secret password, s.host, u.credit, u.firstname, u.lastname, p.id plan, o.id offer FROM mbilling.pkg_user u
'SELECT s.name, u.username, s.secret password, s.host, u.credit, u.firstname,
\
INNER JOIN mbilling.pkg_plan p ON u.id_plan=p.id
u.lastname, p.id plan, IFNULL(o.id, 0) offer, u.active active, u.typepaid
\
LEFT JOIN mbilling.pkg_offer o ON u.id_offer=o.id
FROM mbilling.pkg_user u
\
INNER JOIN mbilling.pkg_sip s ON u.username=s.name
INNER JOIN mbilling.pkg_plan p ON u.id_plan=p.id
\
WHERE username=?"""
,
LEFT JOIN mbilling.pkg_offer o ON u.id_offer=o.id
\
INNER JOIN mbilling.pkg_sip s ON u.id=s.id_user
\
WHERE s.name=?'
,
(
phone_number
,)
(
phone_number
,)
)
)
...
@@ -209,7 +227,7 @@ def get_user(phone_number: str) -> dict:
...
@@ -209,7 +227,7 @@ def get_user(phone_number: str) -> dict:
return
dict
(
zip
(
headers
,
query
))
if
query
else
None
return
dict
(
zip
(
headers
,
query
))
if
query
else
None
def
fully_ported_
clis
()
->
'list[str
]'
:
def
fully_ported_
users
()
->
'list[tuple
]'
:
"""Devuelve los CLI completamente portados a MBilling.
"""Devuelve los CLI completamente portados a MBilling.
Algunos usuarios pueden recibir llamadas en MB pero realizar la salida
\
Algunos usuarios pueden recibir llamadas en MB pero realizar la salida
\
...
@@ -224,17 +242,16 @@ def fully_ported_clis() -> 'list[str]':
...
@@ -224,17 +242,16 @@ def fully_ported_clis() -> 'list[str]':
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
cnx
.
cursor
()
as
cursor
:
with
cnx
.
cursor
()
as
cursor
:
cursor
.
execute
(
cursor
.
execute
(
"""
'SELECT s.name cli, u.username account FROM mbilling.pkg_user u
\
SELECT username cli FROM mbilling.pkg_user u
INNER JOIN mbilling.pkg_cdr c ON c.callerid = u.username
\
INNER JOIN mbilling.pkg_cdr c ON c.callerid = u.username
INNER JOIN mbilling.pkg_trunk t ON c.id_trunk = t.id
\
INNER JOIN mbilling.pkg_trunk t ON c.id_trunk = t.id
INNER JOIN mbilling.pkg_sip s ON s.id_user = u.id
\
WHERE c.id_trunk is not NULL AND t.trunkcode != 'a2billing'
WHERE c.id_trunk is not NULL AND t.trunkcode !=
\'
a2billing
\'
AND u.active=1
\
GROUP BY username;
GROUP BY s.name ORDER BY u.username'
)
"""
)
query
=
[
cli
[
0
]
for
cli
in
cursor
.
fetchall
()]
data
=
cursor
.
fetchall
()
return
query
return
data
def
active_users
()
->
'list[dict]'
:
def
active_users
()
->
'list[dict]'
:
...
@@ -244,25 +261,14 @@ def active_users() -> 'list[dict]':
...
@@ -244,25 +261,14 @@ def active_users() -> 'list[dict]':
list[dict]: Listado de Usuarios y sus Datos
list[dict]: Listado de Usuarios y sus Datos
"""
"""
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
accounts
=
[]
with
cnx
.
cursor
()
as
cursor
:
for
account
in
fully_ported_users
():
headers
=
[
'username'
,
'password'
,
'host'
,
'credit'
,
accounts
.
append
(
get_user
(
account
[
0
]))
'firstname'
,
'lastname'
,
'plan'
,
'offer'
,
'active'
,
'postpaid'
]
cursor
.
execute
(
"""SELECT u.username, s.secret password, s.host, u.credit, u.firstname, u.lastname,
\
p.id plan_id, o.id offer_id, u.active, u.typepaid
FROM mbilling.pkg_user u
INNER JOIN mbilling.pkg_plan p ON u.id_plan=p.id
INNER JOIN mbilling.pkg_sip s ON u.username=s.name
LEFT JOIN mbilling.pkg_offer o ON u.id_offer=o.id"""
)
query
=
[
dict
(
zip
(
headers
,
account
))
return
accounts
for
account
in
cursor
.
fetchall
()]
return
query
def
get_cdrs
(
phone_number
:
str
,
since
:
datetime
=
None
,
upto
:
datetime
=
None
)
->
'list[dict]'
:
def
get_cdrs
(
phone_number
:
str
,
since
:
datetime
=
None
,
upto
:
datetime
=
None
)
->
'list[dict]'
:
"""Obtiene los CDRs de una Línea
"""Obtiene los CDRs de una Línea
Args:
Args:
...
@@ -278,7 +284,8 @@ def get_cdrs(phone_number : str, since : datetime = None, upto : datetime = None
...
@@ -278,7 +284,8 @@ def get_cdrs(phone_number : str, since : datetime = None, upto : datetime = None
with
cnx
.
cursor
()
as
cursor
:
with
cnx
.
cursor
()
as
cursor
:
# Se eliminan llamadas a 016
# Se eliminan llamadas a 016
cursor
.
execute
(
'DELETE FROM mbilling.pkg_cdr WHERE calledstation IN (
\'
016
\'
,
\'
34016
\'
)'
)
cursor
.
execute
(
'DELETE FROM mbilling.pkg_cdr WHERE calledstation IN (
\'
016
\'
,
\'
34016
\'
)'
)
# Carga de CDRs
# Carga de CDRs
if
upto
and
since
:
if
upto
and
since
:
...
@@ -313,8 +320,33 @@ def get_cdrs(phone_number : str, since : datetime = None, upto : datetime = None
...
@@ -313,8 +320,33 @@ def get_cdrs(phone_number : str, since : datetime = None, upto : datetime = None
data
=
cursor
.
fetchall
()
data
=
cursor
.
fetchall
()
cdr
=
[]
cdr
=
[]
headers
=
[
'callerid'
,
'destination'
,
'description'
,
'timestamp'
,
'duration'
,
'price'
,
'cost'
]
headers
=
[
'callerid'
,
'destination'
,
'description'
,
'timestamp'
,
'duration'
,
'price'
,
'cost'
]
for
line
in
data
:
for
line
in
data
:
cdr
.
append
(
dict
(
zip
(
headers
,
line
)))
cdr
.
append
(
dict
(
zip
(
headers
,
line
)))
return
cdr
return
cdr
def
get_sip_accounts
(
only_postpaid
:
bool
=
False
)
->
'list[SipAccount]'
:
"""Obtiene todas las cuentas SIPs de MagnusBilling
Returns:
list[SipAccount]: Listado de las Cuentas
"""
query
=
"SELECT s.callerid sip, u.username acc, s.secret secret, round(u.credit, 2) credit, u.id_plan plan,
\
IFNULL(u.id_offer, 0) offer, u.active active, u.typepaid postpaid
\
FROM mbilling.pkg_sip s
\
INNER JOIN mbilling.pkg_user u ON u.id=s.id_user
\
WHERE s.callerid > ''"
if
only_postpaid
:
query
+=
' AND u.typepaid=1'
with
mariadb
.
connect
(
**
DB_CREDENTIALS
)
as
cnx
:
with
cnx
.
cursor
()
as
cursor
:
cursor
.
execute
(
query
)
sip_data
=
cursor
.
fetchall
()
return
[
SipAccount
(
*
sip
)
for
sip
in
sip_data
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment