xyzio

Archive for the ‘Programming’ Category

Return pdf to browser in a C# razor page using itext7

leave a comment »

Sample code to return a PDF file to the browser in a Razor c# page using itext7.


using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.IO;

public IActionResult OnPostManipulatePdf(String dest)
{
    MemoryStream ms = new MemoryStream();

    PdfWriter writer = new PdfWriter(ms);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document doc = new Document(pdfDoc);
    writer.SetCloseStream(false);

    Paragraph header = new Paragraph("header");

    doc.Add(header);
    doc.Close();

    byte[] byteInfo = ms.ToArray();
    ms.Write(byteInfo, 0, byteInfo.Length);
    ms.Position = 0;

    FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf");

    //Uncomment this to return the file as a download
    //fileStreamResult.FileDownloadName = "Output.pdf";

    return fileStreamResult;
}

Source:
https://stackoverflow.com/questions/1510451/how-to-return-pdf-to-browser-in-mvc

Written by M Kapoor

September 23, 2020 at 2:49 pm

Posted in Programming, c#

Tagged with , , ,

Generating Charts with Python and matplotlib as Base64 images for embedding in HTML webpages

leave a comment »

This is code used to create the charts for my ‘Compare Expense Ratios‘ microsite. It uses matplotlib to generate a chart which is converted to a Base64 to be embedded directly into a webpage. This way there is no need to save an intermediate image.

#Inputs are a dictionary of lists that contains points to plot (points), the title, and the x and y-axis labels.
def mathPlotLib(points, title, xlabel, ylabel):
   import matplotlib
   matplotlib.use('Agg')
   from matplotlib import pyplot
   import base64
   import cStringIO
   import natsort

   #The two funds are the keys to the dicitonary
   fundOrder = []
   for fund in natsort.natsorted(points.keys()):
      #Plot the points and keep track of the order they are added for the legend
      pyplot.plot(points[fund])
      fundOrder.append(fund)

   # Add the legend, title, and x,y labels.
   pyplot.legend(fundOrder)
   pyplot.title(title)
   pyplot.xlabel(xlabel)
   pyplot.ylabel(ylabel)

   # Convert the image as png as a byte-string object
   my_stringIObytes = cStringIO.StringIO()
   pyplot.savefig(my_stringIObytes, format='png')

   # Seek to the beginning of the file and encode it as Base64
   my_stringIObytes.seek(0)
   b64png = base64.b64encode(my_stringIObytes.read())

   # Add the html wrapper to embed it as base64
   html = '<img src="image/png;base64,' + b64png + '" />'

   # Clean up
   my_stringIObytes.close()
   pyplot.clf()
   pyplot.close()

   return html

 

Written by M Kapoor

December 24, 2019 at 8:04 pm

Posted in Programming, python

Tagged with , ,

Split an Excel file’s tabs into CSV with C#

leave a comment »

Split an Excel file into csv, with each tab going into a separate csv file, in the background using C#.


using System.IO;
using Microsoft.Office.Interop.Excel;

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

//Run in background
app.DisplayAlerts = false;

//Open excel file
Workbook wb = app.Workbooks.Open("path_to_excel.xls");

//Iterate through sheets in the excel file
foreach (Worksheet sheet in wb.Worksheets)
{
    string sheetName = sheet.Name;
    
    //Output file name
    string outputFilepath = sheetName + ".csv";
    
    //Save the sheet as CSV
    sheet.SaveAs(outputFilepath, XlFileFormat.xlCSVWindows);
}

//Clean-up
wb.Close();
app.Quit();


Written by M Kapoor

December 24, 2019 at 3:19 am

Posted in c#, Programming

Tagged with ,

Open a zip archive and iterate through its files with Python

leave a comment »

Open a file compressed as .zip and iterate through its files line by line with Python.


import zipfile

#Path to the zip file
zipfilepath = 'path_to_zip_file'

#Read in zip file
zip = zipfile.ZipFile(zipfilepath)

#Iterate through files in zip file
for zipfilename in zip.filelist:
    
    #Read contents of the file
    filecontents = zip.read(zipfilename)
    
    #Break up contents into list and process
    for line in filecontents.replace('\r\n', '\n').split('\n'):
        print line

Written by M Kapoor

December 24, 2019 at 3:09 am

Posted in Programming

Tagged with ,

Open and print a .gz.bz2 file with Python

leave a comment »

Open and print a file that is compressed with gzip and then with bzip2, i.e a gz.bz2 file, with Python.


import sys
import bz2
import gzip
from cStringIO import StringIO

# .gz.bz2 File is given as commandline argument
filename = sys.argv[1]

gzbzfilename = filename

#Read in the bz2 data
o = open(gzbzfilename, 'rb')
gzbzdata = o.read()

#Decompress the bz2 data
gzdata = bz2.decompress(gzbzdata)

#Next, gunzip the gzip file and read out the file pointer
f = gzip.GzipFile(fileobj=StringIO(gzdata))
file_content = f.read()
f.close()

#Print out the file contents
print file_content

Written by M Kapoor

December 24, 2019 at 3:04 am

Posted in Programming

Tagged with , , ,

Use ffmpeg To Create a Timelapse From Pictures With Music

leave a comment »

Create a timelapse movie from images using ffmpeg with background music.

Images must be sequentialy numbered starting with the value given for the start_number argument.

Command:

ffmpeg -framerate 12 -start_number 15 -f image2 -y -i "G%7d.jpg" -i music.mp3 -c:v libx264 -preset fast -c:a mp3 -shortest -movflags +faststart "output_filename.mp4"

 

Options Breakdown:

Option Description
framerate 12 Number of images to show per second
start_number 15 Start number of the first image
-i “G%7d.jpg” ffmpeg formatted file name
-i music.mp3 Background music filename
-shortest Truncate video to shortest of timelapse or music
-movflags +faststart Put file index at beginning to speed up online streaming

Written by M Kapoor

December 23, 2019 at 11:54 pm

Posted in Programming

Tagged with ,

Convert magnet link to torrent file with Python and add additional trackers

leave a comment »

With rtorrent it is easier to have it pick up torrent files from a directory. This code converts a magnet link into a torrent file with Python and then inserts additional trackers to the torrent file.


import urllib
import HTMLParser

#This is the magnet link to modify
magnetLink = 'magnet:?xt=urn:btih:b54a3ba68fd398ed019e21290beecc9dda64a858&dn=wikipedia_en_all_novid_2018-06.zim&tr=udp%3a%2f%2ftracker.mg64.net'

#Here is a list of trackers to insert into the torrent
trackers = {'list','of','trackers'}

#Convert HTML special characters to escaped normal characters
magnetLink = urllib.unquote(magnetLink).decode('utf8')
magnetLink = HTMLParser.HTMLParser().unescape(magnetLink)

#Split out the trackers from the magnet link and save for later
magnetsplit = magnetLink.split('&tr=')
base = magnetsplit[0]
magnetTrackers = magnetsplit[1::]

#Add the trackers from the magnet link to our list of trackers
for magnetTracker in magnetTrackers:
    trackers.add(magnetTracker)

#Add the trackers
magnetLink = base
for tracker in trackers:
    magnetLink += '&tr=' + tracker

#Create the torrent file name - it is named after the magnet hash
magnetName = magnetLink[magnetLink.find("btih:") + 1:magnetLink.find("&")]
magnetName = magnetName.replace('tih:','')
torrentfilename = 'meta-' + magnetName + '.torrent'

#Write the magnet link to the torrent file
with open(torrentfilename, 'w') as o:
    linkstr = u'd10:magnet-uri' + str(len(magnetLink)) + u':' + magnetLink + u'e'
    linkstr = linkstr.encode('utf8')
    o.write(linkstr)


Written by M Kapoor

December 23, 2019 at 11:48 pm

Posted in Programming

Tagged with ,

Change Font Type in Clipboard with C#

leave a comment »

Convert text in the Clipboard to another font and re-save it to the clipboard in C#.


RichTextBox rtb = new RichTextBox();

//The new font is GenericMonospace
Font font = new Font(FontFamily.GenericMonospace, 10);

//Get text from the clipboard
rtb.Text = Clipboard.GetText();

//Select all text in the RichTextBox and apply fontstyle
rtb.SelectAll();
rtb.SelectionFont = font;

//Copy updated text back to the clipboard
rtb.Copy();

Written by M Kapoor

December 23, 2019 at 9:39 pm

Posted in Programming

Tagged with ,

Send an SMS from a Twilio sim via Programmable SMS

leave a comment »

Send an SMS from a Twilio sim via Programmable SMS to the outside world.

Enter this as TwiML in the SMS section of the ‘Progammable Voice and SMS’ in the settings menu for your sim.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <!-- The from tag contains the phone number assigned to the sim -->
    <Message from="+19195551212" to="{{To}}">
     {{Body}} 
   </Message> 
</Response>

Written by M Kapoor

December 23, 2019 at 8:21 pm

Posted in Programming

Tagged with , , ,

Authenticate with BackBlaze B2 and list buckets with Python

with one comment

Authenticate with BackBlaze B2 and list buckets with Python.

First to authenticate with BackBlaze B2:


import requests
from requests.auth import HTTPBasicAuth

#Credentials from BackBlaze B2 manager
key_id          = 'b2_key_id'
application_key = 'b2_application_key'

#Contact authorization server and check response
path   = 'https://api.backblazeb2.com/b2api/v1/b2_authorize_account'
result = requests.get(path, auth=HTTPBasicAuth(key_id, application_key))
if result.status_code != 200:
    print 'Error - Could not connect to BackBlaze B2'
    exit()

#Get results and add authorization token to headers
result_json = result.json()
account_id  = result_json['accountId']
auth_token  = result_json['authorizationToken']
api_url     = result_json['apiUrl'] + '/b2api/v1'
api_session = requests.Session()
api_session.headers.update({ 'Authorization': auth_token })

Now use api_session to interact with B2. For example, download the list of buckets like this:


#Construct the API url
url = api_url + '/b2_list_buckets'

#Use the api_session parameter to get a list of buckets
bucketInfo = api_session.post(url, json={'accountId': account_id})

#Convert datastream to json
jsonBucketInfo = bucketInfo.json()

#List id and name
for bucket in jsonBucketInfo['buckets']:
    bucketId = bucket['bucketId']
    bucketName = bucket['bucketName']

    print bucketId, bucketName

More details about tokens in the json are on BackBlaze’s list_buckets API page.

Written by M Kapoor

December 23, 2019 at 8:05 pm

Posted in Programming

Tagged with ,