Sample

So I was given the task to create a template document for our project, I wanted to make it so that a lot of the pieces of text are automatically synchronized. By that I mean that repeated parts of text like the version of the document, the name of our project group and so on will always be the same throughout the whole document, wherever you might change it.

 

I knew of insert -> quick parts -> document property, which are kept in sync automatically. but the problem is, these are predefined and you can rename them, but if you rename Status to Version, and you place another Status and rename it to Project name, they are still referring to the same piece of text. So Version and Project name will still refer to the initial Status text. I definitely didn't want this! so I started to look up if I could add my own set of these text fields. These document property fields actually use XML, and you can import your own XML to a Word file! So let's take a look at a very basic implementation.

 

Let's first create an XML file.

<Root xmlns="Easy Document management">

  <Company>

    <Name/>

    <Address/>

    <PhoneNumber/>

    <FaxNumber/>

    <E-mail/>

    <Contact/>

  </Company>

 

  <Document>

    <Name/>

    <Date/>

    <Status/>

    <Version/>

    <Description/>

    <Subject/>

    <Type/>

  </Document>

</Root>

 

Let me explain a bit about the above, you always need to have a root element, under which all the properties will be nested. in my case this is the 

<Root xmlns="Easy Document management">

part. The xmlns="Easy Document management" part is an XML Namespace, This will be the title for the XML file in Word.

 

Under the Root element, I created 2 new elements (Company and Document), and under those subjects I can place a lot of the properties I wanted to add to the document. As you can see the properties under Company and Document include a "/" after it, this is because all elements in a XML file have to be opened and closed. Usually you write it like <Element></Element> (remember, XML is case sensitive!!), but <Element/> is a self-closing tag, this way is a little shorter/faster and maybe even cleaner. As you can see the Company is opened and closed, and has all properties in between that are (in my case) related to the company. You don‘t have to do this, but for me it’s more convenient. So at the moment we have a valid XML file that we can import to Word (2010 and above).

 

Let’s open Word and navigate to the developers tab. If you don’t have the developers tab, click on File -> Options -> customize ribbon -> in the right pane check the box Developer and close the options menu. In the developer tab press “XML-mapping pane”. At the right you’ll see a new pane, click on the dropdown menu (Figure 1) and choose “(add new part...)” (Figure 2) and navigate to the XML file. You’ll see your data in a nice tree view (if you created nested elements in your XML data file) (Figure 3), if you already filled in some data in the XML itself, these will be automatically filled when you add them. To add one of these fields, right click the one you want to insert and choose what type of field it should be, Text without markup, Date or an image (sadly, images won’t synchronise).

Figure 1 Figure 2

Figure 1

Figure 2 Figure 3

 

Now when you later see that you forgot something in your XML file, there isn’t a nice edit button… You could also add a new XML file, but this isn’t that nice (I think).

Remember that this might be risky, so always make sure you have a backup before you try the following!

To change the XML file inside the document, you’ll need to have an Archiving program, I recommend 7-Zip, as it is free and Open Source. Save and close your Word document (and just to be sure create a backup), and open the Word document with your favorite archive tool. Open the folder customXml. In there you’ll see 5 items, 1 folder and 4 files (if you only imported 1 XML file) if you want to edit your XML file, you’ll need to edit item1.xml (so add the missing properties), save the XML, update the archive (so your edited XML get imported bank and close the archive). If you created an invalid XML file, Word will encounter an error, and will ask you to repair it, but most of the times the relationships will break, and not automatically synchronize anymore. When Word says that there’s an error, just press don’t repair and close the file. After that just pull your XML file through an XML validator and repair the problem yourself, this will save a lot of time reading all fields ;) .

Super Mario Maker Bookmark API (SMMB API) is a python scraper that makes it possible to do API like requests to the Super Mario Maker Bookmark website.

I've used BeautifulSoup in a few other projects I did, so the choice for that was quite easy. When I was finished writing the initial program, I wanted to create a web based API for everyone to use. I started looking in creating a web API for Python and quickly found Flask. I tried it out and it was super easy to port my python script to Flask! The hardest part was to set the API up on OpenShift (RedHat's free application hosting). I managed to do it, and it is now available for everyone! It's completely OpenSource, so you can host you own API, or use it directly via Python.

 

Check out https://github.com/rikels/SMMBAPI on how it works. I try to keep it as up to date as possible. If there's anything wrong, please let me know!

 

I was exploring Twitch’s IRC chat the other day and realized that it wasn't possible (as far as my knowledgement went back than) to check the chat without logging in via IRC. But I noted that if you join a channel on Twitch, without being logged in, the chat is readable in the web client. So I was curious about how that worked.

So I started looking into it, First I started a virtual machine and ran Wireshark (an awesome network sniffer). Then I loaded my own channel on the virtual machine on which I wasn't logged in to Twitch. The reason that I used a virtual machine is that my main machine has a lot of trafific which makes it harder for me to find the packets I need. After that I loaded the chat on my physical machine Where I was logged in to Twitch. So to made it myself as easy as possible, I sent a message “$test_unique_message” (Wireshark was already sniffing packets when I loaded the page, that is essential, else Wireshark wouldn't have recorded all packets from the start of loading the page).

So I wanted to find all packets that were sent between my virtual machine and Twitch’s IRC server. So I started searching for “test_unique_message” so I knew which IP address I had to look up. I set the options to search for a “String” in “Packet bytes” (as it was text in a packet).

But because Wireshark said “Continuation Data” it meant that it was a connection that was being kept alive, or at least, it wasn't the first message in the stream. That probably meant that ALL the IRC messages were probably in the same connection, so I used Wireshark’s feature “Follow TCP Stream” and (sadly enough, maybe to little of a challenge) there it was…

PASS blah

Nick justinfan332755

And it was easy to guess that the password was probably the same for all the justinfan accounts, which number they had probably didn't matter either (later on I tested random passwords, that also worked... but I recommend using the password blah, just to stay undercover and act like you just used the web client). So I just tried logging on to the IRC server with a random user: justinfan55218, password:blah… and I got in. Sure, you still can’t talk in the chat (well, technically you can, but all messages from the username justinfan won’t get forwarded/shown to other users), but with this you can create a “silent bot” (A bot that monitors the chat without showing that there is anyone, as justinfans also won’t get listed in the viewers section).

Because of earlier work on a bot for Twitch, I knew that when you pass TWITCHCLIENT 2 to the IRC server you also receive messages that someone has subscribed (which is what i needed that bot for). What the difference is between TWITCHCLIENT 2 and 4, I can’t tell… for more info about TWITCHCLIENT see: http://discuss.dev.twitch.tv/t/accessing-chat-information/307

 

Here is a Python script I wrote that logs all messages in 3 chats to their own file, so everything sent in rikels will be written to rikels.txt and eveything sent in rikels1 will be written to rikels1.txt. (I’m not a programmer, so there are probably a few things that could be more efficient).

import socket
import time
from random import random
import re


Twitch_user = "justinfan{random}".format(random=int(random()*9999999))
Twitch_pass = "blah"
Twitch_channel = ["rikels","rikels1","rikels2"]
chat_log_path = "/home/rikels/Twitch/"
sub = False
sub_time = None


def connect():
    irc = socket.socket()
    irc.connect(("irc.twitch.tv", 6667))
    irc.send("PASS {passw}\r\n".format(passw=Twitch_pass))
    irc.send("NICK {user}\r\n".format(user = Twitch_user))
    irc.send("USER {user} 199.9.253.199 bla :{user}\r\n".format(user=Twitch_user))
    irc.send("JOIN #{users}\r\n".format(users=",#".join(Twitch_channel)))
    #making sure we receive info that the subscribermode was turned off/on with "TWITCHCLIENT 2,3,4"
    #by enabling this, you'll lose the join/part messages
    # irc.send("TWITCHCLIENT 4\r\n")
    return(irc)

irc = connect()

while(True):
    #buff as in buffer, but that is a registered python word/function
    buff = irc.recv(8192)
    buff = buff.split("\r\n")
    for buf in buff[:-1]:
        #to keep the connection alive, we have to reply to the ping
        #Twitch terminates the connection if it doesn't receive a reply after 3 pings
        if "PING" in buf:
            print("received ping")
            buf = "PONG tmi.twitch.tv\r\n"
            irc.send(buf)
        if buf == ":tmi.twitch.tv RECONNECT":
            print("received reconnect :S?")
            irc = connect()
            #opening a file with a+ means append, and create if it doesn't exist
            try:
                extracted_info = re.match(":(\w+)!\w+@\w+.tmi.twitch.tv PRIVMSG #(\w+) :(.*)",buf)
                with open(chat_log_path+extracted_info.group(2)+".txt","a+") as chat_log:
                    chat_log.write("{time}{user}:{message}\r\n".format(time=int(time.time()),user=extracted_info.group(1),message=extracted_info.group(3)))
            except:
                try:
                    with open(chat_log_path+"Twitch_logger.txt","a+") as chat_log:
                        chat_log.write("{time}{message}\r\n".format(time=int(time.time()),message=buf))
                #if there is an error, this will print exactly what went wrong, and just continue if possible, because of the while loop
                #Without Try/Except the script would stop completely
                except Exception as error:
                    print(error)