def format_flickr_photo(style, width, height): photo, size = digestfetch.contact_photo() datetaken = datetime.datetime.strptime(photo.attrib['datetaken'], "%Y-%m-%d %H:%M:%S") df = DateFormat(datetaken) datestring = df.format('l F jS P') aspect_ratio = float(size.attrib['height']) / float(size.attrib['width']) height = width * aspect_ratio print u"• %s: %s (%s)" % (photo.attrib['ownername'], photo.attrib['title'], datestring) print u"- %s" % size.attrib['source'] return [ Paragraph( u"""<img src="%s" valign="top" width="%s" height="%s"/>""" % ( size.attrib['source'], width, height, ), style["Photo"]), Paragraph( u"""<b>%s</b>: %s <br/> <i>%s</i>""" % ( photo.attrib['ownername'], photo.attrib['title'], datestring, ), style["PhotoCaption"]) ]
def format_twitter_statuses(style): """Format Twitter statuses into reportlab Flowables""" paragraphs = [] statuses = digestfetch.twitter_friends() if statuses: for status in statuses: updateDate = datetime.datetime.strptime( status['created_at'], '%a %b %d %H:%M:%S +0000 %Y') df = DateFormat(updateDate) image_text = u""" <img src="%(image)s" width="%(dimension)s" height="%(dimension)s" valign="top"/>""" % { 'image': status['user']['profile_image_url'], 'dimension': inch / 5, } message_text = u""" <b>%(user)s</b>: %(message)s (<i>%(time)s</i>)""" % { 'user': status['user']['name'], 'message': status['text'], 'time': df.format('D P') } if status['in_reply_to_user_id'] == TWITTER_USERID: status_style = style["TwitterReply"] else: status_style = style["Twitter"] try: paragraphs.append( Paragraph(u"%s %s" % (image_text, message_text), status_style)) print u"• %s" % status['user']['name'] except IOError, e: paragraphs.append(Paragraph(message_text, status_style)) print u"ERROR: %s" % e print u"! %s" % status['user']['name']
def format_newsgator_headlines(style): """Format RSS feeds from Newsgator into reportlab Flowables""" data = digestfetch.newsgator_headlines() print "Processing", flowables = [] for entry in data.entries: updated = datetime.datetime.strptime(entry.updated, "%a, %d %b %Y %H:%M:%S %Z") df = DateFormat(updated) print ".", flowables.append( Paragraph( u"""<bullet>•</bullet> %s <b>%s</b> (<i>%s</i>)""" % ( entry.feedtitle, entry.title, df.format('D P'), ), style["List"])) print "done" return flowables
def format_event_recommendations(style): """Format event recommendations fetched from Last.fm into reportlab Flowables""" events = digestfetch.lastfm_event_recommendations() paragraphs = [] latlongs = [] i = 1 for e in events: try: print u'• %s (%s)' % (e.getTitle(), e.getID()) startDate = e.getStartDate() df = DateFormat(startDate) image = e.getImage(pylast.IMAGE_SMALL) image_html = "" if image: image_html = u""" <img src="%(image)s" width="%(dimension)s" height="%(dimension)s" valign="top"/>""" % { 'image': e.getImage(pylast.IMAGE_SMALL), 'dimension': inch / 4, } text = u"""%(image_html)s <seq id="eventrec">. <b>%(title)s</b> %(time)s - %(venue)s %(postcode)s <br/> %(artists)s """ % { 'image_html': image_html, 'title': e.getTitle(), 'artists': u", ".join(e._getFromInfo('artists')), 'venue': e.getVenueName(), 'postcode': e.getPostalCode() or u"", 'time': df.format('D P'), } latlongs.append((i, "%s,%s" % e.getGeoPoint())) i += 1 paragraphs.append(Paragraph(text, style["Event"])) except (pylast.ServiceException, httplib.BadStatusLine), exc: print u'! FAILED - %s (%s)' % (exc, e.getID())
def format_weather(style, available_width): forecast_data, warning_data = digestfetch.weather() # http://www.bbc.co.uk/weather/images/symbols/57x57/3.gif # http://www.bbc.co.uk/weather/images/symbols/fiveday_sym/3.gif (80/65) image_source = forecast_data['feed']['image']['href'].replace( '57x57', 'fiveday_sym') aspect_ratio = 65.0 / 80.0 image_width = inch / 2.5 image_height = image_width * aspect_ratio image_cell = Paragraph( u'<img src="%(source)s" valign="middle" width="%(width)s" height="%(height)s"/>' % { 'source': image_source, 'width': image_width, 'height': image_height, }, style['Body']) # Saturday: sunny intervals, Max Temp: 2°C (36°F), Min Temp: -2°C (28°F) weather_regex = re.compile( ur'([^:]+): ([^,]+), Max Temp: ([^°]+)°C [^,]+, Min Temp: ([^°]+)°C(.+)' ) table_data = [] for i, day in enumerate(forecast_data['entries']): details = day['title'].strip() matches = weather_regex.match(details) if matches: params = { 'day': matches.group(1), 'summary': matches.group(2), 'max_temp': matches.group(3), 'min_temp': matches.group(4), } print u"• %s" % params['day'] if i > 0: image_cell = u'' table_data.append(( image_cell, u"%(day)s: %(summary)s %(min_temp)s/%(max_temp)s°C" % params, )) left_width = image_width + inch / 8 table = Table(table_data, colWidths=(left_width, available_width - left_width), style=[ ('SPAN', TL, (0, i)), ]) table.setStyle( TableStyle([ ('VALIGN', TL, BR, 'MIDDLE'), ('TOPPADDING', TL, BR, 0), ('RIGHTPADDING', TL, BR, 0), ('BOTTOMPADDING', TL, BR, 0), ('LEFTPADDING', TL, BR, 0), ])) weather_flowables = [ table, Spacer(available_width, 8), ] warning_regex = re.compile(ur'^(.+: )') warning_summmaries = [] if warning_data['entries']: print "Warnings", for entry in warning_data['entries']: # ADVISORY of Heavy Snow for London & South East England warning_summmary = entry['summary'].strip().replace( ' for %s' % MET_WEATHER_REGION_FULL, '') if warning_summmary not in warning_summmaries: print '.', # Block dupes warning_summmaries.append(warning_summmary) warning_date = datetime.datetime(*entry['updated_parsed'][:6]) df = DateFormat(warning_date) weather_flowables.append( Paragraph( u"%s - %s" % (df.format('D P'), warning_summmary), style['List'])) print "done" return weather_flowables
def format_gcal_events(style, available_width): """Form events from Google Calendar into reportlab Flowables""" events = digestfetch.gcal_events() print "Processing", flowables = [] table_data = [] table_styles = [] line_styles = [] row = 0 weekday = '' for e in events: df = DateFormat(e['start']) old_weekday = weekday weekday = df.format('D') new_day = False if old_weekday and old_weekday != weekday: line_styles.append(('LINEABOVE', (0, row), (1, row), 1, 'black')) if e['allday']: startstring = weekday formatstring = u"<b>%s</b>" else: startstring = u"%s %s" % ( weekday, df.format('P'), ) formatstring = u"%s" title_data = [ Paragraph(formatstring % e['title'].decode('utf8'), style["Cal"]) ] if e['location']: title_data.append(Paragraph(e['location'], style["Cal"])) table_data.append([ Paragraph(formatstring % startstring, style["Cal"]), title_data, ]) row_style = [ ('BACKGROUND', (0, row), (1, row), HexColor("#%s" % e['color'])), ] table_styles.extend(row_style) for c_row, c in enumerate(e['comments']): row += 1 table_data.append([ Paragraph(c['author'], style["CalComment"]), Paragraph(c['content'], style["CalComment"]), ]) row_style = [ ('BACKGROUND', (0, row + c_row), (1, row + c_row), "white"), ] table_styles.extend(row_style) row += 1 print ".", table_styles.extend([ ('VALIGN', TL, BR, 'TOP'), ('TOPPADDING', TL, BR, 3), ('RIGHTPADDING', TL, BR, 2), ('BOTTOMPADDING', TL, BR, 1), ('LEFTPADDING', TL, BR, 2), ]) fifth_width = available_width / 5 table = Table(table_data, colWidths=(1.2 * fifth_width, 3.8 * fifth_width), style=line_styles) table.setStyle(TableStyle(table_styles)) flowables.append(table) print "done" return flowables