Monday, May 2, 2016

Download All Images from a Wordpress.com Media Library

After a few months of blogging photos from my life events, I realised that I should have kept a local version (dummy me). I kept everything else, so why did I forget this time?!
Anyways, I realised that this is not an easy job if you are free hosting on Wordpress.com. All I could have is either an XML with posts and references to images, or a full migration to another Wordpress blog.
So I rolled up my sleeves and reused a python script I used before.

Here is the script:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# This script parses a wordpress backup XML and downloads the contained media files.
# Example:
# python wordpress-media-downloader.py  /Users/madly/Downloads/mycoolblog.wordpress.2016-05-02.xml /Users/madly/Downloads/media-library

import sys
import urllib2
import re

#
# Takes a url and a directory for saving the file. Directory must exist.
#
def download(url, dir_name):
    file_name = url.split('/')[-1]
    u = urllib2.urlopen(url)
    f = open(dir_name+'/'+file_name, 'wb')
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    print "Downloading File: %s (Size: %s Bytes)" % (file_name, file_size)

    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print status,

    f.close()

#
# Take file name and directory parameters from the user call
#
file_name = sys.argv[1]
dir_name = sys.argv[2]
img_regex = '(http:\/\/.*files\.wordpress\.com.*\.(?:jpeg|jpg|png))'

#
# Get the images urls from the xml file
#
urls_to_download = []
file = open(file_name, "r")

for line in file.readlines():
    m = re.search(img_regex, line)
    if m:
        img_url = m.group(1)
        urls_to_download.append(img_url)

urls_count = len(urls_to_download)
print("Images to download: %s images." % (urls_count))

#
# Download images
#
i = 0
for url in urls_to_download:
    i += 1
    print("File %s/%s" % (i, urls_count))
    download(url, dir_name)
    # print(url) # use this line instead of download() if you want to export the output to a download manager

I simply call the script and pass the backup XML file I've just downloaded from my Wordpress.com admin panel (/wp-admin) and the path to the folder where I want to save the images.



One more update can be to use a better parsing technique and use an element called wp:post_id to sort the images into folders by their post. But I am satisfied with the current result. I can do the rest manually as it will be faster in my case :)

Sunday, May 1, 2016

PENNY (Congstar) USB Internet on Linux Mint 17 - Ubuntu 14.04

So, I've been in Germany for a while. And after using the fast WiFi in hotels, I moved to a personal apartment, with no Internet for now. I bought a PENNY USB surf stick as I thought it is a bit cheaper. But there was a problem, it is not working on Linux :(.

 
I lived with this fact for a while, but now I want to get back to programming. So, after some searching and trying, here is how I could use my PENNY Mobil Internet Stick, without their Internet manager of course, and that's a bit bad for calculating my overall usage.

Anyways, it is straight forward:

1. Open 'Network Connections'. Click 'Add'.

2.  Choose 'Mobile Broadband'.

3. The default device is 'Internetstick'.

4. Choose your country. Mine is 'Germany'.

5. The service provider is 'Congstar'.

6. The default plan is 'Prepaid Contracts', which is my case.

7. Verify your data and click 'Apply'.

8. The connection form is pre-populated with the correct data. I just changed the connection name to be meaningful to me. (and the password was tm)

9. Go to 'IPv4 Settings' tab, and Add this additional DNS server IP: 193.254.160.1. Now click 'Save'.

10. The connection is added. Click 'Close'.

11. Now you can see in the connections list that a broadband section is added. Turn it on it was off. Or click the connection name you just created (Congstar Prepaid PENNY).

Now you are connected. Enjoy the surfing and keep an eye on the System Monitor for data usage :).