Архиватор снимков неба над Томском панорамной камерой http://tomsk-sky.blindage.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get-sky.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright by Vladimir Smagin, 2019
  2. # Home page: http://blindage.org
  3. # Source code: https://git.blindage.org/21h/tomsk-sky-archiver
  4. import psycopg2
  5. import requests
  6. import sys, os
  7. import re
  8. import uuid, random
  9. from datetime import datetime
  10. import logging
  11. try:
  12. logging.basicConfig(filename='sky.log', level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
  13. except Exception as e:
  14. print("Error while logging init:", e)
  15. sys.exit(1)
  16. def generate_filename(timestamp):
  17. return timestamp+".jpg"
  18. def generate_date(timestamp):
  19. return str(timestamp.date())
  20. def save_data(picture_data, timestamp, timestampDT):
  21. datadir_path = "datadir/"+generate_date(timestampDT)
  22. filename = generate_filename(timestamp)
  23. logging.debug("Datadir: %s, Generated filename: %s" % (datadir_path, filename))
  24. try:
  25. os.stat(datadir_path)
  26. except:
  27. os.mkdir(datadir_path)
  28. with open(datadir_path+"/"+filename, 'wb') as f:
  29. f.write(picture_data)
  30. return filename
  31. def db_connect():
  32. try:
  33. DBconnection = psycopg2.connect(user = "postgres",
  34. password = "postgres",
  35. host = "127.0.0.1",
  36. port = "5432",
  37. database = "tomsk-sky")
  38. except (Exception, psycopg2.Error) as error :
  39. print ("Error while connecting to PostgreSQL", error)
  40. sys.exit(1)
  41. return DBconnection
  42. # db_get_last: id, filename, photo_timestamp, precipitation, comment
  43. def db_get_last(DBconnection):
  44. try:
  45. cursor = DBconnection.cursor()
  46. cursor.execute("SELECT * from pictures order by photo_timestamp desc limit 1")
  47. record = cursor.fetchone()
  48. except (Exception, psycopg2.Error) as error :
  49. logging.critical("Error while fetching last record %s" % error)
  50. sys.exit(1)
  51. return record
  52. # db_insert_new: filename, photo_timestamp, precipitation, comment
  53. def db_insert_new(DBconnection, newRecord):
  54. try:
  55. cursor = DBconnection.cursor()
  56. postgres_insert_query = """ INSERT INTO pictures (filename, photo_timestamp, precipitation, comment) VALUES (%s, %s, %s, %s); """
  57. cursor.execute(postgres_insert_query, newRecord)
  58. DBconnection.commit()
  59. except (Exception, psycopg2.Error) as error :
  60. logging.critical("Error while inserting new record %s" % error)
  61. sys.exit(1)
  62. dataInternetURL = "http://sky.iao.ru/data/SkyImg.jpg"
  63. dataInternetINFO = "http://sky.iao.ru/data.php"
  64. DBconn = db_connect()
  65. # get last from DB
  66. dataLocal = db_get_last(DBconn)
  67. logging.debug(dataLocal)
  68. # get last from internet
  69. try:
  70. r = requests.get(dataInternetINFO)
  71. if r.status_code != 200:
  72. raise Exception("Status code not 200")
  73. dataInternetUnixTimestamp = re.search(r'\d+', r.text).group(0)
  74. dataInternetDT = datetime.utcfromtimestamp(int(dataInternetUnixTimestamp))
  75. logging.debug("Internet: unix timestamp: %s" % dataInternetUnixTimestamp)
  76. dataInternetTextR = re.search(r'(?s)(?<=\<pre\>)(.*?)(?=\<\/pre\>)', r.text).group(0)
  77. dataInternetText = "\n".join([ll.rstrip() for ll in dataInternetTextR.splitlines() if ll.strip()])
  78. logging.debug("Internet: text\n%s" % dataInternetText)
  79. dataInternetPrecipitation = False
  80. if "Есть осадки" in dataInternetText:
  81. dataInternetPrecipitation = True
  82. logging.debug("Internet: Precipitation %s" % dataInternetPrecipitation)
  83. except Exception as e:
  84. logging.critical("Error while receiving info: %s" % e)
  85. sys.exit(1)
  86. # check difference
  87. logging.info("Timestamps: local %s remote %s" % (dataLocal[2], dataInternetDT))
  88. if dataLocal[2] < dataInternetDT:
  89. try:
  90. picreq = requests.get(dataInternetURL)
  91. if picreq.status_code != 200:
  92. raise Exception("Status code not 200")
  93. filename = save_data(picreq.content, dataInternetUnixTimestamp, dataInternetDT)
  94. db_insert_new(DBconn, (filename, dataInternetDT, dataInternetPrecipitation, dataInternetText))
  95. logging.info("Remote picture saved")
  96. except Exception as e:
  97. logging.critical("Error while saving picture: %s" % e)
  98. sys.exit(1)