| 84 | } |
| 85 | |
| 86 | func GetGists(session *Session) { |
| 87 | localCtx, cancel := context.WithCancel(session.Context) |
| 88 | defer cancel() |
| 89 | |
| 90 | observedKeys := map[string]bool{} |
| 91 | opt := &github.GistListOptions{} |
| 92 | |
| 93 | for c := time.Tick(sleep); ; { |
| 94 | client := session.GetClient() |
| 95 | gists, resp, err := client.Gists.ListAll(localCtx, opt) |
| 96 | |
| 97 | if err != nil { |
| 98 | if _, ok := err.(*github.RateLimitError); ok { |
| 99 | session.Log.Warn("Token %s rate limited. Reset at %s", client.Token, resp.Rate.Reset) |
| 100 | client.RateLimitedUntil = time.Until(resp.Rate.Reset.Time) |
| 101 | break |
| 102 | } |
| 103 | |
| 104 | if _, ok := err.(*github.AbuseRateLimitError); ok { |
| 105 | GetSession().Log.Fatal("GitHub API abused detected. Quitting...") |
| 106 | } |
| 107 | |
| 108 | GetSession().Log.Important("Error getting GitHub Gists... trying again", err) |
| 109 | } |
| 110 | |
| 111 | newGists := make([]*github.Gist, 0, len(gists)) |
| 112 | for _, e := range gists { |
| 113 | if observedKeys[e.GetID()] { |
| 114 | continue |
| 115 | } |
| 116 | |
| 117 | newGists = append(newGists, e) |
| 118 | } |
| 119 | |
| 120 | for _, e := range newGists { |
| 121 | observedKeys[e.GetID()] = true |
| 122 | session.Gists <- e.GetGitPullURL() |
| 123 | } |
| 124 | |
| 125 | opt.Since = time.Now() |
| 126 | |
| 127 | select { |
| 128 | case <-c: |
| 129 | continue |
| 130 | case <-localCtx.Done(): |
| 131 | cancel() |
| 132 | return |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func GetRepository(session *Session, id int64) *github.Repository { |
| 138 | client := session.GetClient() |