If the problem is configobj, then use triple quotes, without the \n:

format = """{station} {dateTime:%H:%M %b %d %Y}:
T: {outTemp:%.1f}ºC
D: {dewpoint:%.1f}ºC"""


-tk


On Mon, Jan 1, 2018 at 7:12 PM, gjr80 <[email protected]> wrote:

> On Tuesday, 2 January 2018 06:58:07 UTC+10, Chris Alemany wrote:
>>
>>
>> It appears the direct editing of the default format variable in
>> twitter.py is best.
>>
>
> Perhaps not, if only we had turned on debug and looked at the log...
>
> using the format config option in weewx.conf (excuse the station name):
>
> Jan  2 13:05:16 stretch22 weewx[2871]: restx: Twitter: msg: Narangba Test
> - weeWX 3.8.0a test2 13:05 Jan 02 2018:\nT: 32.6C\nD: 27.2C\nW: N 0.0kph
> gusting to 0.0kph\nH: 80%\nP: 31.10hPa\nDay Rain {dayRain:%.2f}mm\nRain
> Rate: 0.0mm/hr\nChill:32.6C\nHIndex:32.6C\nSolar: 924W/m2\nUV: 13\n
> #PortAlberni\nhttps://www.alberniweather.ca\nhttps://www
> .youtube.com/alberniweather
>
> omitting the format config option and overriding _DEFAULT_FORMAT with the
> exact same format string (again excuse the station name):
>
> Jan  2 12:55:16 stretch22 weewx[2744]: restx: Twitter: msg: Narangba Test
> 12:55 Jan 02 2018:#012T: 32.6C#012D: 27.1C#012W: N 0.0kph gusting to
> 0.0kph#012H: 80%#012P: 31.10hPa#012Day Rain {dayRain:%.2f}mm#012Rain Rate:
> 0.0mm/hr#012Chill:32.6C#012HIndex:32.6C#012Solar: 948W/m2#012UV:
> 13#012#PortAlberni#012https://www.alberniweather.ca#
> 012https://www.youtube.com/alberniweather
>
> The logged line is the string that is passed to Twython to post as a tweet.
>
> The issue is that when parsing a config file configobj does not convert \n
> to a linefeed escape sequence but rather it escapes the \n (ie \\n) so
> that we end up with a literal \n sequence in the returned string (more
> generally configobj converts any escape sequence to literal characters -
> which is why using #0a in the format config option did not work either) .
> Twython, twitter or whatever ultimately URL encodes this and we end up with
> a plain \ followed by a n, ie no linefeed. On the other hand,
> _DEFAULT_FORMAT is a python string with embedded \n escape sequence and
> the escape sequence flows through twitter.py and Twython or twitter URL
> encodes this escape sequence as a linefeed and consequently you get the
> formatted tweet you seek. A solution that allows the use of a user defined
> format config option that supports linefeeds is to intercept the format
> in the config dict returned by  configobj and replace the literal \n
> sequence with \n escape sequence back in. It feels a little hackish to me
> as it is \n specific but the adding the highlighted lines to twitter.py
> (circa line 180) should do the trick:
>
>         if usn is not None:
>             site_dict['unit_system'] = weewx.units.unit_constants[usn]
>             loginf('units will be converted to %s' % usn)
>
>         try:
>             site_dict['format'] = site_dict['format'].replace('\\n','\n')
>         except:
>             pass
>         site_dict.setdefault('format', self._DEFAULT_FORMAT)
>         site_dict.setdefault('format_None', self._DEFAULT_FORMAT_NONE)
>         site_dict.setdefault('format_utc', False)
>
>
> Of course _DEFAULT_FORMAT should be set back to its original setting and
> the format config option under [[Twitter]] now supports the use of \n to
> insert a new line in the tweet. Note there is no need to enclose the format
> config option in any quotes and of course weeWX will need to be restarted
> for the changes to take effect.
>
> I will caveat this with I do not use Twitter nor have I tested this on
> Twitter.
>
> Gary
>
> --
> 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].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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].
For more options, visit https://groups.google.com/d/optout.

Reply via email to