loading
open in
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
############################################################
# This code displays random verse from the Quran both in   #
# Arabic original and English translation (Pickthall)      #
# -------------------------------------------------------- #
# API from http://api.alquran.cloud                        #
# Author: Abdul Baqi                                       #
# Date: Oct 2018                                           #
# ##########################################################

import random
import requests
from termcolor import colored

# This function takes a random number
# between 1 and 6237 (which are the total number of verses in the Quran
# and returns:
# verse_a : verse in arabic
# verse_en: verse in English (Pickthall translation)
# sura: Name of surah in english, then number of sura and after : the number of verse
# example: Taa-Haa(20):108

def bring_verse(verse):
    url = 'http://api.alquran.cloud/ayah/'+str(verse)+'/editions/quran-uthmani,en.pickthall'
    json_data = requests.get(url).json()
    verse_a = json_data['data'][0]['text']
    verse_en = json_data['data'][1]['text']
    sura = json_data['data'][0]['surah']['englishName']+\
           '('+str(json_data['data'][0]['surah']['number'])+'):'+\
           str(json_data['data'][0]['numberInSurah'])
    return [verse_a,verse_en,sura]

while True:
    verse = input(">>press enter for a random verse (press 'q' to quit): ")
    if verse == 'q':
        print('Thank you, come again')
        break
    
    aya = random.randint(1,6237)
    link = bring_verse(aya)
    print(colored('------------', 'green'))
    print(link[0])
    print('\n')
    print(link[1])
    print('\n')
    print(link[2])
    print(colored('------------', 'green'))
    
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux