Browse Source

import

master
Vladimir Smagin 11 months ago
commit
11b9aa4d06
4 changed files with 195 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 92
    0
      get-sky.py
  3. 2
    0
      requirements.txt
  4. 100
    0
      scheme.sql

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
datadir

+ 92
- 0
get-sky.py View File

@@ -0,0 +1,92 @@
import psycopg2
import requests
import sys, os
import re
import uuid, random
from datetime import datetime

def generate_filename(timestamp):
return timestamp+".jpg"

def save_data(picture_data, timestamp):
datadir_path = "datadir"
filename = generate_filename(timestamp)

print("Datadir", datadir_path, "Generated filename:", filename)

try:
os.stat(datadir_path)
except:
os.mkdir(datadir_path)

with open(datadir_path+"/"+filename, 'wb') as f:
f.write(picture_data)
return filename

def db_connect():
try:
DBconnection = psycopg2.connect(user = "postgres",
password = "postgres",
host = "127.0.0.1",
port = "5432",
database = "tomsk-sky")
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
sys.exit(1)

return DBconnection

# db_get_last: id, filename, photo_timestamp, precipitation, comment
def db_get_last(DBconnection):
try:
cursor = DBconnection.cursor()
cursor.execute("SELECT * from pictures order by photo_timestamp desc limit 1")
record = cursor.fetchone()
except (Exception, psycopg2.Error) as error :
print ("Error while fetching last record", error)
sys.exit(1)
return record

# db_insert_new: filename, photo_timestamp, precipitation, comment
def db_insert_new(DBconnection, newRecord):
try:
cursor = DBconnection.cursor()
postgres_insert_query = """ INSERT INTO pictures (filename, photo_timestamp, precipitation, comment) VALUES (%s, %s, %s, %s); """
cursor.execute(postgres_insert_query, newRecord)
DBconnection.commit()
except (Exception, psycopg2.Error) as error :
print ("Error while inserting new record", error)
sys.exit(1)

dataInternetURL = "http://sky.iao.ru/data/SkyImg.jpg"
dataInternetINFO = "http://sky.iao.ru/data.php"

DBconn = db_connect()

# get last from DB
dataLocal = db_get_last(DBconn)
print(dataLocal)

# get last from internet
r = requests.get(dataInternetINFO)
dataInternetUnixTimestamp = re.search(r'\d+', r.text).group(0)
dataInternetDT = datetime.utcfromtimestamp(int(dataInternetUnixTimestamp))
print("Internet: unix timestamp: %s" % dataInternetUnixTimestamp)

dataInternetTextR = re.search(r'(?s)(?<=\<pre\>)(.*?)(?=\<\/pre\>)', r.text).group(0)
dataInternetText = "\n".join([ll.rstrip() for ll in dataInternetTextR.splitlines() if ll.strip()])
print("Internet: text\n%s" % dataInternetText)

dataInternetPrecipitation = False
if "Есть осадки" in dataInternetText:
dataInternetPrecipitation = True
print("Internet: Precipitation", dataInternetPrecipitation)

# check difference

print("Timestamps: local %s remote %s" % (dataLocal[2], dataInternetDT))
if dataLocal[2] < dataInternetDT:
print("Internet picture differs from last local saved")
picreq = requests.get(dataInternetURL)
filename = save_data(picreq.content, dataInternetUnixTimestamp)
db_insert_new(DBconn, (filename, dataInternetDT, dataInternetPrecipitation, dataInternetText))

+ 2
- 0
requirements.txt View File

@@ -0,0 +1,2 @@
psycopg2
requests

+ 100
- 0
scheme.sql View File

@@ -0,0 +1,100 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 11.1 (Debian 11.1-1.pgdg90+1)
-- Dumped by pg_dump version 11.0 (Ubuntu 11.0-1.pgdg18.04+2)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: pictures; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.pictures (
id integer NOT NULL,
filename character varying NOT NULL,
photo_timestamp timestamp without time zone NOT NULL,
precipitation boolean DEFAULT false NOT NULL,
comment text NOT NULL
);


ALTER TABLE public.pictures OWNER TO postgres;

--
-- Name: pictures_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

CREATE SEQUENCE public.pictures_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


ALTER TABLE public.pictures_id_seq OWNER TO postgres;

--
-- Name: pictures_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--

ALTER SEQUENCE public.pictures_id_seq OWNED BY public.pictures.id;


--
-- Name: pictures id; Type: DEFAULT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.pictures ALTER COLUMN id SET DEFAULT nextval('public.pictures_id_seq'::regclass);


--
-- Data for Name: pictures; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.pictures (id, filename, photo_timestamp, precipitation, comment) FROM stdin;
4 1546718369.jpg 2019-01-05 19:59:29 f Дата съемки: 06.01.2019\nВремя съемки: 02:58:37 UTC+7\nДиафрагма: f/4\nВыдержка: 25\nЧувствительность: ISO-320\nExpN: 500\nЦветовая температура: 5500К\nОсадки: Осадков нет\nТемпература фoтометра: 19.81°C
\.


--
-- Name: pictures_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.pictures_id_seq', 4, true);


--
-- Name: pictures pictures_pk; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.pictures
ADD CONSTRAINT pictures_pk PRIMARY KEY (id);


--
-- Name: pictures_photo_timestamp_idx; Type: INDEX; Schema: public; Owner: postgres
--

CREATE INDEX pictures_photo_timestamp_idx ON public.pictures USING btree (photo_timestamp);


--
-- PostgreSQL database dump complete
--


Loading…
Cancel
Save