给dabr添加多种图片上传服务

Felix Yan | 2010-01-23 | 523 views

众所周知,Dabr/奶瓶腿默认的图片上传服务Twitpic在很久以前就已经惨遭杯具,为了让广大手机推友能完善体验到Twitter衍生的强大的图片功能,Felix将现在流行的数种未被墙的图片上传服务(Twic.li/Mobypicture/imgur/Img.ly/TwitSnaps/Tweetphoto)加入了Dabr的代码中。
完成后的效果应该是这样:

下面就是我的修改啦!

在common/twitter.php里找到

1
2
3
4
  'twitpic' => array(
    'security' => true,
    'callback' => 'twitter_twitpic_page',
  ),

替换成

1
2
3
4
  'picture' => array(
    'security' => true,
    'callback' => 'twitter_picture_page',
  ),

(毕竟添加了很多种图片上传服务后,菜单也不能再叫twitpic了:-) )
再找到

1
  if ($_POST['message']) {

从这里一直到函数结束,替换为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  if ($_POST['message']) {
    $ms1 = stripslashes($_POST['message']);
    $ms2 = urlencode($ms1);
    switch ($_POST['service']){
      case 'twitpic':
        $response = twitter_process('http://twitpic.com/api/upload', array(
          'media' => '@'.$_FILES['media']['tmp_name'],
          'username' => user_current_username(),
          'password' => $GLOBALS['user']['password'],
        ));
        if (preg_match('#mediaurl>(.*)</mediaurl#', $response, $matches)) {
            //$id = $matches[1];
            twitter_update_pic($ms1.' '.$matches[1]);
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
 
      case 'imgly':
        if($_POST['service']=='imgly')
          $response = twitter_process('http://img.ly/api/upload', array(
            'media' => '@'.$_FILES['media']['tmp_name'],
            'username' => user_current_username(),
            'password' => $GLOBALS['user']['password'],
          ));
        if (preg_match('#mediaurl>(.*)</mediaurl#', $response, $matches)) {
            //$id = base64_encode($matches[1]);
            twitter_update_pic($ms1.' '.$matches[1]);
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
 
      case 'twicli':
        $response = twitter_process('http://twic.li/api/uploadPhotoAndTweet', array(
          'photo' => '@'.$_FILES['media']['tmp_name'],
          'tweet' => $ms2,
          'username' => user_current_username(),
          'password' => $GLOBALS['user']['password'],
        ));
        if (preg_match('#square_url>(.*)</square_url#', $response, $matches)) {
            //$id = base64_encode($matches[1]);
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
 
      case 'tweetphoto':
        $response = twitter_process('http://tweetphotoapi.com/api/upload.aspx', array(
          'media' => '@'.$_FILES['media']['tmp_name'],
          'api_key' => '<YOUR API KEY>',
          'username' => user_current_username(),
          'password' => $GLOBALS['user']['password'],
        ));
        if (preg_match('#MediaUrl>(.*)</MediaUrl#', $response, $matches)) {
            //$id = base64_encode($matches[1]);
            twitter_update_pic($ms1.' '.$matches[1]);
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
 
      case 'imgur':
        $response = twitter_process('http://imgur.com/api/upload', array(
          'image' => '@'.$_FILES['media']['tmp_name'],
          'key' => '<YOUR API KEY>',
        ));
        if (preg_match('#small_thumbnail>(.*)</small_thumbnail#', $response, $matches)) {
            //$id = base64_encode($matches[1]);
            twitter_update_pic($ms1.' '.$matches[1]);
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
 
      case 'twitsnaps':
        $response = twitter_process('http://www.twitsnaps.com/api.php', array(
          'file' => '@'.$_FILES['media']['tmp_name'],
          'message' => $ms2,
          'user_name' => user_current_username(),
          'password' => $GLOBALS['user']['password'],
        ));
        if (preg_match('#imageurl>(.*)</imageurl#', $response, $matches)) {
            //$id = base64_encode($matches[1]);
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
 
      case 'moby':
        $response = twitter_process('http://api.mobypicture.com' array(
          'action' => 'postMediaUrl',
          'i' => '@'.$_FILES['media']['tmp_name'],
          'u' => user_current_username(),
          'p' => $GLOBALS['user']['password'],
          'k' => MOBYPICTURE_API_KEY,
          's' => 'none',
          'format' => 'plain',
        ));
        if (preg_match('/(http:\/\/moby\.to\/.+)/', $response, $matches)) {
            //$id = base64_encode($matches[1]);
            twitter_update_pic($ms1.' '.$matches[1]);            
            twitter_refresh("picture/confirm/".$_POST['service']);
        } else {
            twitter_refresh("picture/fail/".$_POST['service']);
        }
        break;
    }
    $content = $response;
  } elseif ($query[1] == 'confirm') {
    $content = $_POST['service']."<p>Picture upload to $query[2] success.</p>";
  } elseif ($query[1] == 'fail') {
    $content = $_POST['service']."<p>Picture upload to $query[2] failed. No idea why!</p>";
  } else {
    $content = "<form method='post' action='picture' enctype='multipart/form-data'>Service: <select name='service'><option value='twicli'>Twic.li</option><option value='moby'>Mobypicture</option><option value='imgur'>imgur</option><option value='imgly'>Img.ly</option><option value='twitsnaps'>TwitSnaps</option><option value='tweetphoto'>TweetPhoto</option><option value='twitpic'>Twitpic</option></select><br />Image: <input type='file' name='media' /><br />Message: <input type='text' name='message' maxlength='120' /><br /><input type='submit' value='Upload' /></form>";
  }
  return theme('page', 'Picture Upload', $content);
}
 
function twitter_update_pic($status) { //simply update a status for picture function use
    $request = 'http://twitter.com/statuses/update.json';
    $post_data = array('source' => 'dabr', 'status' => $status);
    $b = twitter_process($request, $post_data);
}

其中,tweetphotoimgurmobypicture的使用是需要“API Key”的,可以到它们各自的网站申请:)

这样,六种其他的图片上传服务(Twic.li/Mobypicture/imgur/Img.ly/TwitSnaps/Tweetphoto)就成功地添加到dabr上啦:)

8 Tweets

  1. Kars CHINA Mozilla Firefox Windows says:

    猫猫~ 更新一下 OAuth 方式的上传~ ; )

  2. Sharling CHINA Google Chrome Windows says:

    BTW,t.byhh.info现在用的是最新版的奶瓶腿原装代码,但是注册页面的验证码显示不出来。。https://dabr.in/reg 也有这个问题,但原因貌似不太一样,很是诡异 >,<

  3. Sharling CHINA Google Chrome Windows says:

    第97行少了个逗号,ft

  4. dongyingtian CHINA Google Chrome Windows says:

    好牛,我是个大菜鸟,互联网技术如何入门呢?

  5. Showfom CHINA Mozilla Firefox Windows says:

    直接把你的发过来呗……

  6. Hexchain CHINA Mozilla Firefox Fedora Linux says:

    猫,您牛case那里给个换行/注释行不?看着累

  7. 星网 CHINA Mozilla Firefox Windows says:

    — —!我现在很囧!

  8. Kada CHINA Google Chrome Windows says:

    贴的代码还真够长的。

    • Felix Yan CHINA Mozilla Firefox Windows says:

      :)各图片服务的API调用都不一样,各自分开写就变这么长了,呵呵,不过逻辑上应该挺清晰:)

  9. 散人 CHINA Google Chrome Windows says:

    看不懂,来顶个先

  10. Kars CHINA Flock Windows says:

    好巧啊~ 我昨天 刚发了一篇 关于 dabr主题 的 日志 、、

  11. NetPuter POLAND Opera Mini says:

    顶!
    沙发怎么还空着……

Post a comment

Additional comments powered by BackType