Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Plugins::AladhanPlugin;
- use strict;
- use LWP::Simple;
- use JSON;
- # Define a variable to track whether audio is currently playing
- my $audio_playing = 0;
- # Define variables to store prayer times
- my %prayer_times_cache;
- sub new {
- my $class = shift;
- my $self = {};
- bless $self, $class;
- return $self;
- }
- sub init {
- # Initialization code for the plugin
- load_prayer_times(); # Load prayer times from cache
- }
- sub start {
- while (1) {
- my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
- $mon += 1;
- $year += 1900;
- my $url = "https://api.aladhan.com/v1/timingsByCity/$mday-$mon-$year?city=Alexandria&country=Egypt";
- my $response = get($url);
- if ($response) {
- my $data = decode_json($response);
- my $prayer_times = $data->{'data'}->{'timings'};
- # Store prayer times in cache
- %prayer_times_cache = %$prayer_times;
- save_prayer_times(); # Save prayer times to cache
- # Check if the current time matches any prayer time
- if ($hour == $fajr_hour && $min == $fajr_minute) {
- if (!$audio_playing) {
- stop_audio(); # Stop any currently playing audio
- play_adhan("Fajr");
- $audio_playing = 1;
- }
- }
- # Add similar checks for other prayer times...
- } else {
- # If API request fails, load prayer times from cache
- load_prayer_times();
- }
- sleep(60); # Check every minute
- }
- }
- sub play_adhan {
- my $prayer_name = shift;
- # Code to play Adhan based on prayer name
- }
- sub stop_audio {
- # Code to stop currently playing audio
- }
- sub save_prayer_times {
- # Save prayer times to a local cache file
- open(my $fh, '>', 'prayer_times_cache.json') or die "Could not open file 'prayer_times_cache.json' $!";
- print $fh encode_json(\%prayer_times_cache);
- close $fh;
- }
- sub load_prayer_times {
- # Load prayer times from the local cache file
- if (-e 'prayer_times_cache.json') {
- open(my $fh, '<', 'prayer_times_cache.json') or die "Could not open file 'prayer_times_cache.json' $!";
- my $json_str = do { local $/; <$fh> };
- close $fh;
- %prayer_times_cache = %{decode_json($json_str)};
- }
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement