Home > Blogosphere > Kumat Mrogram, bikin laporan tren saldo pulsa SMS Gateway Gammu menggunakan Python

Kumat Mrogram, bikin laporan tren saldo pulsa SMS Gateway Gammu menggunakan Python


Pagi ini berniat untuk membuat laporan Tren Penggunaan Pulsa dari SMS Gateway Sistem Informasi Akademik Universitas Lampung yang digunakan untuk mengirimkan Token pada saat transaksi akademik berjalan, setelah mikir 3 jam plus ngerjain hana hini yang lain, jadilah program “bar bar” tren pulsanya si Gammu SMS-Gateway Server.

Pertama kita buat system cronjob menggunakan BASH Script untuk secara periodik si GAMMU mengecek pulsa melalui modem, programnya adalah Sbb (file sms.sh);

#!/bin/sh
/etc/init.d/gammu-smsd stop &&
sleep 2
sleep 2
/etc/init.d/gammu-smsd stop &&
/bin/echo  "CEK TERAKHIR:"  > /xyz/www/saldo.txt &&
/bin/date >> /xzy/www/saldo.txt &&
/usr/bin/gammu getussd *888# >> /xyz/www/saldo.txt &&
sleep 900
/etc/init.d/gammu-smsd start &&
exit 0

Program ini tinggal dijalankan di cronjob setiap berapa Jam/Menit sekali, lalu buat program parsing datanya, misal baca.py yg isinya;

##By-Gigih-F, iseng-iseng aja : 13-10-2015 09:14 WIB)
 import matplotlib
 matplotlib.use('Agg')
 import MySQLdb as mdb
 import sys
 import matplotlib.pyplot as plt
 import datetime
 now = datetime.datetime.now()
 f=open('saldo.txt')
 lines=f.readlines()
 string = lines[4]
 words = ['pulsa ', 'sd']
 def method(string, words, end_word):
 segments = string.split(end_word)
 counter = 0
 while counter < len(words):
 data = segments[counter].split(words[counter])[-1]
 counter += 1
 yield data.strip()
 for r in method(string,words, '.'):
 print r
 text_file = open("pulsa.txt", "w")
 text_file.write(r)
 text_file.close()
 f = open('pulsa.txt').readlines()[0]
 baca= int(f)
 print baca
 print now 
 conn = mdb.connect("localhost", "UserNameDB", "XXyyyZZZ", "cek_pulsa");
 with conn:
 cursor = conn.cursor ()
 cursor.execute ("SELECT * FROM saldo")
 cursor.execute ("""INSERT INTO saldo(tanggal,pulsa) VALUES(%s,"%s")""" , (now,baca))
 cursor.close()
 conn = mdb.connect('localhost', 'UserNameDB', 'XXyyyZZZ', 'cek_pulsa');
 with conn:
 cursor = conn.cursor ()
 cursor.execute ("SELECT saldo.tanggal, saldo.pulsa  FROM saldo  ")
 row = cursor.fetchall()
 print "RESULT:", row[0]
 waktu, user=zip(*row) #Fetching setiap row 
 fig = matplotlib.pyplot.gcf()
 fig.set_size_inches(18.5,10.5)
 ax = plt.subplot(111)
 plt.plot(waktu, user,marker='+',linestyle='-',color='c',label='SALDO PULSA SMS GATEWAY',lw=1)
 plt.fill_between(waktu, user,color='c',alpha=.4) 
 plt.legend(loc=10)
 plt.ylabel('Pulsa (Dalam Rupiah)')  #Label pada sumbu Y
 plt.xlabel('Tanggal')  #Label pada sumbu X
 plt.grid()
 plt.title('TREN SALDO PULSA SMS GATEWAY SIAKAD : [%s] (Credit to Python+MySQL)'%(now))  #Judul Grafik
 plt.savefig('/xxx/yyy/pulsa.png', dpi=100) #Simpan file dengan judul xxx.png
 #plt.show() #Tampilkan hasil script

Ok Selesai, nanti kurang lebihnya tampilan tren penggunaan pulsanya adalah seperti ini;

pulsa

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: