Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 118 lines (92 sloc) 3.548 kb
ec715751 »
2011-09-16 Public version of githugs.
1 <?php
2
3 require_once './vendor/php-github-api/lib/Github/Autoloader.php';
4 require_once './lib/hugs.php';
5
6 $config = include './config.php';
7
8 Github_Autoloader::register();
9
10 $github = new Github_Client();
11
12 $following = $github->getUserApi()->getFollowing($config['github']['login']);
13
14 if (is_array($following) && count($following)) {
15 // Pick one at random.
16 $randomPerson = $following[array_rand($following)];
17
18 // Get a list of their repos.
19 $repos = $github->getRepoApi()->getUserRepos($randomPerson);
20
21 if (is_array($repos) && count($repos)) {
22 // Get the most recently changed repo.
23 usort($repos, function($a, $b) { return strtotime($b['pushed_at']) - strtotime($a['pushed_at']); });
24 $repo = $repos[0];
25
26 $branch = isset($repo['master_branch']) ? $repo['master_branch'] : 'master';
27
28 $commits = $github->getCommitApi()->getBranchCommits($repo['owner'], $repo['name'], $branch);
29
30 $hug = array(
31 'hug' => Hugs::random(),
32 'url' => 'https://github.com/' . $repo['owner'] . '/' . $repo['name'],
33 'project' => $repo['owner'] . '/' . $repo['name'],
34 'time' => time(),
35 'commit' => $commits[0]
36 );
37
38 $cookieJar = tempnam("~", "CURLCOOKIE");
39
40 $ch = curl_init();
41 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
42 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
43 curl_setopt($ch, CURLOPT_URL, 'https://github.com/login');
44 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
45 $html = trim(curl_exec($ch));
46
47 $dom = new DOMDocument();
48 $dom->preserveWhiteSpace = FALSE;
49 $dom->strictErrorChecking = FALSE;
50 @$dom->loadHTML($html);
51
52 // TODO: Use XPath, duh.
53 $token = FALSE;
54 if ($input = getElementByName($dom, 'input', 'authenticity_token')) {
55 $token = $input->getAttribute('value');
56 }
57
58 $post = array(
59 'authenticity_token' => $token,
60 'login' => $config['github']['login'],
61 'password' => $config['github']['password']
62 );
63
64 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
65 curl_setopt($ch, CURLOPT_URL, 'https://github.com/session');
66 $html = trim(curl_exec($ch));
67
68 curl_setopt($ch, CURLOPT_POSTFIELDS, FALSE);
69 curl_setopt($ch, CURLOPT_URL, 'https://github.com' . $hug['commit']['url']);
70 $html = trim(curl_exec($ch));
71
72 $dom = new DOMDocument();
73 @$dom->loadHTML($html);
74
75 $commit_id = FALSE;
76 if ($input = getElementByName($dom, 'input', 'commit_id')) {
77 $commit_id = $input->getAttribute('value');
78 }
79
80 $token = FALSE;
81 if ($input = getElementByName($dom, 'input', 'authenticity_token')) {
82 $token = $input->getAttribute('value');
83 }
84
85 $post = array(
86 'authenticity_token' => $token,
87 'commit_id' => $commit_id,
88 'comment[body]' => $hug['hug']
89 );
90
91 curl_setopt($ch, CURLOPT_URL, $hug['url'] . '/commit_comment/create');
92 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
93 $html = trim(curl_exec($ch));
94
95 try {
96 $m = new Mongo($config['db']['connection_string']);
97 $db = $m->selectDB($config['db']['name']);
98 $db->hugs->insert($hug);
99
100 print_r($hug);
101 }
102 catch (MongoConnectionException $e) {
103 echo '<h1>Database connection failure</h1><hr><p>I blame myself :(';
104 exit();
105 }
106 }
107 }
108
109 function getElementByName($dom, $tag, $name) {
110 $xpath = new DOMXpath($dom);
111 $nodeList = $xpath->query('//' . $tag . '[contains(@name, "' . $name . '")]');
112 foreach ($nodeList as $node) {
113 if ($node->getAttribute('name') == $name) {
114 return $node;
115 }
116 }
117 return FALSE;
118 }
Something went wrong with that request. Please try again.