cron installation now works on regular user

This commit is contained in:
a-Sansara 2017-10-28 03:59:37 +02:00
parent aebeaa5939
commit 0405e3be0e
5 changed files with 170 additions and 116 deletions

View File

@ -1,6 +1,6 @@
# PrayTime # PrayTime
PrayTime is a small vala program which find timings of Islamic Prayer PrayTime is a small program written in vala which find timings of Islamic Prayer
and play the adhan at the given time. and play the adhan at the given time.
PrayTime use the api on aladhan.com to retriew timings. PrayTime use the api on aladhan.com to retriew timings.
@ -21,7 +21,7 @@ gstreamer_dep = dependency('gstreamer-1.0')
on debian or debian like you can do : on debian or debian like you can do :
``` ```
$ sudo apt-get install valac libjson-glib-dev libgstreamer1.0-dev meson ninja-build $ sudo apt-get install valac libjson-glib-dev libgstreamer1.0-dev libgstreamer0.10-dev meson ninja-build
``` ```
## Install ## Install
@ -68,8 +68,6 @@ isha =
[Cron] [Cron]
# timings updating time # timings updating time
time = 00:00 time = 00:00
# cron file
path = /etc/cron.d/praytime
``` ```
## Usage ## Usage
@ -79,22 +77,31 @@ First step is to edit configuration file
/usr/share/praytime.praytime.ini /usr/share/praytime.praytime.ini
``` ```
and set your city & location, then add some adhan file. Set your city & location, then add some adhan file
After that you can initialise the cron installation with After that you can initialise the cron installation with
``` ```
$ sudo praytime cron $ praytime cron
updating crontab a-sansara : ok
__________________________________________________________
____ _______
/ __ \_________ ___ _/_ __(_____ ___ ___
/ /_/ / ___/ __ `/ / / // / / / __ `__ \/ _ \
/ ____/ / / /_/ / /_/ // / / / / / / / / __/
/_/ /_/ \__,_/\__, //_/ /_/_/ /_/ /_/\___/
by a-sansara /____/ gnu gpl v3
Paris FR +0200 Saturday 28 October 2017 03:46:55
__________________________________________________________
Fajr : 06:45
Dhuhr : 13:34
Asr : 16:10
Maghrib : 18:37
Isha : 20:17
updating /etc/cron.d/praytime : ok
----------------------------------------------------------
Paris FR - +0200 Thursday 19 October 2017 02:17:12
----------------------------------------------------------
Fajr : 06:32
Dhuhr : 13:36
Asr : 16:23
Maghrib : 18:53
Isha : 20:32
``` ```
you can test adhan with : you can test adhan with :

View File

@ -30,7 +30,3 @@ isha =
[Cron] [Cron]
# timings updating time # timings updating time
time = 00:00 time = 00:00
# crontab user playing adhan
user = a-sansara
# crontab update time - root required
path = /etc/cron.d/praytime

View File

@ -13,8 +13,8 @@ int main (string[] argv)
p.init_cron (); p.init_cron ();
break; break;
case "timings" : case "version" :
stdout.printf ("@VERSION@");
break; break;
case "play" : case "play" :
@ -26,6 +26,40 @@ int main (string[] argv)
return 1; return 1;
} }
break; break;
default :
string usage = """%s%s%s usage :
%s# update user crontab
# before installing please check config file
# @DATA_PATH@/praytime.ini%s
praytime %scron%s
# display program version%s
praytime %sversion%s
# play adhan (Fajr, Dhuhr, Asr, Maghrib, Isha)%s
praytime %splay %sPRAYER_NAME%s
# display prayer timings%s
praytime%s
""";
stderr.printf (
usage,
PrayTime.COLOR1,
PrayTime.HEADER,
PrayTime.COLOR6,
PrayTime.COLOR5,
PrayTime.COLOR3,
PrayTime.COLOR1,
PrayTime.COLOR5,
PrayTime.COLOR3,
PrayTime.COLOR1,
PrayTime.COLOR5,
PrayTime.COLOR3,
PrayTime.COLOR1,
PrayTime.COLOR4,
PrayTime.COLOR5,
PrayTime.COLOR3,
PrayTime.COLOR_OFF
);
break;
} }
} }
else { else {

View File

@ -6,7 +6,7 @@ gio_dep = dependency('gio-2.0')
json_dep = dependency('json-glib-1.0') json_dep = dependency('json-glib-1.0')
gstreamer_dep = dependency('gstreamer-1.0') gstreamer_dep = dependency('gstreamer-1.0')
version = '0.2.2' version = '0.2.4'
bindir = join_paths(get_option('prefix'), get_option('bindir')) bindir = join_paths(get_option('prefix'), get_option('bindir'))
datadir = join_paths(get_option('prefix'), get_option('datadir'), 'praytime') datadir = join_paths(get_option('prefix'), get_option('datadir'), 'praytime')

View File

@ -3,19 +3,35 @@ using Gst;
class Pluie.PrayTime : GLib.Object class Pluie.PrayTime : GLib.Object
{ {
const bool DEBUG = false; public const string HEADER = """ ____ _______
const string SEP = "----------------------------------------------------------"; / __ \_________ ___ _/_ __(_____ ___ ___
const string[] PRAYLIST = { "Fajr", "Dhuhr", "Asr", "Maghrib", "Isha" }; / /_/ / ___/ __ `/ / / // / / / __ `__ \/ _ \
const string protocol = "http"; / ____/ / / /_/ / /_/ // / / / / / / / / __/
const string hostname = "api.aladhan.com"; /_/ /_/ \__,_/\__, //_/ /_/_/ /_/ /_/\___/
const string uri = "timingsByCity?"; by a-sansara /____/ gnu gpl v3
private string path; """;
private string version; public const string COLOR1 = "\033[1;38;5;36m";
private string bin; public const string COLOR2 = "\033[1;38;5;97m";
private GLib.KeyFile kf; public const string COLOR3 = "\033[1;38;5;255m";
private GLib.MainLoop loop; public const string COLOR4 = "\033[1;38;5;157m";
private Gst.Element playbin; public const string COLOR5 = "\033[1;38;5;67m";
public const string COLOR6 = "\033[1;38;5;193m";
public const string COLOR_OFF = "\033[m";
private const bool DEBUG = false;
private const string SEP = "__________________________________________________________\n";
private const string[] PRAYLIST = { "Fajr", "Dhuhr", "Asr", "Maghrib", "Isha" };
private const string protocol = "http";
private const string hostname = "api.aladhan.com";
private const string uri = "timingsByCity?";
private string path;
private string version;
private string bin;
private GLib.KeyFile kf;
private GLib.MainLoop loop;
private Gst.Element playbin;
public PrayTime (string path, string bin, string version) public PrayTime (string path, string bin, string version)
@ -32,7 +48,22 @@ class Pluie.PrayTime : GLib.Object
if (pray in PrayTime.PRAYLIST) { if (pray in PrayTime.PRAYLIST) {
double volume = this.get_volume (pray.down ()); double volume = this.get_volume (pray.down ());
string mp3 = this.get_mp3 (pray.down ()); string mp3 = this.get_mp3 (pray.down ());
stdout.printf (
"%s%s%s %s %s time%s \n",
PrayTime.COLOR1,
PrayTime.SEP,
PrayTime.HEADER,
PrayTime.COLOR6,
pray,
PrayTime.COLOR_OFF
);
this.play (mp3, volume); this.play (mp3, volume);
stdout.printf (
"%s%s%s\n",
PrayTime.COLOR1,
PrayTime.SEP,
PrayTime.COLOR_OFF
);
} }
else { else {
this.on_error(@"invalid pray parameter '$pray'"); this.on_error(@"invalid pray parameter '$pray'");
@ -42,19 +73,35 @@ class Pluie.PrayTime : GLib.Object
public void infos() public void infos()
{ {
KeyFile k = this.load_config ("praytime.daily.ini"); KeyFile k = this.load_config ("praytime.daily.ini", true);
var date = new GLib.DateTime.now_local (); var date = new GLib.DateTime.now_local ();
stdout.printf ( stdout.printf (
"%s\n %s %s - %s\n%s\n", "%s%s%s%s %s %s %s %s %s %s\n%s%s%s\n",
PrayTime.SEP, PrayTime.COLOR1,
PrayTime.SEP,
PrayTime.HEADER,
PrayTime.COLOR6,
this.get_config ("city"), this.get_config ("city"),
this.get_config ("country"), this.get_config ("country"),
date.format ("%z %A %d %B %Y %T"), PrayTime.COLOR3,
PrayTime.SEP date.format ("%z %A %d %B %Y"),
PrayTime.COLOR4,
date.format ("%T"),
PrayTime.COLOR1,
PrayTime.SEP,
PrayTime.COLOR_OFF
); );
foreach (string pray in PrayTime.PRAYLIST) { foreach (string pray in PrayTime.PRAYLIST) {
try { try {
stdout.printf (" %10s : %s\n", pray, k.get_string ("Praytime", pray.down ())); stdout.printf (
" %s%10s%s : %s%s%s\n",
PrayTime.COLOR1,
pray,
PrayTime.COLOR3,
PrayTime.COLOR4,
k.get_string ("Praytime", pray.down ()),
PrayTime.COLOR_OFF
);
} }
catch (GLib.KeyFileError e) { catch (GLib.KeyFileError e) {
this.on_error (e.message); this.on_error (e.message);
@ -92,18 +139,18 @@ class Pluie.PrayTime : GLib.Object
} }
private string get_config_file (string basename) private string get_config_file (string basename, bool tmp)
{ {
return Path.build_filename (this.path, basename); return Path.build_filename (!tmp ? this.path : Environment.get_tmp_dir (), basename);
} }
private KeyFile load_config (string basename) private KeyFile load_config (string basename, bool tmp = false)
{ {
KeyFile f = new KeyFile (); KeyFile f = new KeyFile ();
f.set_list_separator (','); f.set_list_separator (',');
try { try {
f.load_from_file (this.get_config_file (basename), KeyFileFlags.NONE); f.load_from_file (this.get_config_file (basename, tmp), KeyFileFlags.NONE);
} }
catch (KeyFileError e) { catch (KeyFileError e) {
this.on_error (e.message); this.on_error (e.message);
@ -148,6 +195,19 @@ class Pluie.PrayTime : GLib.Object
} }
private int spawn_cmd (string cmd, out string response)
{
int status;
string std_error;
try {
Process.spawn_command_line_sync (cmd, out response, out std_error, out status);
} catch (SpawnError e) {
stderr.printf ("%s\n", e.message);
}
return status;
}
private string get_timings () private string get_timings ()
{ {
string url = "%s://%s/%scity=%s&country=%s&method=%s&latitudeAdjustmentMethod=%s".printf( string url = "%s://%s/%scity=%s&country=%s&method=%s&latitudeAdjustmentMethod=%s".printf(
@ -162,9 +222,6 @@ class Pluie.PrayTime : GLib.Object
var f = File.new_for_uri (url); var f = File.new_for_uri (url);
var response = ""; var response = "";
try { try {
// root user experience problem with that
// don't know why, use curl as alternative
// in get_alt_timings
FileInputStream fs = f.read (); FileInputStream fs = f.read ();
var dis = new DataInputStream (fs); var dis = new DataInputStream (fs);
string line; string line;
@ -183,59 +240,22 @@ class Pluie.PrayTime : GLib.Object
private string get_alt_timings (string url) private string get_alt_timings (string url)
{ {
stdout.printf(" trying alternate method to get timings\n"); stdout.printf(" trying alternate method to get timings\n");
string response = ""; string response;
string std_error; this.spawn_cmd ("curl %s".printf (url), out response);
int status;
try {
Process.spawn_command_line_sync (
"curl "+url,
out response,
out std_error,
out status
);
} catch (SpawnError e) {
stderr.printf ("%s\n", e.message);
}
return response; return response;
} }
private int get_user_crontab_content (string user, out string response) private int get_user_crontab_content (string user, out string response)
{ {
stdout.printf("getting %s crontab content\n", user); return this.spawn_cmd ("crontab -l -u %s".printf (user), out response);
int status;
string std_error;
try {
Process.spawn_command_line_sync (
"crontab -l -u "+user,
out response,
out std_error,
out status
);
} catch (SpawnError e) {
stderr.printf ("%s\n", e.message);
}
return status;
} }
private int install_user_crontab (string user, string path) private int install_user_crontab (string user, string path)
{ {
int status;
string std_error;
string response; string response;
try { return this.spawn_cmd ("crontab %s -u %s".printf (path, user), out response);
Process.spawn_command_line_sync (
"crontab %s %s".printf (path, user),
out response,
out std_error,
out status
);
} catch (SpawnError e) {
stderr.printf ("%s\n", e.message);
}
stdout.printf ("install crontab %s :\n%s\n", user, response);
return status;
} }
@ -245,7 +265,7 @@ class Pluie.PrayTime : GLib.Object
string udata; string udata;
try { try {
if (this.get_user_crontab_content (user, out udata) == 0) { if (this.get_user_crontab_content (user, out udata) == 0) {
var regex = new Regex (Path.build_filename (this.bin, "praytime").escape ()); var regex = new Regex (Path.build_filename (this.bin, "praytime").escape (null));
foreach (string line in udata.split ("\n")) { foreach (string line in udata.split ("\n")) {
if (!regex.match (line) && line != "") { if (!regex.match (line) && line != "") {
data += line + "\n"; data += line + "\n";
@ -258,7 +278,6 @@ class Pluie.PrayTime : GLib.Object
data = null; data = null;
stdout.printf ("Error %s\n", e.message); stdout.printf ("Error %s\n", e.message);
} }
stdout.printf ("crontab %s content :\n%s\n", user, data);
return data; return data;
} }
@ -266,29 +285,27 @@ class Pluie.PrayTime : GLib.Object
private void set_cron (GLib.DateTime date) private void set_cron (GLib.DateTime date)
{ {
try { try {
string bin = Path.build_filename(this.bin, "praytime"); KeyFile k = this.load_config ("praytime.daily.ini", true);
string cron_path = this.get_config ("path", "Cron"); string? user = Environment.get_variable ("SUDO_USER");
string user = this.get_config ("user", "Cron"); if (user == null) {
string[] update = this.get_config ("time", "Cron").split (":", 2); user = Environment.get_variable ("USER");
string content = "# %s\n%d %d * * * root %s cron\n".printf (date.format ("%c"), int.parse (update[1]), int.parse (update[0]) , bin); }
bool done = FileUtils.set_contents (cron_path, content); string[] update = this.get_config ("time", "Cron").split (":", 2);
stdout.printf ("\n updating %s : %s\n", cron_path, done ? "ok" : "ko"); string[] time = null;
if (done) { string bin = Path.build_filename (this.bin, "praytime");
KeyFile k = this.load_config ("praytime.daily.ini"); string cron_path = Path.build_filename (Environment.get_tmp_dir (), "praytime.crontab");
content = "# > autogenerated by %s %s\n".printf(bin, date.format ("%c")); string content = "# > autogenerated by %s %s\n".printf (bin, date.format ("%c")) +
string[] time = null; "%02d %02d * * * %s cron\n".printf (int.parse (update[1]), int.parse (update[0]) , bin);
foreach (string pray in PrayTime.PRAYLIST) { foreach (string pray in PrayTime.PRAYLIST) {
time = k.get_string ("Praytime", pray.down ()).split (":", 2); time = k.get_string ("Praytime", pray.down ()).split (":", 2);
content += "%s %s * * * sh -c \"DISPLAY=:0 %s play %s\"\n".printf (time[1], time[0] , bin, pray); content += "%s %s * * * sh -c \"DISPLAY=:0 %s play %s\"\n".printf (time[1], time[0] , bin, pray);
} }
content += "# < autogenerated by %s\n".printf(bin); content += "# < autogenerated by %s\n".printf(bin);
cron_path = Path.build_filename (Environment.get_tmp_dir (), "praytime.crontab"); content = this.get_user_crontab (ref user, ref content);
content = this.get_user_crontab (ref user, ref content); if (content != null) {
if (content != null) { if (FileUtils.set_contents (cron_path, content)) {
if (FileUtils.set_contents (cron_path, content)) { int status = this.install_user_crontab (user, cron_path);
this.install_user_crontab (user, cron_path); stdout.printf ("\n updating crontab %s : %s\n", user, status == 0 ? "ok" : "ko");
stdout.printf ("\n updating crontab %s : %s\n", user, done ? "ok" : "ko");
}
} }
} }
} }
@ -308,7 +325,7 @@ class Pluie.PrayTime : GLib.Object
data += "%-10s = %s\n".printf (pray.down(), results.get_string_member (pray)); data += "%-10s = %s\n".printf (pray.down(), results.get_string_member (pray));
} }
try { try {
FileUtils.set_contents (this.get_config_file ("praytime.daily.ini"), data); FileUtils.set_contents (this.get_config_file ("praytime.daily.ini", true), data);
} }
catch (GLib.FileError e) { catch (GLib.FileError e) {
stderr.printf ("%s\n", e.message); stderr.printf ("%s\n", e.message);