Attached is a diff file for my current changes.  Note that this also 
disables the request for TAF data as I request that separately using 
tafgenerator.py.
In Seasons/skin.conf just tack another couple lines at the end for example:

[TafReport]
  [[kbwi]]

and append ", user.tafgenerator.MyTafSearch" to the search_list_extensions 
in the same file.

I'll submit another pull request on github to get these changes in.

My taf.inc for the Seasons skin is also attached.
On Friday, June 2, 2023 at 4:07:24 PM UTC-4 Michael Bruski wrote:

> I actually did the python3 rewrite and provided it to the original author 
> to post on github.   I may have changed my local copy sometime later to fix 
> a problem with the URL but can't say for sure.  I have about 7 lines in my 
> generator that are commented out.  I know I didn't post further updates to 
> the author.   I could post my current code block for statsHTMLTable() here 
> but don't know how to go about getting it formatted correctly.
> Either that or send me your email address and I'll send it to you the file.
>
> I also created a tafgenerator based on the metargenerator so that I can 
> post that separately from the METAR blocks.
>
>
> On Friday, June 2, 2023 at 3:15:14 PM UTC-4 MikeQ wrote:
>
>> I'm running the same thing.  I don't think you will find anything in 
>> debug mode since metargenerator.py seems to be running and it logs to 
>> syslog directly.  I am not a python programmer, but I think you will need 
>> to add some additional "syslog.syslog(syslog.LOG_INFO,......." code to find 
>> out exactly what is happening in def statsHTMLTable(self, airport): when it 
>> tries to get the metar.
>>
>> On Friday, June 2, 2023 at 12:58:21 PM UTC-6 Nate Bargmann wrote:
>>
>>> * On 2023 02 Jun 13:31 -0500, MikeQ wrote: 
>>> > One more thought - what version of Python are you running? The 
>>> original 
>>> > weewx-metar code was written in 2018 for Python2 and was re-written 2 
>>> years 
>>> > ago for Python3. The current version in master on github is the 
>>> Python3 
>>> > version. 
>>>
>>> I am running on Amrbian (Debian) Bullseye which has Python 3.9.2. Would 
>>> the extension benefit from running Weewx with debug mode enabled? 
>>>
>>> - Nate 
>>>
>>> -- 
>>> "The optimist proclaims that we live in the best of all 
>>> possible worlds. The pessimist fears this is true." 
>>> Web: https://www.n0nb.us 
>>> Projects: https://github.com/N0NB 
>>> GPG fingerprint: 82D6 4F6B 0E67 CD41 F689 BBA6 FB2C 5130 D55A 8819 
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/weewx-user/76bc97d5-ccd5-4163-9e48-f683509b82fbn%40googlegroups.com.
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2017-2018  Daniel Macías Perea <[email protected]>
#
# Distributed under the terms of the GNU GENERAL PUBLIC LICENSE
#
# Derived metargenerator.py by Michael Bruski (AJ9X)
# Runs under Python3 and WeeWx version 4
#
# None of the local airports in my immediate vacinity provide TAFs.
# This extension allows me to display a TAF(s) for larger Airport(s) around
# me that do provide them.  The results in this table are display separately
# from the metar table.  I have also modified the URL in my copy of
# my copy of metargenerator.py to turn off the check for TAFs.  Likewise,
# the URL in this generator turns off the check for METARs.
#
# To distinguish TAF backup files from METAR backup files, this generator
# will produce filenames in the form "icao.taf".  Use the data tag
# $iaco_taf to include them in your report just as you would use
# $iaco_metar to display a METAR.
#
"""Extends the Cheetah generator search list to add TAF data tables.
To use it, add this generator to search_list_extensions in skin.conf:

[CheetahGenerator]
    search_list_extensions = user.tafgenerator.MyTafSearch

############################################################################################
#
# HTML TAF obtained from aviationweather.gov
#
[TafReport]
    
    # Taf Airports to generate
    refresh_interval = 5

    [[kbwi]]                           # Create a new Cheetah tag which will have a _taf suffix: $kbwi_taf
    [[kmdt]] 

"""

from weewx.cheetahgenerator import SearchList
from weewx.tags import TimespanBinder
from datetime import datetime
import time
import os.path
import weeutil.weeutil
import syslog
import random
import requests
import re

class MyTafSearch(SearchList):
    def __init__(self, generator):
        
        SearchList.__init__(self, generator)
        self.taf_dict = generator.skin_dict['TafReport']
        
        self.refresh_interval = int(self.taf_dict.get('refresh_interval', 5))
        self.cache_time = 0      

        self.search_list_extension = {}        

    def get_extension_list(self, valid_timespan, db_lookup):
        """For weewx V3.x extensions. Should return a list
        of objects whose attributes or keys define the extension.

        valid_timespan:  An instance of weeutil.weeutil.TimeSpan. This will hold the
        start and stop times of the domain of valid times.

        db_lookup: A function with call signature db_lookup(data_binding), which
        returns a database manager and where data_binding is an optional binding
        name. If not given, then a default binding will be used.
        """	
        # Time to recalculate?
        if (time.time() - (self.refresh_interval * 60)) > self.cache_time:
            self.cache_time = time.time()
            
            t1 = time.time()
            ngen = 0
            for airport in self.taf_dict.sections:
                taf_name = airport + '_taf'              
                
                try:
                    self.search_list_extension[taf_name] = self.statsHTMLTable(airport)
                    ngen += 1
                except:
                    syslog.syslog(syslog.LOG_INFO, "%s: error: Cannot get TAF Report (%s). Recovering the last file saved." % (os.path.basename(__file__), taf_name))                                    
                    # try to get last TAF file saved
                    try:
                        with open("/tmp/" + airport + ".taf", 'r') as f:
                            self.search_list_extension[taf_name] = f.read()
                    except:
                        syslog.syslog(syslog.LOG_INFO, "%s: error: There could not be found an older TAF Report. Skipping!" % os.path.basename(__file__))
                        self.search_list_extension[taf_name] = "Error - No TAF available"
                    
            t2 = time.time()

            syslog.syslog(syslog.LOG_INFO, "%s: Generated %d taf tables in %.2f seconds" %
                         (os.path.basename(__file__), ngen, t2 - t1))

        return [self.search_list_extension]

    def statsHTMLTable(self, airport):
        """
        airport: ICAO Code of Airport to generate taf
        """
        url = "https://www.aviationweather.gov/adds/metars?station_ids=%s+&std_trans=translated&chk_metars=off&chk_tafs=on&submitmet=Get+Weather"; % (airport)
        ##syslog.syslog(syslog.LOG_INFO, "%s: request URL: %s" % (os.path.basename(__file__), url))

        attempts = 0
        success  = False
        while attempts < 3 and not success:
            try:
                rq = requests.get(url, data=None, headers={'Connection': 'close'}, timeout=0.6)
                rq.raise_for_status()
                if rq.status_code == requests.codes.ok:
                    myfile = rq.text
                    ##if myfile.endswith('</BODY>\n<HTML>\n'):
                    myfile = myfile[:-16]
                    lines = myfile.split("<TR VALIGN=")
                    ##syslog.syslog(syslog.LOG_INFO, "%s: request succeeded" % os.path.basename(__file__))
                    success = True
            except:
                attempts = attempts + 1
                time.sleep(random.uniform(3,60))

        if attempts == 3:
            syslog.syslog(syslog.LOG_INFO, "%s: request failed" % os.path.basename(__FILE__))

        # Obtain the new TAF information and replace the airport file
        if myfile.find("Output produced by METARs form") != -1:
            htmlText = "<TABLE style=\"border-spacing: 5px;border-collapse: inherit;line-height: 1.3;\">"
            
            for i in range(len(lines)):
                if i > 1:
                    htmlText += "<TR VALIGN=%s" % lines[i]

            thtml = "/tmp/" + airport + ".taf"
            with open(thtml, 'w') as f:
                f.write(htmlText)
                ##syslog.syslog(syslog.LOG_INFO, "%s: modified HTML written to %s" % (os.path.basename(__file__), thtml))

        ##syslog.syslog(syslog.LOG_INFO, "%s: return - %s" % (os.path.basename(__file__), htmlText))
        return htmlText
8a9,13
> # The URL has been modified to turn off checks for TAFs.  If you wish
> # to display TAFs, you can use the TAF extension to display those in
> # a separate table of your report.
> #
> # Give that the results from aviation weather 
35a41,42
> import random
> import requests
92,93c99,107
<         url = "https://www.aviationweather.gov/adds/metars?station_ids=%s+&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submitmet=Get+Weather"; % (airport)
<         syslog.syslog(syslog.LOG_INFO, "%s: request URL - %s" % (os.path.basename(__file__), url))
---
>         url = "https://www.aviationweather.gov/adds/metars?station_ids=%s+&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=off&submitmet=Get+Weather"; % (airport)
>         ##syslog.syslog(syslog.LOG_INFO, "%s: request URL: %s" % (os.path.basename(__file__), url))
> 
>         ##import urllib.request, urllib.error, urllib.parse
>         ##myfile = urllib.request.urlopen(url).read()
>         ##syslog.syslog(syslog.LOG_INFO, "%s: myfile: %s" % (os.path.basename(__file__), myfile))
>         ##lines = myfile.split("<TR VALIGN=")
>         ##syslog.syslog(syslog.LOG_INFO, "%s: line[0]: %s" % (os.path.basename(__file__), line[0]))
>         ##syslog.syslog(syslog.LOG_INFO, "%s: line[1]: %s" % (os.path.basename(__file__), line[1]))
95,96d108
<         import random
<         import requests
101c113
<                 rq = requests.get(url, timeout=5.0)
---
>                 rq = requests.get(url, data=None, headers={'Connection': 'close'}, timeout=0.6)
104a117,118
>                     ##if myfile.endswith('</BODY>\n<HTML>\n'):
>                     myfile = myfile[:-16]
106c120
<                     syslog.syslog(syslog.LOG_INFO, "%s: request succeeded" % os.path.basename(__file__))
---
>                     ##syslog.syslog(syslog.LOG_INFO, "%s: request succeeded" % os.path.basename(__file__))
126c140
<                 syslog.syslog(syslog.LOG_INFO, "%s: modified HTML written to %s" % (os.path.basename(__file__), mhtml))
---
>                 ##syslog.syslog(syslog.LOG_INFO, "%s: modified HTML written to %s" % (os.path.basename(__file__), mhtml))

Attachment: taf.inc
Description: Binary data

Reply via email to