Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 1 | from apiclient.discovery import build |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 2 | from apiclient.discovery import build |
| 3 | from apiclient.errors import HttpError |
| 4 | from apiclient.ext.authtools import run |
| 5 | from apiclient.ext.file import Storage |
| 6 | from apiclient.oauth import CredentialsInvalidError |
| 7 | from apiclient.oauth import FlowThreeLegged |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 8 | |
| 9 | import Queue |
| 10 | import httplib2 |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 11 | import threading |
| 12 | import time |
| 13 | |
| 14 | # Uncomment to get detailed logging |
| 15 | # httplib2.debuglevel = 4 |
| 16 | |
| 17 | |
| 18 | NUM_THREADS = 4 |
| 19 | NUM_ITEMS = 40 |
| 20 | |
| 21 | queue = Queue.Queue() |
| 22 | |
| 23 | |
| 24 | class Backoff: |
| 25 | """Exponential Backoff |
| 26 | |
| 27 | Implements an exponential backoff algorithm. |
| 28 | """ |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 29 | |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 30 | def __init__(self, maxretries=8): |
| 31 | self.retry = 0 |
| 32 | self.maxretries = maxretries |
| 33 | self.first = True |
| 34 | |
| 35 | def loop(self): |
| 36 | if self.first: |
| 37 | self.first = False |
| 38 | return True |
| 39 | else: |
| 40 | return self.retry < self.maxretries |
| 41 | |
| 42 | def fail(self): |
| 43 | self.retry += 1 |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 44 | delay = 2 ** self.retry |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 45 | time.sleep(delay) |
| 46 | |
| 47 | |
| 48 | def start_threads(credentials): |
| 49 | # Start up NUM_THREADS to handle requests |
| 50 | |
| 51 | def process_requests(): |
| 52 | http = httplib2.Http() |
| 53 | http = credentials.authorize(http) |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 54 | credentials_ok = True |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 55 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 56 | while credentials_ok: |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 57 | request = queue.get() |
| 58 | backoff = Backoff() |
| 59 | while backoff.loop(): |
| 60 | try: |
| 61 | request.execute(http) |
| 62 | break |
| 63 | except HttpError, e: |
| 64 | if e.resp.status in [402, 403, 408, 503, 504]: |
| 65 | print "Increasing backoff, got status code: %d" % e.resp.status |
| 66 | backoff.fail() |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 67 | except CredentialsInvalidError: |
| 68 | print "Credentials no long valid. Exiting." |
| 69 | credentials_ok = False |
| 70 | break |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 71 | |
| 72 | print "Completed request" |
| 73 | queue.task_done() |
| 74 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 75 | |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 76 | for i in range(NUM_THREADS): |
| 77 | t = threading.Thread(target=process_requests) |
| 78 | t.daemon = True |
| 79 | t.start() |
| 80 | |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 81 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 82 | def main(): |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 83 | storage = Storage('moderator.dat') |
| 84 | credentials = storage.get() |
| 85 | if credentials is None or credentials.invalid == True: |
| 86 | moderator_discovery = build("moderator", "v1").auth_discovery() |
| 87 | |
| 88 | flow = FlowThreeLegged(moderator_discovery, |
| 89 | consumer_key='anonymous', |
| 90 | consumer_secret='anonymous', |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 91 | user_agent='python-threading-sample/1.0', |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 92 | domain='anonymous', |
| 93 | scope='https://www.googleapis.com/auth/moderator', |
| 94 | xoauth_displayname='Google API Client Example App') |
| 95 | |
| 96 | credentials = run(flow, storage) |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 97 | |
| 98 | start_threads(credentials) |
| 99 | |
| 100 | http = httplib2.Http() |
| 101 | http = credentials.authorize(http) |
| 102 | |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 103 | service = build("moderator", "v1", http=http) |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 104 | |
| 105 | series_body = { |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 106 | "data": { |
| 107 | "description": "An example of bulk creating topics", |
| 108 | "name": "Using threading and queues", |
| 109 | "videoSubmissionAllowed": False |
| 110 | } |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 111 | } |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 112 | try: |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 113 | series = service.series().insert(body=series_body).execute() |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 114 | print "Created a new series" |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 115 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 116 | for i in range(NUM_ITEMS): |
| 117 | topic_body = { |
| 118 | "data": { |
| 119 | "description": "Sample Topic # %d" % i, |
| 120 | "name": "Sample", |
| 121 | "presenter": "me" |
| 122 | } |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 123 | } |
Joe Gregorio | 1ae3e74 | 2011-02-25 15:17:14 -0500 | [diff] [blame] | 124 | topic_request = service.topics().insert(seriesId=series['id']['seriesId'], |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 125 | body=topic_body) |
| 126 | print "Adding request to queue" |
| 127 | queue.put(topic_request) |
| 128 | except CredentialsInvalidError: |
| 129 | print 'Your credentials are no longer valid.' |
| 130 | print 'Please re-run this application to re-authorize.' |
| 131 | |
Joe Gregorio | 0c73a67 | 2010-10-14 08:27:59 -0400 | [diff] [blame] | 132 | |
| 133 | queue.join() |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | main() |