Archive for the ‘Programming’ Category
rsync a file using a ssh .pem public key
Using a public SSH key with rsync to transfer a single file. The key is the -e option which modifies the SSH command used by rsync.
rsync -a ./localfile.jpg [email protected]:/home/user/localfile.jpg -e 'ssh -i keypath.pem'
Return pdf to browser in a C# razor page using itext7
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
Generating Charts with Python and matplotlib as Base64 images for embedding in HTML webpages
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
Split an Excel file’s tabs into CSV with C#
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();
Open a zip archive and iterate through its files with Python
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
Open and print a .gz.bz2 file with Python
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
Use ffmpeg To Create a Timelapse From Pictures With Music
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 |
Convert magnet link to torrent file with Python and add additional trackers
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)
Change Font Type in Clipboard with C#
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();
Send an SMS from a Twilio sim via Programmable SMS
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>