AngularJS Setup Environment

Similar to yesterday’s post, I started a basic AngularJS startup environment, something I can just grab and program with whenever I want to start a new angular app. It’s just a pain re-setting up the environment each time.

Borrow if you like or contribute, thanks!


Screenshot of github.com
angular-setup-environment
Initial Angular Environment Setup

Python URL Term Scrapper (Kind of Like a Very Dumb/Simple Watson)

I wrote this a while back to scrape a given URL page for all links it contains and then search those links for term relations. The Python scrapper first finds all links in a given url. It then searches all the links found for a list of search terms provided. It will return stats on the number of times specific provided terms show up.

This may come in handy while trying to find more information on a given topic. I’ve used it on Google searches (be careful, you can only scrape google once ever 8 or so seconds before you are locked out) and wikipedia pages to gather correlation statistics between topics.

It is old code… so there might be some errors. Keep me posted!

#!/usr/bin/env python
#ENSURE permissions are 755 in order to have script run as executable

import os, sys, re, datetime
from optparse import OptionParser
import logging, urllib2

def parsePage(link, list):
    searchList = {}
    try:
        f = urllib2.urlopen(link)
        data = f.read()
        for item in list:
            if (item.title() in data) or (item.upper() in data) or (item.lower() in data):
                searchList[item]=searchList[item]+1
                searchList["count"]=searchList["count"]+1
        return searchList
    except Exception, e:
        print "An error has occurred while parsing page " +str(link)+"."
        log.error(str(datetime.datetime.now())+" "+str(e))

def searchUrl(search):
    try:
        f = urllib2.urlopen(search)
        data = f.read()
        pattern = r"/wiki(/\S*)?$" #regular expression to find url
        links = re.findall(pattern, data)
        return links
    except Exception, e:
        print "An error has occurred while trying to reach the site."
        log.error(str(datetime.datetime.now())+" "+str(e))

def main():
    try:
        parser = OptionParser() #Help menu options
        parser.add_option("-u", "--url", dest="search", help="String containing URL to search.")
        parser.add_option("-f", "--file", dest="file", help="File containing search terms.")
        (options, args) = parser.parse_args()
        if not options.search or not options.file:
            parser.error('Term file or URL to scrape not given')
        else:
            urls = searchUrl(options.search)
            f = open(options.file, 'r')
            terms = f.readlines()
            for url in urls:
                parsePage(url, terms)
            print "Results:"
            print searchList
    except Exception, e:
        log.error(str(datetime.datetime.now())+" "+str(e))

if __name__ == "__main__":
    log = logging.getLogger("error") #create error log
    log.setLevel(logging.ERROR)
    formatter = logging.Formatter('[%(levelname)s] %(message)s')
    handler = logging.FileHandler('error.log')
    handler.setFormatter(formatter)
    log.addHandler(handler)
    try:
        main()
    except Exception, e:
        print "An error has occurred, please review the error.log for more details."
        log.error(str(datetime.datetime.now())+" "+str(e))

Creating a Sharepoint ‘Like’ through SOAP Requests

Sharepoint is not my most favorite environment to program in but I figure I’d share a cool javascript app I put together that communicates with Sharepoint’s SOAP API to add on to custom ‘like’ buttons (just like facebook!) With this feature, users can ‘like’ sharepoint items without going through the crazy sharepoint interface. This javascript can be added to a web interface, and users can ‘like’ items from there!

None of this garbage
None of this garbage

I wrote some javascript/jquery code that can easily retrieve the number of ‘likes’ for a sharepoint list item or list. The script basically queries Sharepoint’s built in social services for the tag count with SOAP POST calls.

Get Like Count

Here is the script to get ‘like’ count:


<script>

function GetLikeCountFromService(strURL) {

var count = 0;

//change the webMethod var to match up to the correct sharepoint _vti_bin location. (https://msdn.microsoft.com/en-us/library/office/Bb862916(v=office.12).aspx)

var webMethod = \'/_vti_bin/SocialDataService.asmx?op=GetTagTermsOnUrl\';

var soapEnv = "<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'> <soap:Body><GetTagTermsOnUrl xmlns=\'http://microsoft.com/webservices/SharePointPortalServer/SocialDataService\'> <url>" + strURL + "</url> <maximumItemsToReturn>1</maximumItemsToReturn> </GetTagTermsOnUrl> </soap:Body> </soap:Envelope>";



$.ajax({

type: "POST",

async: true,

url: webMethod,

data: soapEnv,

contentType: "text/xml; charset=utf-8",

dataType: "xml",

success: function(data, textStatus, XMLHttpRequest) {

count  = $(\'SocialTermDetail\', data).find(\'Count\').text();

}

});

return count;

}

</script>

Called by: GetLikeCountFromService(‘<LIST OR LIST ITEM URL>’)

Example: GetLikeCountFromService(‘http://mysharepoint/list/item’)

Here is the script to post ‘likes’ to an article. Once again it’s a nice javascript that can put into an HTML header or what not.

Emulate a ‘like’

Script to post a ‘like’:


function AddLikeCountFromService(strURL) {

//change the webMethod var to match up to the correct sharepoint _vti_bin location. (https://msdn.microsoft.com/en-us/library/office/Bb862916(v=office.12).aspx)

var webMethod = \'/_vti_bin/SocialDataService.asmx?op=GetAllTagTermsForUrlFolder\';

var soapEnv = "<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'><soap:Body><GetAllTagTermsForUrlFolder xmlns=\'http://microsoft.com/webservices/SharePointPortalServer/SocialDataService\'><urlFolder>"+strURL+"</urlFolder><maximumItemsToReturn>1</maximumItemsToReturn></GetAllTagTermsForUrlFolder></soap:Body></soap:Envelope>";

$.ajax({

type: "POST",

async: true,

url: webMethod,

data: soapEnv,

contentType: "text/xml; charset=utf-8",

dataType: "xml",

success: function(data, textStatus, XMLHttpRequest) {

var info  = ($(\'SocialTermDetail\', data).text()).split(" ");

var guid = (info[0]).substring(0, info[0].length - 1);

//change the webMethod var to match up to the correct sharepoint _vti_bin location. (https://msdn.microsoft.com/en-us/library/office/Bb862916(v=office.12).aspx)

webMethod = \'/_vti_bin/SocialDataService.asmx?op=AddTag\';

//In the soapEnv request, you can change the title field to anything. This is the title of the thing the user is liking.

soapEnv = "<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'> <soap:Body><AddTag xmlns=\'http://microsoft.com/webservices/SharePointPortalServer/SocialDataService\'><url>"+strURL+"</url><termID>"+guid+"</termID><title>I like it!</title><isPrivate>false</isPrivate></AddTag></soap:Body> </soap:Envelope>";

$.ajax({

type: "POST",

async: true,

url: webMethod,

data: soapEnv,

contentType: "text/xml; charset=utf-8",

dataType: "xml",

success: function(data, textStatus, XMLHttpRequest) {

}

});

}

});

Called By:

Example: AddLikeCountFromService(‘<LIST OR LIST ITEM URL>’)

AddLikeCountFromService(‘http://mysharepoint/list/item’)