Guest User

Untitled

a guest
Jan 19th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1.  
  2. // spiffy sidebar updater
  3. // https://github.com/chromakode/reddit-sidebar-updater
  4. //
  5. // Copyright (c) 2014 Max Goodman.
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. // 1. Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // 2. Redistributions in binary form must reproduce the above copyright
  14. // notice, this list of conditions and the following disclaimer in the
  15. // documentation and/or other materials provided with the distribution.
  16. // 3. The name of the author or contributors may not be used to endorse or
  17. // promote products derived from this software without specific prior
  18. // written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. // SUCH DAMAGE.
  31.  
  32. // your subreddit must have a wiki page titled "sidebar_template", which can be
  33. // accessed by USERNAME (configured below). the template will be used as the
  34. // base content for the sidebar, with {{SCHEDULE}} replaced with a tabular
  35. // schedule from CALENDAR (configured below).
  36.  
  37. // find the id of the calendar you wish to use in the Google Calendar preferences.
  38. // it looks like this: abcdefhijklmnop@calendar.google.com
  39. // (you must be subscribed to this calendar in Google Calendar)
  40. var CALENDAR = '5nqfbv3iotsmerkbrt9qugvi4@group.calendar.google.com'
  41. var SUBREDDIT = 'the1099'
  42. var DAY_RANGE = 14 // how many days ahead to list events for
  43. var SCHEDULE_TIME_ZONE = 'America/New_York'
  44.  
  45. // register a "script" type reddit OAuth2 app at reddit.com/prefs/apps for these:
  46. var CLIENT_ID = '5PwPyAkidoO6Yg' // your OAuth2 client ID
  47. var CLIENT_SECRET = 'HC6kCbMqnbnqul94HTlQuc4Fh1k' // your OAuth2 client secret
  48.  
  49. // please create a user solely for this script and mod them with wiki permissions.
  50. var USERNAME = '1099calendar'
  51. var PASSWORD = 'kode'
  52.  
  53. // the maximum sidebar length, set by reddit
  54. var LENGTH_LIMIT = 10240
  55.  
  56. function updateSchedule() {
  57. // get an OAuth2 token
  58. var authData = UrlFetchApp.fetch('https://ssl.reddit.com/api/v1/access_token', {
  59. payload: {
  60. grant_type: 'password',
  61. scope: 'wikiread,wikiedit',
  62. username: USERNAME,
  63. password: PASSWORD
  64. },
  65. method: 'post',
  66. headers: {'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)}
  67. })
  68. authData = JSON.parse(authData)
  69. var authToken = authData['access_token']
  70.  
  71. // load the sidebar template from reddit's wiki
  72. var templateData = UrlFetchApp.fetch('https://oauth.reddit.com/r/' + SUBREDDIT + '/wiki/sidebar_template.json', {
  73. headers: {'Authorization': 'bearer ' + authToken}
  74. })
  75. templateData = JSON.parse(templateData)
  76. var template = templateData['data']['content_md']
  77. template = template
  78. .replace(/&/g, '&')
  79. .replace(/&lt;/g, '<')
  80. .replace(/&gt;/g, '>')
  81. .replace(/&quot;/g, '"')
  82.  
  83. // read the calendar for events and build our schedule table
  84. var calendar = CalendarApp.getCalendarById(CALENDAR)
  85. var now = new Date()
  86. var dateStart = new Date(now)
  87. // look at events starting with the beginning of the current day
  88. dateStart.setHours(0, 0, 0, 0)
  89. var dateEnd = new Date(now.getTime() + (DAY_RANGE * 24 * 60 * 60 * 1000))
  90. var events = calendar.getEvents(dateStart, dateEnd)
  91. var sidebarTable = ''
  92. var sidebarLength = template.length - '{{SCHEDULE}}'.length
  93. for (var i = 0; i < events.length; i++) {
  94. var event = events[i]
  95. var eventDate = event.getStartTime()
  96.  
  97. // these four fields form each table line
  98. var tableLine = [
  99. Utilities.formatDate(eventDate, SCHEDULE_TIME_ZONE, 'd MMM'),
  100. Utilities.formatDate(eventDate, SCHEDULE_TIME_ZONE, eventDate.getMinutes() != 0 ? 'h:mma' : 'ha').toLowerCase(),
  101. event.getTitle(),
  102. event.getDescription()
  103. ].join('|') + '\n'
  104.  
  105. if (sidebarLength + tableLine.length > LENGTH_LIMIT) {
  106. break
  107. } else {
  108. sidebarTable += tableLine
  109. sidebarLength += tableLine.length
  110. }
  111. }
  112.  
  113. // update the sidebar! :)
  114. var sidebar = template.replace('{{SCHEDULE}}', sidebarTable)
  115. UrlFetchApp.fetch('https://oauth.reddit.com/r/' + SUBREDDIT + '/api/wiki/edit', {
  116. payload: {
  117. content: sidebar,
  118. page: 'config/sidebar',
  119. reason: 'Automated Google Apps Script update @ ' + Utilities.formatDate(now, 'America/Los_Angeles', 'd MMM h:mma z')
  120. },
  121. method: 'post',
  122. headers: {'Authorization': 'bearer ' + authToken}
  123. })
  124. }
Add Comment
Please, Sign In to add comment