summaryrefslogtreecommitdiff
path: root/app/unused_apps/daily/retriever.py
blob: 3b7da87f0871e8aca438b4e50d4e1cdd9c580479 (plain)
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
49
50
51
52
53
from datetime import datetime
import requests
import json

from django.utils import timezone
from django.template.loader import render_to_string
from django.contrib.auth.models import User
from django.db.models import Q

from .models import CheckIn, Daily, Weather


def create_daily():
    users = User.objects.filter(Q(username="luxagraf") | Q(username="corrinne"))
    for user in users:
        d, created = Daily.objects.get_or_create(
            user=user,
            date=timezone.now()
        )
        if not created:
            print("already existed")


def get_yesterday_weather():
    current = CheckIn.objects.latest()
    PB_URL = "http://api.wunderground.com/api/39c3ce6a12b14e75/yesterday/q/%s,%s.json" % (current.lat, current.lon)
    r = requests.get(PB_URL)
    weather = json.loads(r.text)
    data = weather['history']['dailysummary'][0]
    date = "%s %s %s" % (data['date']['year'], data['date']['mon'], data['date']['mday'])
    dt = datetime.strptime(date, "%Y %m %d")
    w, created = Weather.objects.get_or_create(
        point=current.point,
        date=dt,
        temp_max=data['maxtempi'],
        temp_min=data['mintempi'],
        temp_mean=data['meantempi'],
        wind_mean=data['meanwindspdi'],
        wind_max=data['maxwspdi'],
        humidity=data['humidity'],
        snow_amount=data['snowfalli'],
        rain_amount=data['precipi'],
        fog=int(data['fog']),
        rain=int(data['rain']),
        snow=int(data['snow']),
        hail=int(data['hail']),
        thunder=int(data['thunder']),
    )
    dailies = Daily.objects.filter(date=dt)
    for d in dailies:
        d.weather = w
        d.weather_human = render_to_string('human_weather.txt', {'object': w}).strip()
        d.save()