weather.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. import json
  3. import subprocess
  4. import sys, os
  5. from pathlib import Path
  6. home = str(Path.home())
  7. lokfile = home + "/.config/waybar/location"
  8. import gi
  9. gi.require_version("Gtk", "3.0")
  10. from gi.repository import Gtk, GLib, Gdk
  11. class EntryWindow(Gtk.Window):
  12. def __init__(self):
  13. super().__init__(title="Hava Durumu Şehri")
  14. self.set_size_request(200, 100)
  15. self.timeout_id = None
  16. vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  17. self.add(vbox)
  18. self.entry = Gtk.Entry()
  19. sehir = ""
  20. with open(lokfile,"r") as f:
  21. sehir = f.read().strip()
  22. self.entry.set_text(sehir)
  23. vbox.pack_start(self.entry, True, True, 0)
  24. button = Gtk.Button.new_with_label("Değiştir")
  25. button.connect("clicked", self.duzenle)
  26. self.entry.connect("key-press-event",self.key_press)
  27. vbox.pack_start(button, True, True, 0)
  28. def sehir_ayarla(self,sehir):
  29. with open(lokfile,"w") as f:
  30. f.write(sehir)
  31. sys.exit()
  32. def duzenle(self, button):
  33. sehir = self.entry.get_text()
  34. self.sehir_ayarla(sehir)
  35. def key_press(self, widget, event):
  36. key_name = Gdk.keyval_name(event.keyval)
  37. if key_name == "Return":
  38. sehir = self.entry.get_text()
  39. self.sehir_ayarla(sehir)
  40. if len(sys.argv) > 1 and sys.argv[1] == "--girdi":
  41. win = EntryWindow()
  42. #win.connect("destroy", Gtk.main_quit)
  43. win.connect("delete-event", Gtk.main_quit)
  44. win.show_all()
  45. Gtk.main()
  46. sys.exit()
  47. WEATHER_CODES = {
  48. '113': '',
  49. '116': '',
  50. '119': '',
  51. '122': '️',
  52. '143': '',
  53. '176': '',
  54. '179': '',
  55. '182': '',
  56. '185': '',
  57. '200': '',
  58. '227': '',
  59. '230': '',
  60. '248': '',
  61. '260': '',
  62. '263': '',
  63. '266': '',
  64. '281': '',
  65. '284': '',
  66. '293': '',
  67. '296': '',
  68. '299': '',
  69. '302': '',
  70. '305': '',
  71. '308': '',
  72. '311': '',
  73. '314': '',
  74. '317': '',
  75. '320': '',
  76. '323': '',
  77. '326': '',
  78. '329': '️',
  79. '332': '',
  80. '335': '️',
  81. '338': '',
  82. '350': '',
  83. '353': '',
  84. '356': '',
  85. '359': '',
  86. '362': '',
  87. '365': '',
  88. '368': '',
  89. '371': '️️',
  90. '374': '',
  91. '377': '',
  92. '386': '',
  93. '389': '',
  94. '392': '',
  95. '395': '️'
  96. }
  97. location = "Istanbul"
  98. with open(lokfile,"r") as f:
  99. location = f.read().strip()
  100. komut = "curl -s https://tr.wttr.in/{}?format=j1".format(location)
  101. #print("-",komut)
  102. result = subprocess.check_output(komut, shell=True).strip()
  103. data={}
  104. try:
  105. weather = json.loads(result)
  106. temp = weather["current_condition"][0]["temp_C"]
  107. code = weather["current_condition"][0]["weatherCode"]
  108. stat = weather["current_condition"][0]["lang_tr"][0]["value"]
  109. data["text"] = "{} {}°C".format(WEATHER_CODES[code], temp)
  110. except:
  111. data["text"] = "error"
  112. data["tooltip"] = location
  113. print(json.dumps(data))