cc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. """
  2. THis program is capable of converting from one currency to another as of today itself.
  3. It uses the api at fixer.io and then calculates the value of the currency in terms of the other as of today.
  4. """
  5. # https://github.com/chavarera/python-mini-projects/issues
  6. # https://medium.com/@cereblanco/setup-black-and-isort-in-vscode-514804590bf9
  7. # Source: https://fixer.io/quickstart
  8. # Imp read: https://stackoverflow.com/questions/3139879/how-do-i-get-currency-exchange-rates-via-an-api-such-as-google-finance
  9. import requests
  10. import json
  11. import sys
  12. from pprint import pprint
  13. # The below 4 lines bring out the value of currency from the api at fixer.io. I had to register there, the key is unique to me.
  14. url = "http://data.fixer.io/api/latest?access_key=33ec7c73f8a4eb6b9b5b5f95118b2275"
  15. data = requests.get(url).text
  16. data2 = json.loads(data) #brings whether request was successful,timestamp etc
  17. fx = data2["rates"]
  18. currencies = [
  19. "AED : Emirati Dirham,United Arab Emirates Dirham",
  20. "AFN : Afghan Afghani,Afghanistan Afghani",
  21. "ALL : Albanian Lek,Albania Lek",
  22. "AMD : Armenian Dram,Armenia Dram",
  23. "ANG : Dutch Guilder,Netherlands Antilles Guilder,Bonaire,Curaçao,Saba,Sint Eustatius,Sint Maarten",
  24. "AOA : Angolan Kwanza,Angola Kwanza",
  25. "ARS : Argentine Peso,Argentina Peso,Islas Malvinas",
  26. "AUD : Australian Dollar,Australia Dollar,Christmas Island,Cocos (Keeling) Islands,Norfolk Island,Ashmore and Cartier Islands,Australian Antarctic Territory,Coral Sea Islands,Heard Island,McDonald Islands,Kiribati,Nauru",
  27. "AWG : Aruban or Dutch Guilder,Aruba Guilder",
  28. "AZN : Azerbaijan Manat,Azerbaijan Manat",
  29. "BAM : Bosnian Convertible Mark,Bosnia and Herzegovina Convertible Mark",
  30. "BBD : Barbadian or Bajan Dollar,Barbados Dollar",
  31. "BDT : Bangladeshi Taka,Bangladesh Taka",
  32. "BGN : Bulgarian Lev,Bulgaria Lev",
  33. "BHD : Bahraini Dinar,Bahrain Dinar",
  34. "BIF : Burundian Franc,Burundi Franc",
  35. "BMD : Bermudian Dollar,Bermuda Dollar",
  36. "BND : Bruneian Dollar,Brunei Darussalam Dollar",
  37. "BOB : Bolivian Bolíviano,Bolivia Bolíviano",
  38. "BRL : Brazilian Real,Brazil Real",
  39. "BSD : Bahamian Dollar,Bahamas Dollar",
  40. "BTC : Bitcoin,BTC, XBT",
  41. "BTN : Bhutanese Ngultrum,Bhutan Ngultrum",
  42. "BWP : Botswana Pula,Botswana Pula",
  43. "BYN : Belarusian Ruble,Belarus Ruble",
  44. "BYR : Belarusian Ruble,Belarus Ruble",
  45. "BZD : Belizean Dollar,Belize Dollar",
  46. "CAD : Canadian Dollar,Canada Dollar",
  47. "CDF : Congolese Franc,Congo/Kinshasa Franc",
  48. "CHF : Swiss Franc,Switzerland Franc,Liechtenstein,Campione d'Italia,Büsingen am Hochrhein",
  49. "CLF : Chilean Unit of Account",
  50. "CLP : Chilean Peso,Chile Peso",
  51. "CNY : Chinese Yuan Renminbi,China Yuan Renminbi",
  52. "COP : Colombian Peso,Colombia Peso",
  53. "CRC : Costa Rican Colon,Costa Rica Colon",
  54. "CUC : Cuban Convertible Peso,Cuba Convertible Peso",
  55. "CUP : Cuban Peso,Cuba Peso",
  56. "CVE : Cape Verdean Escudo,Cape Verde Escudo",
  57. "CZK : Czech Koruna,Czech Republic Koruna",
  58. "DJF : Djiboutian Franc,Djibouti Franc",
  59. "DKK : Danish Krone,Denmark Krone,Faroe Islands,Greenland",
  60. "DOP : Dominican Peso,Dominican Republic Peso",
  61. "DZD : Algerian Dinar,Algeria Dinar",
  62. "EGP : Egyptian Pound,Egypt Pound,Gaza Strip",
  63. "ERN : Eritrean Nakfa,Eritrea Nakfa",
  64. "ETB : Ethiopian Birr,Ethiopia Birr,Eritrea",
  65. "EUR : Euro,Euro Member Countries,Andorra,Austria,Azores,Baleares (Balearic Islands),Belgium,Canary Islands,Cyprus,Finland,France,French Guiana,French Southern Territories,Germany,Greece,Guadeloupe,Holland (Netherlands),Holy See (Vatican City),Ireland (Eire),Italy,Luxembourg,Madeira Islands,Malta,Monaco,Montenegro,Netherlands",
  66. "FJD : Fijian Dollar,Fiji Dollar",
  67. "FKP : Falkland Island Pound,Falkland Islands (Malvinas) Pound",
  68. "GBP : British Pound,United Kingdom Pound,United Kingdom (UK),England,Northern Ireland,Scotland,Wales,Falkland Islands,Gibraltar,Guernsey,Isle of Man,Jersey,Saint Helena and Ascension,South Georgia and the South Sandwich Islands,Tristan da Cunha",
  69. "GEL : Georgian Lari,Georgia Lari",
  70. "GGP : Guernsey Pound,Guernsey Pound",
  71. "GHS : Ghanaian Cedi,Ghana Cedi",
  72. "GIP : Gibraltar Pound,Gibraltar Pound",
  73. "GMD : Gambian Dalasi,Gambia Dalasi",
  74. "GNF : Guinean Franc,Guinea Franc",
  75. "GTQ : Guatemalan Quetzal,Guatemala Quetzal",
  76. "GYD : Guyanese Dollar,Guyana Dollar",
  77. "HKD : Hong Kong Dollar,Hong Kong Dollar",
  78. "HNL : Honduran Lempira,Honduras Lempira",
  79. "HRK : Croatian Kuna,Croatia Kuna",
  80. "HTG : Haitian Gourde,Haiti Gourde",
  81. "HUF : Hungarian Forint,Hungary Forint",
  82. "IDR : Indonesian Rupiah,Indonesia Rupiah,East Timor",
  83. "ILS : Israeli Shekel,Israel Shekel,Palestinian Territories",
  84. "IMP : Isle of Man Pound,Isle of Man Pound",
  85. "INR : Indian Rupee,India Rupee,Bhutan,Nepal",
  86. "IQD : Iraqi Dinar,Iraq Dinar",
  87. "IRR : Iranian Rial,Iran Rial",
  88. "ISK : Icelandic Krona,Iceland Krona",
  89. "JEP : Jersey Pound,Jersey Pound",
  90. "JMD : Jamaican Dollar,Jamaica Dollar",
  91. "JOD : Jordanian Dinar,Jordan Dinar",
  92. "JPY : Japanese Yen,Japan Yen",
  93. "KES : Kenyan Shilling,Kenya Shilling",
  94. "KGS : Kyrgyzstani Som,Kyrgyzstan Som",
  95. "KHR : Cambodian Riel,Cambodia Riel",
  96. "KMF : Comorian Franc,Comorian Franc",
  97. "KPW : North Korean Won,Korea (North) Won",
  98. "KRW : South Korean Won,Korea (South) Won",
  99. "KWD : Kuwaiti Dinar,Kuwait Dinar",
  100. "KYD : Caymanian Dollar,Cayman Islands Dollar",
  101. "KZT : Kazakhstani Tenge,Kazakhstan Tenge",
  102. "LAK : Lao Kip,Laos Kip",
  103. "LBP : Lebanese Pound,Lebanon Pound",
  104. "LKR : Sri Lankan Rupee,Sri Lanka Rupee",
  105. "LRD : Liberian Dollar,Liberia Dollar",
  106. "LSL : Basotho Loti,Lesotho Loti",
  107. "LTL : Lithuanian litas",
  108. "LVL : Latvia Lats",
  109. "LYD : Libyan Dinar,Libya Dinar",
  110. "MAD : Moroccan Dirham,Morocco Dirham,Western Sahara",
  111. "MDL : Moldovan Leu,Moldova Leu",
  112. "MGA : Malagasy Ariary,Madagascar Ariary",
  113. "MKD : Macedonian Denar,Macedonia Denar",
  114. "MMK : Burmese Kyat,Myanmar (Burma) Kyat",
  115. "MNT : Mongolian Tughrik,Mongolia Tughrik",
  116. "MOP : Macau Pataca,Macau Pataca",
  117. "MRU : Mauritanian Ouguiya,Mauritania Ouguiya",
  118. "MUR : Mauritian Rupee,Mauritius Rupee",
  119. "MVR : Maldivian Rufiyaa,Maldives (Maldive Islands) Rufiyaa",
  120. "MWK : Malawian Kwacha,Malawi Kwacha",
  121. "MXN : Mexican Peso,Mexico Peso",
  122. "MYR : Malaysian Ringgit,Malaysia Ringgit",
  123. "MZN : Mozambican Metical,Mozambique Metical",
  124. "NAD : Namibian Dollar,Namibia Dollar",
  125. "NGN : Nigerian Naira,Nigeria Naira",
  126. "NIO : Nicaraguan Cordoba,Nicaragua Cordoba",
  127. "NOK : Norwegian Krone,Norway Krone,Bouvet Island,Svalbard,Jan Mayen,Queen Maud Land,Peter I Island",
  128. "NPR : Nepalese Rupee,Nepal Rupee,India (unofficially near India-Nepal border)",
  129. "NZD : New Zealand Dollar,New Zealand Dollar,Cook Islands,Niue,Pitcairn Islands,Tokelau",
  130. "OMR : Omani Rial,Oman Rial",
  131. "PAB : Panamanian Balboa,Panama Balboa",
  132. "PEN : Peruvian Sol,Peru Sol",
  133. "PGK : Papua New Guinean Kina,Papua New Guinea Kina",
  134. "PHP : Philippine Peso,Philippines Peso",
  135. "PKR : Pakistani Rupee,Pakistan Rupee",
  136. "PLN : Polish Zloty,Poland Zloty",
  137. "PYG : Paraguayan Guarani,Paraguay Guarani",
  138. "QAR : Qatari Riyal,Qatar Riyal",
  139. "RON : Romanian Leu,Romania Leu",
  140. "RSD : Serbian Dinar,Serbia Dinar",
  141. "RUB : Russian Ruble,Russia Ruble,Tajikistan,Abkhazia,South Ossetia",
  142. "RWF : Rwandan Franc,Rwanda Franc",
  143. "SAR : Saudi Arabian Riyal,Saudi Arabia Riyal",
  144. "SBD : Solomon Islander Dollar,Solomon Islands Dollar",
  145. "SCR : Seychellois Rupee,Seychelles Rupee",
  146. "SDG : Sudanese Pound,Sudan Pound",
  147. "SEK : Swedish Krona,Sweden Krona",
  148. "SGD : Singapore Dollar,Singapore Dollar",
  149. "SHP : Saint Helenian Pound,Saint Helena Pound",
  150. "SLL : Sierra Leonean Leone,Sierra Leone Leone",
  151. "SOS : Somali Shilling,Somalia Shilling",
  152. "SRD : Surinamese Dollar,Suriname Dollar",
  153. "STN : Sao Tomean Dobra,São Tomé and Príncipe Dobra",
  154. "SVC : Salvadoran Colon,El Salvador Colon",
  155. "SYP : Syrian Pound,Syria Pound",
  156. "SZL : Swazi Lilangeni,eSwatini Lilangeni",
  157. "THB : Thai Baht,Thailand Baht",
  158. "TJS : Tajikistani Somoni,Tajikistan Somoni",
  159. "TMT : Turkmenistani Manat,Turkmenistan Manat",
  160. "TND : Tunisian Dinar,Tunisia Dinar",
  161. "TOP : Tongan Pa'anga,Tonga Pa'anga",
  162. "TRY : Turkish Lira,Turkey Lira,North Cyprus",
  163. "TTD : Trinidadian Dollar,Trinidad and Tobago Dollar,Trinidad,Tobago",
  164. "TWD : Taiwan New Dollar,Taiwan New Dollar",
  165. "TZS : Tanzanian Shilling,Tanzania Shilling",
  166. "UAH : Ukrainian Hryvnia,Ukraine Hryvnia",
  167. "UGX : Ugandan Shilling,Uganda Shilling",
  168. "USD : US Dollar,United States Dollar,America,American Samoa,American Virgin Islands,British Indian Ocean Territory,British Virgin Islands,Ecuador,El Salvador,Guam,Haiti,Micronesia,Northern Mariana Islands,Palau,Panama,Puerto Rico,Turks and Caicos Islands,United States Minor Outlying Islands,Wake Island,East Timor",
  169. "UYU : Uruguayan Peso,Uruguay Peso",
  170. "UZS : Uzbekistani Som,Uzbekistan Som",
  171. "VEF : Venezuelan Bolívar,Venezuela Bolívar",
  172. "VND : Vietnamese Dong,Viet Nam Dong",
  173. "VUV : Ni-Vanuatu Vatu,Vanuatu Vatu",
  174. "WST : Samoan Tala,Samoa Tala",
  175. "XAF : Central African CFA Franc BEAC,Communauté Financière Africaine (BEAC) CFA Franc BEAC,Cameroon,Central African Republic,Chad,Congo/Brazzaville,Equatorial Guinea,Gabon",
  176. "XAG : Silver Ounce,Silver",
  177. "XAU : Gold Ounce,Gold",
  178. "XCD : East Caribbean Dollar,East Caribbean Dollar,Anguilla,Antigua and Barbuda,Dominica,Grenada,The Grenadines and Saint Vincent,Montserrat",
  179. "XDR : IMF Special Drawing Rights,International Monetary Fund (IMF) Special Drawing Rights",
  180. "XOF : CFA Franc,Communauté Financière Africaine (BCEAO) Franc,Benin,Burkina Faso,Ivory Coast,Guinea-Bissau,Mali,Niger,Senegal,Togo",
  181. "XPF : CFP Franc,Comptoirs Français du Pacifique (CFP) Franc,French Polynesia,New Caledonia,Wallis and Futuna Islands",
  182. "YER : Yemeni Rial,Yemen Rial",
  183. "ZAR : South African Rand,South Africa Rand,Lesotho,Namibia",
  184. "ZMK : Zambian Kwacha,Zambia Kwacha",
  185. "ZMW : Zambian Kwacha,Zambia Kwacha",
  186. "ZWL : Zimbabwean Dollar,Zimbabwe Dollar",
  187. ]
  188. # The below function calculates the actual conversion
  189. def function1():
  190. query = input(
  191. "Please specify the amount of currency to convert, from currency, to currency (with space in between).\nPress SHOW to see list of currencies available. \nPress Q to quit. \n"
  192. )
  193. if query == "Q":
  194. sys.exit()
  195. elif query == "SHOW":
  196. pprint(currencies)
  197. function1()
  198. else:
  199. qty, fromC, toC = query.split(" ")
  200. fromC = fromC.upper()
  201. toC = toC.upper()
  202. qty = float(round(int(qty), 2))
  203. amount = round(qty * fx[toC] / fx[fromC], 2)
  204. print(f"{qty} of currency {fromC} amounts to {amount} of currency {toC} today")
  205. try:
  206. function1()
  207. except KeyError:
  208. print("You seem to have inputted wrongly, retry!")
  209. function1()