{"id":1606,"date":"2014-03-11T21:25:13","date_gmt":"2014-03-11T20:25:13","guid":{"rendered":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/?p=1606"},"modified":"2014-03-11T21:25:13","modified_gmt":"2014-03-11T20:25:13","slug":"automatic-love","status":"publish","type":"post","link":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/2014\/03\/automatic-love\/","title":{"rendered":"Automatic love"},"content":{"rendered":"<p><span style=\"line-height: 1.5em;\">I think more people in libraries should learn scripting skills &#8211; that is how to write short computer programmes. The reason is simple &#8211; because it can help you do things quickly and easily that would otherwise be time consuming and dull. This is probably the main \u00a0reason I started to use code and scripts in my work, and if you ever find yourself doing a job regularly that is time consuming and\/or dull and thinking &#8216;there must be a better way to do this&#8217; it may well be a good project for learning to code.<\/span><\/p>\n<p>To give and example. I work on &#8216;Knowledgebase+&#8217; (KB+) &#8211; a shared service for electronic resource management run by Jisc in the UK. KB+ holds details on a whole range of electronic journals and related information including details of organisations providing or using the resources.<\/p>\n<p>I&#8217;ve just been passed the details of 79 new organisations to be added to the system. To create these normally would require a couple of pieces of information (including the name of the organisation) into a web form and click &#8216;submit&#8217;.<\/p>\n<p>While not the worst nor the most time consuming job in the world, it seemed like something that could be made quicker and easier through a short piece of code. If I do this in a sensible way, next time there is a list of organisations to add to the system, I can just re-use the same code to do the job again.<\/p>\n<p>Luckily I&#8217;d already been experimenting with automating some processes in KB+ so I had a head start, leaving me with just three things to do:<\/p>\n<ol>\n<li>Write code to extract the organisation name from the list I&#8217;d been given<\/li>\n<li>Find out how the &#8216;create organisation&#8217; form in KB+ worked<\/li>\n<li>Write code to replicate this process that could take the organisation name and other data as input, and create an organisation on KB+<\/li>\n<\/ol>\n<p>I&#8217;d been given the original list as a spreadsheet, so I just exported the list of organisation names as a csv to make it easy to read programmatically, after that writing code that opened the file, read a line at a time and found the name was trivial:<\/p>\n<pre>CSV.foreach(orgfile, :headers =&gt; true, :header_converters =&gt; :symbol) do |row|\r\n    org_name = row[:name]\r\nend<\/pre>\n<p>The code to trigger the creation of the organisation in KB+ was a simple http &#8216;POST&#8217; command (i.e. it is just a simple web form submission). The code I&#8217;d written previously essentially &#8216;faked&#8217; a browser session and logged into KB+ (I did this using a code library called &#8216;mechanize&#8217; which is specially designed for this type of thing), so it was simply a matter of finding the relevant URL and parameters for the &#8216;post&#8217;. I used the handy Firefox extension &#8216;Tamper Data&#8217; which allows you to see (and adjust) &#8216;POST&#8217; and &#8216;GET&#8217; requests sent from your browser &#8211; which allowed me to see the relevant information.<\/p>\n<p><a href=\"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-content\/uploads\/2014\/03\/Screen-Shot-2014-03-11-at-18.03.48.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1607 alignnone\" alt=\"Screenshot of Tamper Data\" src=\"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-content\/uploads\/2014\/03\/Screen-Shot-2014-03-11-at-18.03.48-1024x358.png\" width=\"614\" height=\"215\" srcset=\"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-content\/uploads\/2014\/03\/Screen-Shot-2014-03-11-at-18.03.48-1024x358.png 1024w, http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-content\/uploads\/2014\/03\/Screen-Shot-2014-03-11-at-18.03.48-300x105.png 300w, http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-content\/uploads\/2014\/03\/Screen-Shot-2014-03-11-at-18.03.48-900x315.png 900w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/a><\/p>\n<p><span style=\"line-height: 1.5em;\">The relevant details here are the URL at the top right of the form, and the list of &#8216;parameters&#8217; on the right.\u00a0<\/span>Since I&#8217;d already got the code that dealt with authentication, the code to carryout this &#8216;post&#8217; request looks like this<\/p>\n<pre>page = @magent.post(url, {\r\n  \"name\" =&gt; org_name,\r\n  \"sector\" =&gt; org_sector\r\n  })\r\nend<\/pre>\n<p>So &#8211; I&#8217;ve written less than 10 new lines of code and I&#8217;ve got everything I need to automate the creation of organisations in KB+ given a list in a CSV file.<\/p>\n<p>Do you have any jobs that involve dull, repetitive tasks? Ever find yourself re-keying bits of data? Why not learn to code?<\/p>\n<p>P.S. If you work on Windows, try looking at tools like <a href=\"http:\/\/www.macros.com\">MacroExpress<\/a>\u00a0or <a href=\"http:\/\/www.autohotkey.com\">AutoHotKey<\/a>, especially if &#8216;learning to code&#8217; sounds too daunting\/time-consuming<\/p>\n<p>P.P.S. Perfection is the enemy of the good &#8211; try to avoid getting yourself into an <a href=\"https:\/\/xkcd.com\/974\/\">XKCD &#8216;pass the salt&#8217;<\/a> situation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think more people in libraries should learn scripting skills &#8211; that is how to write short computer programmes. The reason is simple &#8211; because it can help you do things quickly and easily that would otherwise be time consuming and dull. This is probably the main \u00a0reason I started to use code and scripts [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1606","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/posts\/1606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/comments?post=1606"}],"version-history":[{"count":2,"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/posts\/1606\/revisions"}],"predecessor-version":[{"id":1609,"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/posts\/1606\/revisions\/1609"}],"wp:attachment":[{"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/media?parent=1606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/categories?post=1606"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.meanboyfriend.com\/overdue_ideas\/wp-json\/wp\/v2\/tags?post=1606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}