Apr. 17, 2014

Resizing with maintained aspect ratio applet

Maybe I'm just way too old school, but when dealing with resizing anything, I like to jot down a simple fractional equation. For example, if there's a video that's 530 x 298 pixels and you need to resize that to 505 pixels wide, and maintain the aspect ratio:

First, setup the simple equation like so:

\frac {530}{298}=\frac {505}{y}

Second, cross-multiply the fraction:

530*y = 505 * 298

Then solve for y, divide both sides by 530:

y = \frac{505 * 298}{530}

And you may want to round the answer:

y = 283.9433962264...

y \approx 284

Original Resized (Rounded)
Width:
Height:

 

In general terms, if you have:

\frac {a}{b}=\frac {c}{d}

Formulas for solving c and d are:

c = \frac{(a* d)}{b}\ ; d = \frac{(b* c)}{a}

No comments \ Leave a comment
Nov. 22, 2013

North Korean Central News Agency (KCNA) server logs were protected by client-side JavaScript

Sat on this for a year, and figured it's time to publish. 

After a chance discovery of what appear to be KCNA's webserver logs in late 2012, I was able to perform an analysis and shed some light on the secretive North Korean news agency known to the world as www.kcna.kp.  The media often quotes the source, analysts study their content, but until now -- no one on the outside knew exactly how big their web audience was.

 

Traffic Overview, Apr 2011 - Dec 2012

For the first eleven months of 2011, there was only a bare trickle of web traffic registering on the North Korean Central News Agency's website. Only about 600 people would visit the website daily, and perhaps click on four or five stories apiece. In terms of modern news site traffic, that's akin to cosmic background radiation being picked up by a Geiger counter: insignificant.

Then, in December of 2011, Kim Jong Il died. In the two weeks following his death, everything changed.  The world turned its gaze on North Korea, and by relative standards, the KCNA website traffic skyrocketed. Kim Jong Il's dying alone gave the KCNA a traffic boost it desperately needed. Pageviews in that brief period were nearly half of all their year's traffic.

Language Breakdown:
kor 341,163
chn 68,185
jpn 20,439
eng 38,489
spa 2,896

Media events cause public attention to fade away quickly and this was no exception. However, a peculiar thing happened. After the event, KCNA's baseline traffic levels rose to about 3,000 unique visitors per day - a five-fold increase, and it's been slowly but steadily growing.

After the April 13 test missile launch (second large spike), world again started paying attention to North Korea: KCNA is definitely gaining some traction.

There are five language editions: Korean, Chinese, Japanese, English and Spanish. Of that, 72% of their visitors read in Korean, 14% in Chinese, and the remaining languages comprise the remainder.

Security Hole

Often enough, a content management system's backend has a commonly predicted login URL, such as /admin/. Most such backends are entirely firewalled, or heavily fortified.  That initially appeared to be the case with KCNA: pulling up that URL returned you to where you came from after displaying a browser alert roughly saying "not allow" (at least according to Google Translate.)

However, if you opened the URL up in a new tab, you'd see this overview on the right after dismissing the alert() message - presumably a visitor log, with IP addresses, timestamps and language editions. Clicking into any of the links would tripwire the same security apparatus, and you'd get another alert box.

Not allow!KCNA.KP /admin/

Since the data scrape, the hole seems to have been patched by an actual firewall. However, it's bizarre that for years KCNA's reader privacy was essentially protected by a mechanism that nagged you into not looking around with a history.back() statement.  This was the source view of the /admin/ pages:

kcna_security

Think this part has to be spelled out: KCNA readers' IP addresses were exposed to the public.

 

Success Rate

Goals of any news agency are remarkably simple -- to report the news -- and to reach and grow audience.  How that's actually done is a constant struggle and constant adaptation, but the mark of success is also remarkably simple: growing audience into a loyal following, and with that, increasing outreach.

Despite having no external obstacles to their mission (such as active internet censorship by the west) the North Korean Central News Agency has largely failed to reach a significant web audience. Even with the two event jolts, KCNA web traffic is tiny: a bare trickle over the past years. However much money they're expending in the effort can't be justifiably worth it.

That's just my editorializing, but, at least it's based on on actual numbers. See for yourself.

No comments \ Leave a comment
Nov. 17, 2013

Simple python list picker using curses

Are you writing a workhorse shell script in python?  Why not curses?

This class, using curses, lets a user select from a simple list of options. The return is a simple list of selected options, or False (for cancel). And, for the OCD in all of us, it preserves terminal window contents after exiting.

picker3picker4

First, get picker.py from github: https://github.com/pp19dd/picker

Usage example:

from picker import *
    
opts = Picker(
    title = 'Select files to delete',
    options = [
        ".autofsck", ".autorelabel", "bin/", "boot/", 
        "cgroup/", "dev/", "etc/", "home/", "installimage.conf",
        "installimage.debug", "lib/", "lib64/", "lost+found/",
        "media/", "mnt/", "opt/", "proc/", "root/",
        "sbin/", "selinux/", "srv/", "sys/",
        "tmp/", "usr/", "var/"
    ]
).getSelected()

if opts == False:
    print "Aborted!"
else:
    print opts

If the user hits cancel, the routine returns a False - otherwise, you get a simple list:

['.autofsck', '.autorelabel', 'bin/', 'boot/', 'cgroup/']

The return, being a simple list, allows for a fairly readable logic:

if "cgroup/" in opts:
    print "cgroup is done for!"

Now, mind the possible False return, and put it to good use!

No comments \ Leave a comment

22 Wood logs. From spam.

Got a spam email asking me whether I'm interested in buying various types of wood from a wood exporter. For the record, I'm ... probably very obviously not in any sort of industry that would use wood. Still, the list fascinated me. Since I'm a fan of minecraft, the repetitive "logs" part stuck with me. At this time, minecraft only has six types of logs: oak, birch, spruce, "jungle", acacia and "dark oak." The spammers offered more:

  1. TEAK WOOD LOGS
  2. OBECHE WOOD LOGS
  3. MASONIA WOOD LOGS
  4. PINE WOOD LOGS
  5. MELEGBA WOOD LOGS
  6. DOUSSIE WOOD LOGS
  7. GMELINA WOOD LOGS
  8. MAHOGAN WOOD LOGS
  9. DABEMA AZOBE WOOD LOGS
  10. KOSSO SQUARED LOGS
  11. OVENGKOL WOOD LOGS
  12. KHAYA WOOD LOGS
  13. PANGA PANGA WOOD LOGS
  14. OKUME WOOD LOGS
  15. ZINGAWA WOOD LOGS
  16. BILINGA WOOD LOGS
  17. DAHOMA WOOD LOGS
  18. ROSEWOOD WOODS LOGS
  19. EBIARA WOODS LOGS
  20. POCOULI WOODS LOGS
  21. OKAN WOOD LOGS
  22. TALI WOOD LOGS

 

Since I'm me, I tried to make sense of this list, and I couldn't.  Names don't seem to be standardized or easily recognizable to someone in the states. However, according to wikipedia, the boldfaced ones are endangered in some way - vulnerable or a threatened species.

No comments \ Leave a comment
Oct. 08, 2013

Python script to remove /tmp/ files older than 7 days

For no reason at all, I needed a simple trimmer program. Rather than write a shell script in bash, thought to do it in python.

#!/usr/bin/python

# run by crontab
# removes any files in /tmp/ older than 7 days

import os, sys, time
from subprocess import call

now = time.time()
cutoff = now - (7 * 86400)

files = os.listdir("/tmp")
for file in files:
        if os.path.isfile( "/tmp/" + file ):
                t = os.stat( "/tmp/" + file )
                c = t.st_ctime

                # delete file if older than a week
                if c < cutoff:
                        os.remove("/tmp/" + file)

Save this as /wherever/trim_tmp.py, and chmod +x it for good measure. Then, add to crontab with crontab -e. In the localized user crontab file, put @daily /wherever/trim_tmp.py

No comments \ Leave a comment
Jul. 11, 2013

Stippled Mandala of Nikola Tesla

tesla-mandala

1 comment \ Leave a comment
Posts on this blog solely represent my personal opinions and technical experience.

© 2009-2014 Edin (Dino) Beslagic