Advertisement
GodfatherX64

Untitled

Mar 6th, 2024
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.39 KB | Software | 0 0
  1. package Plugins::AladhanPlugin;
  2.  
  3. use strict;
  4. use LWP::Simple;
  5. use JSON;
  6.  
  7. # Define a variable to track whether audio is currently playing
  8. my $audio_playing = 0;
  9.  
  10. # Define variables to store prayer times
  11. my %prayer_times_cache;
  12.  
  13. sub new {
  14.     my $class = shift;
  15.     my $self = {};
  16.     bless $self, $class;
  17.     return $self;
  18. }
  19.  
  20. sub init {
  21.     # Initialization code for the plugin
  22.     load_prayer_times();  # Load prayer times from cache
  23. }
  24.  
  25. sub start {
  26.     while (1) {
  27.         my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
  28.         $mon += 1;
  29.         $year += 1900;
  30.  
  31.         my $url = "https://api.aladhan.com/v1/timingsByCity/$mday-$mon-$year?city=Alexandria&country=Egypt";
  32.         my $response = get($url);
  33.  
  34.         if ($response) {
  35.             my $data = decode_json($response);
  36.             my $prayer_times = $data->{'data'}->{'timings'};
  37.  
  38.             # Store prayer times in cache
  39.             %prayer_times_cache = %$prayer_times;
  40.  
  41.             save_prayer_times();  # Save prayer times to cache
  42.  
  43.             # Check if the current time matches any prayer time
  44.             if ($hour == $fajr_hour && $min == $fajr_minute) {
  45.                 if (!$audio_playing) {
  46.                     stop_audio();  # Stop any currently playing audio
  47.                     play_adhan("Fajr");
  48.                     $audio_playing = 1;
  49.                 }
  50.             }
  51.             # Add similar checks for other prayer times...
  52.         } else {
  53.             # If API request fails, load prayer times from cache
  54.             load_prayer_times();
  55.         }
  56.  
  57.         sleep(60);  # Check every minute
  58.     }
  59. }
  60.  
  61. sub play_adhan {
  62.     my $prayer_name = shift;
  63.     # Code to play Adhan based on prayer name
  64. }
  65.  
  66. sub stop_audio {
  67.     # Code to stop currently playing audio
  68. }
  69.  
  70. sub save_prayer_times {
  71.     # Save prayer times to a local cache file
  72.     open(my $fh, '>', 'prayer_times_cache.json') or die "Could not open file 'prayer_times_cache.json' $!";
  73.     print $fh encode_json(\%prayer_times_cache);
  74.     close $fh;
  75. }
  76.  
  77. sub load_prayer_times {
  78.     # Load prayer times from the local cache file
  79.     if (-e 'prayer_times_cache.json') {
  80.         open(my $fh, '<', 'prayer_times_cache.json') or die "Could not open file 'prayer_times_cache.json' $!";
  81.         my $json_str = do { local $/; <$fh> };
  82.         close $fh;
  83.  
  84.         %prayer_times_cache = %{decode_json($json_str)};
  85.     }
  86. }
  87.  
  88. 1;
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement