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>
Authenticate with BackBlaze B2 and list buckets with Python
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.
Authenticate with BackBlaze B2 and get file URLs using Python
Authenticate with BackBlaze B2 and get the URLs of files in a bucket.
First to authenticate with BackBlaze B2:
import requests from requests.auth import HTTPBasicAuth #Auth information from Backblaze key_id = 'key_id' application_key = 'application_key' #Authenticate 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() #Read response result_json = result.json() account_id = result_json['accountId'] auth_token = result_json['authorizationToken'] api_url = result_json['apiUrl'] + '/b2api/v1' download_url = result_json['downloadUrl'] + '/file/' api_session = requests.Session() api_session.headers.update({ 'Authorization': auth_token })
Now get bucket contents and assemble URL. Sample code for bucket ID and bucket name is here.
#Initialize bucketId = 'bucket_id_from_b2' bucketName = 'name_of_bucket' params = {'bucketId': bucketId} urls = set() #Loop for as long as a nextFile exists while True: #Construct api call, execute, and read back information url = api_url + '/' + 'b2_list_file_names' fileList = api_session.post(url, json=params) jFileList = fileList.json() #Loop through files and construct url for file in jFileList['files']: urls.add(download_url + bucketName + '/' + file['filename']) #Check for next file and break if it doesn't exist startFileName = jFileList['nextFileName'] if startFileName == None: break else: #continue If next file exists params['startFileName'] = startFileName
Forward SMS to multiple phone numbers with TwiML Bin
Other parameters are possible like {{FromCity}} and {{FromState}}. You can forward to multiple numbers by duplicating the Message tag.
More details here:
https://www.twilio.com/docs/sms/twiml
<?xml version="1.0" encoding="UTF-8"?> <Response> <Message to="+16623458765">Twilio: {{From}}: {{Body}}</Message> <Message to="+15033943032">Twilio: {{From}}: {{Body}}</Message> </Response>
Generate and save a static page in Django
Generate and save a static page in Django.
Where
html/static.html is a Django template
data is the data you want to pass into the page
static(request) is a function mapped to a url in urls.py
Then
Visiting the page will generate this static file
Code
from django.template.loader import render_to_string def static(request): results = render_to_string('html/static.html', {'content': html}) with open(r'C:tempstatic.html','w') as f: f.write(results)
Copy a file from a URL directly to S3 using boto3 and requests
Copy a file at inUrl directly to a s3 bucket bucketName. ACL is set to public-read and ContentType is maintained from the from URL.
Your AWS credentials must be pre-set.
import boto3 import requests #Set up boto3 env vars os.environ['AWS_ACCESS_KEY_ID'] = '' os.environ['AWS_SECRET_ACCESS_KEY'] = '' os.environ['AWS_DEFAULT_REGION'] = 'us-west-2' inUrl = r'file_to_read' response = requests.get(inUrl) s3 = boto3.resource('s3') s3.Bucket(bucketName).put_object(Key='test/file_path.ext', Body=response.content, ACL='public-read',ContentType=response.headers['Content-Type'])
Wait a random delay in Python
Wait a random interval in seconds between 0 and delay.
import random import time timeDelay = random.randrange(0, delay) time.sleep(timeDelay)
Set User-Agent for Python requests library
Some sites don’t allow page access if the User-Agent isn’t set. headers is a hash and can hold additional header tokens.
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} resp = requests.get(url, headers=headers)
Download and save file using Python Requests
Download and save a file using the Python requests library.
Where:
url = URL to download
filename = Name of local file for url
import requests url = '' response = requests.get(url) with open(filename, 'wb') as f: f.write(response.content)