├── .gitignore ├── LICENSE.md ├── README.md ├── api.cgi ├── images ├── audio.png ├── nothumb.png ├── play_overlay.png ├── search.png └── spinner.gif ├── index.html ├── py ├── Backfill.py ├── DB.py ├── Gonewild.py ├── Httpy.py ├── ImageUtils.py ├── Queries.py ├── Reddit.py ├── __init__.py ├── deluser.sh └── populatedb.py ├── serve.py └── ui ├── jquery.2.0.3.min.js ├── script.js ├── spinner.gif └── style.css /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_STORE 2 | *.db 3 | *.pyc 4 | *.swp 5 | *.jpg 6 | *.mp4 7 | /content/ 8 | /users/ 9 | history.log 10 | database.db* 11 | TODO 12 | /zips/ 13 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gonewilder 2 | ========== 3 | 4 | Content downloader. 5 | 6 | 7 | Installing 8 | ========== 9 | 10 | Requires: 11 | * Python 12 | * Python Imaging Library (PIL) 13 | * SQLite3 14 | 15 | Optional: 16 | * Apache (for web interface only) 17 | ** Files in root (`.`) and `py` directories need to be CGI Executable in Apache 18 | 19 | Install dependencies on Debian: 7 (wheezy) 20 | ========================================== 21 | 22 | ```bash 23 | apt-get install python2.7-dev python-tk python-setuptools python-pip python-dev libjpeg8-dev libjpeg8 tcl8.5-dev tcl8.5 zlib1g-dev zlib1g libsnack2-dev tk8.5-dev libwebp-dev libwebp2 vflib3-dev libfreetype6-dev libtiff5-dev libjbig-dev 24 | pip install pillow 25 | ``` 26 | 27 | Executing 28 | ========= 29 | 30 | Execute `Gonewild.py` in the `./py/` directory. Include no arguments to start infinite loop which checks for and downloads new content. Other options available, see: 31 | 32 | ```bash 33 | python Gonewild.py --help 34 | ``` 35 | -------------------------------------------------------------------------------- /api.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from py.Queries import Queries # Interacting w/ database 4 | from py.Gonewild import Gonewild 5 | 6 | from json import dumps 7 | 8 | from traceback import format_exc # Stack traces 9 | 10 | from cgi import FieldStorage # Query keys 11 | from cgitb import enable as cgi_enable; cgi_enable() # for debugging 12 | 13 | 14 | ''' Where the magic happens ''' 15 | def main(): 16 | keys = get_keys() 17 | 18 | # Input sanitization 19 | if not 'method' in keys: 20 | return {'error':'unspecified method'} 21 | if 'start' in keys and not keys['start'].isdigit(): 22 | return {'error':'start parameter must be numeric'} 23 | if 'count' in keys and not keys['count'].isdigit(): 24 | return {'error':'count parameter must be numeric'} 25 | 26 | method = keys['method'] 27 | 28 | if method == 'get_users': return get_users(keys) 29 | elif method == 'get_user': return get_user(keys) 30 | elif method == 'get_posts': return get_posts(keys) 31 | elif method == 'search': return search(keys) 32 | elif method == 'add_user': return add_user(keys) 33 | elif method == 'get_zip': return get_zip(keys) 34 | elif method == 'get_rip': return get_rip(keys) 35 | elif method == 'search_user': return search_user(keys) 36 | else: return {'error':'unexpected method'} 37 | 38 | 39 | ''' 40 | Get list of users 41 | ''' 42 | def get_users(keys): 43 | return Queries.get_users( 44 | sortby = keys.get('sort', ''), 45 | orderby = keys.get('order', ''), 46 | start = int(keys.get('start', 0)), 47 | count = int(keys.get('count', 10)) 48 | ) 49 | 50 | 51 | ''' 52 | Get posts/images for a specific user 53 | ''' 54 | def get_user(keys): 55 | if not 'user' in keys: 56 | return {'error' : 'user required for get_user API'} 57 | 58 | if keys.get('feed', 'posts') != 'posts': 59 | return Queries.get_user_comments( 60 | keys['user'], 61 | sortby = keys.get('sort', ''), 62 | orderby = keys.get('order', ''), 63 | start = int(keys.get('start', 0)), 64 | count = int(keys.get('count', 10)) 65 | ) 66 | else: 67 | return Queries.get_user_posts( 68 | keys['user'], 69 | sortby = keys.get('sort', ''), 70 | orderby = keys.get('order', ''), 71 | start = int(keys.get('start', 0)), 72 | count = int(keys.get('count', 10)) 73 | ) 74 | 75 | 76 | ''' 77 | Get list of posts 78 | ''' 79 | def get_posts(keys): 80 | return Queries.get_posts( 81 | user = keys.get('user', None), 82 | sortby = keys.get('sort', ''), 83 | orderby = keys.get('order', ''), 84 | start = int(keys.get('start', 0)), 85 | count = int(keys.get('count', 10)) 86 | ) 87 | 88 | 89 | ''' 90 | Search for user/post/comment 91 | ''' 92 | def search(keys): 93 | if not 'search' in keys: 94 | return {'error':'search parameter required for search method'} 95 | if not 'type' in keys: 96 | # Default search 97 | return Queries.search( 98 | keys['search'], 99 | start = int(keys.get('start', 0)), 100 | count = int(keys.get('count', 10)) 101 | ) 102 | elif keys['type'] == 'post': 103 | return Queries.search_posts( 104 | keys['search'], 105 | start = int(keys.get('start', 0)), 106 | count = int(keys.get('count', 10)) 107 | ) 108 | elif keys['type'] == 'user': 109 | return Queries.search_users( 110 | keys['search'], 111 | start = int(keys.get('start', 0)), 112 | count = int(keys.get('count', 10)) 113 | ) 114 | 115 | ''' 116 | Search by user 117 | ''' 118 | def search_user(keys): 119 | if not 'user' in keys: 120 | return {'error':'user required'} 121 | from py.DB import DB 122 | db = DB() 123 | cursor = db.conn.cursor() 124 | try: 125 | user = db.select_one('username', 'users', 'UPPER(username) like UPPER(?)', [keys['user']]) 126 | if user != None: 127 | return {'users' : [user]} 128 | except: 129 | pass 130 | q = ''' 131 | select username 132 | from users 133 | where UPPER(username) like UPPER(?) 134 | limit %d 135 | offset %d 136 | ''' % (keys.get('count', 10), keys.get('start', 0)) 137 | curexec = cursor.execute(q, ['%%%s%%' % keys['user'] ]) 138 | result = [] 139 | for (username,) in curexec: 140 | result.append(username) 141 | cursor.close() 142 | return { 143 | 'users' : result 144 | } 145 | 146 | ''' 147 | Add user to list 148 | ''' 149 | def add_user(keys): 150 | if not 'user' in keys: 151 | return {'error':'user not entered'} 152 | user = sanitize_user(keys['user']) 153 | if len(user) < 3: 154 | return {'error':'invalid username: "%s" -- too short' % user} 155 | if Queries.user_already_added(user): 156 | return {'error':'user already added'} 157 | gonewild = Gonewild() 158 | if not gonewild.user_has_gone_wild(keys['user']): 159 | return {'error':'user "%s" has not recently gone wild' % user} 160 | gonewild.db.add_user(user, new=True) 161 | return {'error':'added user "%s"' % user} 162 | 163 | 164 | def get_zip(keys): 165 | user = keys.get('user') 166 | album = keys.get('album', None) 167 | videos = keys.get('include_videos', 'false') 168 | include_videos = videos in ['true', 'True'] 169 | return Queries.get_zip( 170 | user, 171 | include_videos = include_videos, 172 | album = album 173 | ) 174 | 175 | 176 | def get_rip(keys): 177 | if not 'user' in keys: 178 | return {'error':'user not entered'} 179 | return Queries.get_rip(keys['user']) 180 | 181 | 182 | ##################### 183 | # HELPER METHODS 184 | 185 | def get_cookies(): # Get client cookies 186 | d = {} 187 | if not 'HTTP_COOKIE' in os.environ: return d 188 | cookies = os.environ['HTTP_COOKIE'].split(";") 189 | for cookie in cookies: 190 | cookie = cookie.strip() 191 | (key, value) = cookie.split('=') 192 | d[key] = value 193 | return d 194 | 195 | def get_keys(): # Get query keys 196 | form = FieldStorage() 197 | keys = {} 198 | for key in form.keys(): 199 | keys[key] = form[key].value 200 | return keys 201 | 202 | def sanitize_user(user): # lower() and strip() non-valid characters from user 203 | return ''.join([c if c.lower() in 'abcdefghijklmnopqrstuvwxyz1234567890_-' else '' for c in user]) 204 | 205 | 206 | ######################## 207 | # ENTRY POINT 208 | 209 | if __name__ == '__main__': 210 | print "Content-Type: application/json" 211 | print "" 212 | try: 213 | print dumps(main(), indent=2) 214 | except Exception, e: 215 | # Return stacktrace 216 | print dumps({'error': str(format_exc())}) 217 | print "\n\n" 218 | -------------------------------------------------------------------------------- /images/audio.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/images/audio.png -------------------------------------------------------------------------------- /images/nothumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/images/nothumb.png -------------------------------------------------------------------------------- /images/play_overlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/images/play_overlay.png -------------------------------------------------------------------------------- /images/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/images/search.png -------------------------------------------------------------------------------- /images/spinner.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/images/spinner.gif -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | gonewilder 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 | 19 |
20 | 21 |
22 |
23 | 24 | 25 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /py/Backfill.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | """ 4 | "Backfill" existing database data. 5 | Gets titles, permalinks, dates, etc from reddit. 6 | Overwrites existing 'bad' data with accurate data. 7 | """ 8 | 9 | from DB import DB 10 | from Reddit import Reddit 11 | from sys import exit, stdout 12 | from urllib2 import HTTPError 13 | from os import remove as osremove 14 | from ImageUtils import ImageUtils 15 | 16 | db = DB() 17 | reddit = Reddit() 18 | 19 | def backfill_users(): 20 | q_users = ''' 21 | select id,username 22 | from users 23 | where deleted = 0 24 | ''' 25 | cur = db.conn.cursor() 26 | execur = cur.execute(q_users) 27 | ids_and_users = execur.fetchall() # Get list of users + ids 28 | index = 0 29 | for (userid, username) in ids_and_users: 30 | index += 1 31 | print '(%d/%d) updating %s...' % (index, len(ids_and_users), username), 32 | stdout.flush() 33 | try: 34 | ui = Reddit.get_user_info(username) 35 | except Exception, e: 36 | print str(e) 37 | continue 38 | q_user = ''' 39 | update users 40 | set 41 | created = %d, 42 | username = "%s" 43 | where id = %d 44 | ''' % (ui.created, ui.name, userid) 45 | cur.execute(q_user) 46 | print 'done' 47 | 48 | cur.close() 49 | 50 | def backfill_posts(legacy=True): 51 | 52 | (username, password) = db.get_credentials('reddit') 53 | reddit.login(username, password) 54 | 55 | where = '' 56 | if legacy: 57 | where = 'where legacy = 1' 58 | cur = db.conn.cursor() 59 | query = ''' 60 | select id, userid, title, selftext, url, subreddit, over_18, created, legacy, permalink, ups, downs 61 | from posts 62 | %s 63 | order by id 64 | ''' % where 65 | total = 0 66 | ids_to_fetch = [] 67 | # Store existing values in dict 68 | for (postid, userid, title, selftext, url, subreddit, over_18, created, legacy, permalink, ups, downs) in cur.execute(query): 69 | ids_to_fetch.append(str(postid)) 70 | if len(ids_to_fetch) >= 99: 71 | total += len(ids_to_fetch) 72 | ids_to_fetch.append('1234') 73 | url = 'http://www.reddit.com/by_id/t3_%s.json' % ',t3_'.join(ids_to_fetch) 74 | try: 75 | posts = reddit.get(url) 76 | except HTTPError, e: 77 | print 'HTTPError: %s' % str(e) 78 | posts = [] 79 | for post in posts: 80 | oldpost = {} 81 | oldpost['title'] = post.title 82 | oldpost['url'] = post.url 83 | oldpost['selftext'] = post.selftext 84 | oldpost['subreddit'] = post.subreddit 85 | oldpost['created'] = int(post.created) 86 | oldpost['permalink'] = post.permalink() 87 | oldpost['over_18'] = int(post.over_18) 88 | oldpost['legacy'] = 0 89 | oldpost['id'] = post.id.rjust(6, '0') 90 | oldpost['ups'] = post.ups 91 | oldpost['downs'] = post.downs 92 | Reddit.debug('updating post %s by %s' % (post.id, post.author)) 93 | update_post(oldpost) 94 | db.conn.commit() 95 | ids_to_fetch = list() 96 | print 'running total: %d' % total 97 | 98 | if len(ids_to_fetch) > 0: 99 | total += len(ids_to_fetch) 100 | ids_to_fetch.append('1234') 101 | url = 'http://www.reddit.com/by_id/t3_%s.json' % ',t3_'.join(ids_to_fetch) 102 | try: 103 | posts = reddit.get(url) 104 | except HTTPError, e: 105 | print 'HTTPError: %s' % str(e) 106 | posts = [] 107 | for post in posts: 108 | oldpost = {} 109 | oldpost['title'] = post.title 110 | oldpost['url'] = post.url 111 | oldpost['selftext'] = post.selftext 112 | oldpost['subreddit'] = post.subreddit 113 | oldpost['created'] = int(post.created) 114 | oldpost['permalink'] = post.permalink() 115 | oldpost['over_18'] = int(post.over_18) 116 | oldpost['legacy'] = 0 117 | oldpost['id'] = post.id.rjust(6, '0') 118 | oldpost['ups'] = post.ups 119 | oldpost['downs'] = post.downs 120 | Reddit.debug('updating post %s by %s' % (post.id, post.author)) 121 | update_post(oldpost) 122 | db.conn.commit() 123 | print 'total posts updated: %d' % total 124 | 125 | def update_post(post): 126 | query = ''' 127 | update posts 128 | set 129 | title = ?, 130 | url = ?, 131 | selftext = ?, 132 | subreddit = ?, 133 | over_18 = ?, 134 | created = ?, 135 | permalink = ?, 136 | legacy = ?, 137 | ups = ?, 138 | downs = ? 139 | where 140 | id = ? 141 | ''' 142 | cur = db.conn.cursor() 143 | cur.execute(query, (post['title'], post['url'], post['selftext'], post['subreddit'], 144 | post['over_18'], post['created'], post['permalink'], 145 | post['legacy'], post['ups'], post['downs'], post['id']) ) 146 | cur.close() 147 | 148 | 149 | def backfill_comments(): 150 | (username, password) = db.get_credentials('reddit') 151 | reddit.login(username, password) 152 | 153 | cur = db.conn.cursor() 154 | query = ''' 155 | select 156 | id, 157 | userid, 158 | postid, 159 | subreddit, 160 | text, 161 | created, 162 | legacy, 163 | permalink, 164 | ups, 165 | downs 166 | from comments 167 | where legacy = 1 168 | order by id 169 | ''' 170 | execur = cur.execute(query) 171 | results = execur.fetchall() 172 | 173 | for (commentid, 174 | userid, 175 | postid, 176 | subreddit, 177 | text, 178 | created, 179 | legacy, 180 | permalink, 181 | ups, 182 | downs) in results: 183 | # Get comment from reddit 184 | post = Reddit.get('http://www.reddit.com/comments/%s/_/%s' % (postid, commentid)) 185 | if len(post.comments) > 0: 186 | comment = post.comments[0] 187 | # Update db 188 | query = ''' 189 | update comments 190 | set 191 | postid = ?, 192 | subreddit = ?, 193 | text = ?, 194 | created = ?, 195 | permalink = ?, 196 | legacy = 0, 197 | ups = ?, 198 | downs = ? 199 | where 200 | id = ? 201 | ''' 202 | cur.execute(query, (postid, subreddit, text, created, permalink, legacy, ups, downs, commentid) ) 203 | db.commit() 204 | cur.close() 205 | 206 | 207 | ''' 208 | Iterate over every user, 209 | Get most-recent post id, 210 | Set user's "last since" id to the latest post id 211 | ''' 212 | def backfill_last_since(): 213 | cur = db.conn.cursor() 214 | query = ''' 215 | select username,themax 216 | from 217 | users, 218 | ( 219 | select userid,max(posts.id) as themax 220 | from posts 221 | group by posts.userid 222 | ) 223 | where userid = users.id 224 | ''' 225 | for user,since in cur.execute(query).fetchall(): 226 | print user,since 227 | db.set_last_since_id(user, since) 228 | 229 | def backfill_videos(): 230 | query = ''' 231 | select id, path, thumb 232 | from images 233 | where type = 'video' 234 | and 235 | ( 236 | thumb like '%.mp4' 237 | or 238 | thumb like '%.flv' 239 | or 240 | thumb like '%.wmv' 241 | ) 242 | ''' 243 | cur = db.conn.cursor() 244 | for imgid, image, oldthumb in cur.execute(query).fetchall(): 245 | saveas = oldthumb 246 | saveas = '%s.png' % saveas[:saveas.rfind('.')] 247 | try: 248 | newthumb = ImageUtils.create_thumbnail(image, saveas) 249 | except Exception, e: 250 | print 'ERROR: %s' % str(e) 251 | continue 252 | print 'replacing %s with %s' % (oldthumb, newthumb) 253 | q = ''' 254 | update images 255 | set 256 | thumb = ? 257 | where 258 | id = ? 259 | ''' 260 | cur.execute(q, (newthumb, imgid)) 261 | db.commit() 262 | print 'removing %s...' % oldthumb, 263 | osremove(oldthumb) 264 | print 'removed' 265 | cur.close() 266 | 267 | if __name__ == '__main__': 268 | #backfill_users() 269 | #backfill_posts() 270 | #backfill_last_since() 271 | backfill_videos() 272 | -------------------------------------------------------------------------------- /py/DB.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import time 3 | from os import path, listdir 4 | from sys import stderr 5 | from shutil import copy2, rmtree 6 | from Reddit import Comment, Post 7 | from ImageUtils import ImageUtils 8 | 9 | try: import sqlite3 10 | except ImportError: import sqlite as sqlite3 11 | 12 | SCHEMA = { 13 | 'newusers' : 14 | '\n\t' + 15 | 'username text unique \n\t', 16 | 17 | 'users' : 18 | '\n\t' + 19 | 'id integer primary key autoincrement, \n\t' + 20 | 'username text unique, \n\t' + 21 | 'sinceid text, \n\t' + 22 | 'created integer, \n\t' + 23 | 'updated integer, \n\t' + 24 | 'deleted integer, \n\t' + 25 | 'blacklist integer, \n\t' + 26 | 'views integer, \n\t' + 27 | 'rating integer, \n\t' + 28 | 'ratings integer \n\t', 29 | 30 | 'posts' : 31 | '\n\t' + 32 | 'id text primary key, \n\t' + 33 | 'userid integer, \n\t' + 34 | 'title text, \n\t' + 35 | 'url text, \n\t' + 36 | 'selftext text, \n\t' + 37 | 'subreddit text, \n\t' + 38 | 'over_18 integer, \n\t' + 39 | 'created integer, \n\t' + 40 | 'legacy integer, \n\t' + 41 | 'permalink text, \n\t' + 42 | 'ups integer, \n\t' + 43 | 'downs integer, \n\t' + 44 | 'foreign key(userid) references users(id)\n\t', 45 | 46 | 'comments' : 47 | '\n\t' + 48 | 'id text primary key, \n\t' + 49 | 'userid integer, \n\t' + 50 | 'postid text, \n\t' + 51 | 'subreddit text, \n\t' + 52 | 'text text, \n\t' + 53 | 'created integer, \n\t' + 54 | 'legacy integer, \n\t' + 55 | 'permalink text, \n\t' + 56 | 'ups integer, \n\t' + 57 | 'downs integer, \n\t' + 58 | 'foreign key(userid) references users(id)\n\t', 59 | 60 | 'albums' : 61 | '\n\t' 62 | 'id integer primary key, \n\t' + 63 | 'path text unique, \n\t' + 64 | 'userid integer, \n\t' + 65 | 'url text, \n\t' + 66 | 'post text, \n\t' + 67 | 'comment text, \n\t' + 68 | 'views integer, \n\t' + 69 | 'foreign key(userid) references users(id)\n\t', 70 | 71 | 'images' : 72 | '\n\t' + 73 | 'id integer primary key, \n\t' + 74 | 'path text unique, \n\t' + 75 | 'userid integer, \n\t' + 76 | 'source text, \n\t' + 77 | 'width integer, \n\t' + 78 | 'height integer, \n\t' + 79 | 'size integer, \n\t' + 80 | 'thumb text, \n\t' + 81 | 'type text, \n\t' + # image/video 82 | 'albumid integer, \n\t' + 83 | 'post text, \n\t' + 84 | 'comment text, \n\t' + 85 | 'views integer, \n\t' + 86 | 'foreign key(userid) references users(id), \n\t' + 87 | 'foreign key(albumid) references albums(id)\n\t', 88 | 89 | 'zips' : 90 | '\n\t' + 91 | 'zippath text unique, \n\t' + 92 | 'user text, \n\t' + 93 | 'album text, \n\t' + 94 | 'images integer, \n\t' + 95 | 'videos integer, \n\t' + 96 | 'audios integer, \n\t' + 97 | 'size integer \n\t', 98 | 99 | 'credentials' : 100 | '\n\t' + 101 | 'site text primary key, \n\t' + 102 | 'username text, \n\t' + 103 | 'password text \n\t', 104 | 105 | 'config' : 106 | '\n\t' + 107 | 'key text primary key, \n\t' + 108 | 'value text \n\t', 109 | 110 | 'friends' : 111 | '\n\t' + 112 | 'username text primary key\n\t', 113 | } 114 | 115 | DB_FILE = path.join(ImageUtils.get_root(), 'database.db') 116 | 117 | class DB: 118 | def __init__(self): 119 | self.logger = stderr 120 | if path.exists(DB_FILE): 121 | self.debug('__init__: using database file: %s' % DB_FILE) 122 | else: 123 | self.debug('__init__: database file (%s) not found, creating...' % DB_FILE) 124 | self.conn = None 125 | self.conn = sqlite3.connect(DB_FILE) #TODO CHANGE BACK, encoding='utf-8') 126 | self.conn.text_factory = lambda x: unicode(x, "utf-8", "ignore") 127 | # Don't create tables if not supplied. 128 | if SCHEMA != None and SCHEMA != {} and len(SCHEMA) > 0: 129 | # Create table for every schema given. 130 | for key in SCHEMA: 131 | self.create_table(key, SCHEMA[key]) 132 | 133 | def debug(self, text): 134 | tstamp = time.strftime('[%Y-%m-%dT%H:%M:%SZ]', time.gmtime()) 135 | text = '%s DB: %s' % (tstamp, text) 136 | self.logger.write('%s\n' % text) 137 | if self.logger != stderr: 138 | stderr.write('%s\n' % text) 139 | 140 | def create_table(self, table_name, schema): 141 | cur = self.conn.cursor() 142 | query = '''create table if not exists %s (%s)''' % (table_name, schema) 143 | cur.execute(query) 144 | self.commit() 145 | cur.close() 146 | 147 | def commit(self): 148 | try_again = True 149 | while try_again: 150 | try: 151 | self.conn.commit() 152 | try_again = False 153 | except: 154 | time.sleep(1) 155 | 156 | def insert(self, table, values): 157 | cur = self.conn.cursor() 158 | try: 159 | questions = '' 160 | for i in xrange(0, len(values)): 161 | if questions != '': questions += ',' 162 | questions += '?' 163 | exec_string = '''insert into %s values (%s)''' % (table, questions) 164 | result = cur.execute(exec_string, values) 165 | last_row_id = cur.lastrowid 166 | cur.close() 167 | return last_row_id 168 | except sqlite3.IntegrityError: 169 | cur.close() 170 | return -1 171 | 172 | def delete(self, table, where, values=[]): 173 | cur = self.conn.cursor() 174 | q = ''' 175 | delete from %s 176 | where %s 177 | ''' % (table, where) 178 | cur.execute(q, values) 179 | 180 | def get_cursor(self): 181 | return self.conn.cursor() 182 | 183 | def count(self, table, where='', values=[]): 184 | return self.select_one('count(*)', table, where, values=values) 185 | 186 | def select(self, what, table, where='', values=[]): 187 | cur = self.conn.cursor() 188 | query = ''' 189 | select %s 190 | from %s 191 | ''' % (what, table) 192 | if where != '': 193 | query += 'where %s' % (where) 194 | cur.execute(query, values) 195 | results = [] 196 | for result in cur: 197 | results.append(result) 198 | cur.close() 199 | return results 200 | 201 | def select_one(self, what, table, where='', values=[]): 202 | cur = self.conn.cursor() 203 | if where != '': 204 | where = 'where %s' % where 205 | query = ''' 206 | select %s 207 | from %s 208 | %s 209 | ''' % (what, table, where) 210 | execur = cur.execute(query, values) 211 | one = execur.fetchone() 212 | cur.close() 213 | return one[0] 214 | 215 | def update(self, table, what, where='', values=[]): 216 | cur = self.conn.cursor() 217 | if where != '': 218 | where = 'where %s' % where 219 | query = ''' 220 | update %s 221 | set %s 222 | %s 223 | ''' % (table, what, where) 224 | execur = cur.execute(query, values) 225 | one = execur.fetchone() 226 | cur.close() 227 | 228 | def execute(self, statement): 229 | cur = self.conn.cursor() 230 | result = cur.execute(statement) 231 | return result 232 | 233 | ##################### 234 | # GW-specific methods 235 | 236 | ''' Add user to list of either 'users' or 'newusers' table ''' 237 | def add_user(self, user, new=False): 238 | cur = self.conn.cursor() 239 | if new: 240 | q = ''' 241 | insert into newusers values ("%s") 242 | ''' % user 243 | else: 244 | now = int(time.time()) 245 | q = 'insert into users values (' 246 | q += 'NULL,' # user id 247 | q += '"%s",' % user # username 248 | q += ' "" ,' # since id 249 | q += ' %d ,' % now # created 250 | q += ' %d ,' % now # updated 251 | q += ' 0 ,' # deleted 252 | q += ' 0 ,' # blacklisted 253 | q += '0,0,0)' # views, rating, ratings 254 | try: 255 | cur.execute(q) 256 | except sqlite3.IntegrityError, e: 257 | self.debug('add_user: user "%s" already exists in %susers: %s' % (user, 'new' if new else '', str(e))) 258 | raise e 259 | self.commit() 260 | 261 | def remove_user(self, user): 262 | userid = self.get_user_id(user) 263 | user = self.select_one('username', 'users', where='id = ?', values=[userid]) 264 | self.delete('posts', 'userid = ?', [userid]) 265 | self.delete('comments', 'userid = ?', [userid]) 266 | self.delete('albums', 'userid = ?', [userid]) 267 | self.delete('users', 'UPPER(username) like UPPER(?)', [user]) 268 | self.delete('newusers', 'UPPER(username) like UPPER(?)', [user]) 269 | dirpath = path.join(ImageUtils.get_root(), 'content', user) 270 | if path.exists(dirpath): 271 | rmtree(dirpath) 272 | 273 | ''' Finds user ID for username; creates new user if not found ''' 274 | def get_user_id(self, user): 275 | cur = self.conn.cursor() 276 | results = cur.execute(''' 277 | select id 278 | from users 279 | where UPPER(username) like UPPER("%s") 280 | ''' % user) 281 | users = results.fetchall() 282 | if len(users) == 0: 283 | self.add_user(user, new=False) 284 | results = cur.execute(''' 285 | select id 286 | from users 287 | where UPPER(username) like UPPER("%s") 288 | ''' % user) 289 | users = results.fetchall() 290 | cur.close() 291 | return users[0][0] 292 | 293 | ''' True if user has been added to 'users' or 'newusers', False otherwise ''' 294 | def user_already_added(self, user): 295 | cur = self.conn.cursor() 296 | results = cur.execute(''' 297 | select * 298 | from users 299 | where UPPER(username) like UPPER("%s") 300 | ''' % user) 301 | if len(results.fetchall()) > 0: 302 | return True 303 | results = cur.execute(''' 304 | select * 305 | from newusers 306 | where UPPER(username) like UPPER("%s") 307 | ''' % user) 308 | return len(results.fetchall()) > 0 309 | 310 | def get_last_since_id(self, user): 311 | cur = self.conn.cursor() 312 | results = cur.execute(''' 313 | select sinceid 314 | from users 315 | where UPPER(username) like UPPER("%s") 316 | ''' % user) 317 | return results.fetchall()[0][0] 318 | 319 | def set_last_since_id(self, user, since_id): 320 | cur = self.conn.cursor() 321 | query = ''' 322 | update users 323 | set sinceid = "%s" 324 | where UPPER(username) like UPPER("%s") 325 | ''' % (since_id, user) 326 | cur.execute(query) 327 | self.commit() 328 | 329 | def add_post(self, post, legacy=0): 330 | userid = self.get_user_id(post.author) 331 | values = [ ( 332 | post.id, # reddit post id 333 | userid, # id of user in 'users' table 334 | post.title, # title of reddit post 335 | post.selftext, # selftext 336 | post.url, # reddit post url 337 | post.subreddit, # subreddit 338 | post.over_18, # NSFW 339 | post.created, # UTC timestamp 340 | legacy, # If post was generated (legacy) or retrieved in-full from reddit 341 | post.permalink(),# link to post on reddit, 342 | post.ups, # upvotes 343 | post.downs # downvotes 344 | ) ] 345 | q = 'insert into posts values (%s)' % ','.join(['?'] * len(values[0])) 346 | cur = self.conn.cursor() 347 | try: 348 | result = cur.executemany(q, values) 349 | except sqlite3.IntegrityError, e: # Column already exists 350 | raise Exception('post already exists in DB (%s): %s' % (post.id, str(e))) 351 | cur.close() 352 | self.commit() 353 | 354 | def add_comment(self, comment, legacy=0): 355 | userid = self.get_user_id(comment.author) 356 | values = [ ( 357 | comment.id, # reddit comment id 358 | userid, # id of user in 'users' table 359 | comment.post_id, # reddit post id 360 | comment.subreddit, # subreddit 361 | comment.body, # body of comment 362 | comment.created, # utc timestamp 363 | legacy, # if comment was 'generated' (legacy) or retrieved from reddit 364 | comment.permalink(),# link to comment 365 | comment.ups, # upvotes 366 | comment.downs # downvotes 367 | ) ] 368 | q = 'insert into comments values (%s)' % ','.join(['?'] * len(values[0])) 369 | cur = self.conn.cursor() 370 | try: 371 | result = cur.executemany(q, values) 372 | except sqlite3.IntegrityError, e: # Column already exists 373 | raise Exception('comment already exists in DB (%s): %s' % (comment.id, str(e))) 374 | cur.close() 375 | self.commit() 376 | 377 | def add_album(self, path, user, url, postid, commentid): 378 | userid = self.get_user_id(user) 379 | values = [ ( 380 | None, # albumid 381 | path, # path to album (filesystem) 382 | userid, # if of user in 'users' table 383 | url, # url to album 384 | postid, # reddit post id 385 | commentid, # reddit comment id 386 | 0 # views 387 | ) ] 388 | q = 'insert into albums values (%s)' % ','.join(['?'] * len(values[0])) 389 | cur = self.conn.cursor() 390 | try: 391 | result = cur.executemany(q, values) 392 | except sqlite3.IntegrityError, e: # Column already exists 393 | raise Exception('album already exists in DB (%s): %s' % (path, str(e))) 394 | lastrow = cur.lastrowid 395 | cur.close() 396 | self.commit() 397 | return lastrow 398 | 399 | def album_exists(self, album_url): 400 | return self.count('albums', 'url = ?', [album_url]) 401 | 402 | ''' 403 | Add an "image" to the database. Might be a video 404 | ''' 405 | def add_image(self, path, user, url, width, height, size, thumb, 406 | mediatype, albumid, postid, commentid): 407 | userid = self.get_user_id(user) 408 | values = [ ( 409 | None, # imageid 410 | path, # path to image (locally) 411 | userid, # id of user in 'users' table 412 | url, # image source 413 | width, # image width 414 | height, # image height 415 | size, # size of image (in bytes) 416 | thumb, # path to thumbnail (locally) 417 | mediatype, # 'image' or 'video' 418 | albumid, # album in which the image is contained 419 | postid, # reddit post 420 | commentid, # reddit comment 421 | 0 # views 422 | ) ] 423 | q = 'insert into images values (%s)' % ','.join(['?'] * len(values[0])) 424 | cur = self.conn.cursor() 425 | try: 426 | result = cur.executemany(q, values) 427 | except sqlite3.IntegrityError, e: # Column already exists 428 | raise Exception('image already exists in DB (%s): %s' % (path, str(e))) 429 | lastrow = cur.lastrowid 430 | cur.close() 431 | self.commit() 432 | return lastrow 433 | 434 | ''' 435 | Get list of (non-deleted) users. 436 | If "new" is flagged: 437 | * Deletes list of 'newusers' 438 | * Adds 'newusers' to 'users' list. 439 | * Returns list of 'newusers' 440 | ''' 441 | def get_users(self, new=False): 442 | if new: q = 'select username from newusers' 443 | else: q = 'select username from users where deleted = 0' 444 | cur = self.conn.cursor() 445 | users = cur.execute(q).fetchall() 446 | if new: 447 | # Delete list of new users, add to new users list 448 | for user in [x[0] for x in users]: 449 | delq = 'delete from newusers where UPPER(username) like UPPER("%s")' % user 450 | cur.execute(delq) 451 | try: self.add_user(user, new=False) 452 | except: pass 453 | cur.close() 454 | self.commit() 455 | else: 456 | cur.close() 457 | return [str(x[0]) for x in users] 458 | 459 | ######################## 460 | # STUPID EXTRA FUNCTIONS 461 | 462 | def get_post_comment_id(self, pci): 463 | if not '_' in pci: 464 | raise Exception('unable to find post/comment/imgid from filename %s' % pci) 465 | (pc, i) = pci.split('_') 466 | if '-' in pc: 467 | (post, comment) = pc.split('-') 468 | else: 469 | post = pc 470 | comment = None 471 | return (post, comment, i) 472 | 473 | ''' 474 | Copy old image (/users//...) to new format (/content//...) 475 | Create new thumbnail 476 | Derive values for post/comment from filename 477 | ''' 478 | def add_existing_image(self, user, oldimage, oldpath, subdir='', album_id=-1): 479 | if 'tumblr' in oldpath: 480 | # Can't properly handle tumblr links 481 | self.debug('cannot properly handle tumblr links; trying anyway') 482 | #return 483 | if subdir == '' and album_id == -1: 484 | self.debug('adding image: %s' % oldpath) 485 | # Ensure image is an actual image 486 | try: 487 | dims = ImageUtils.get_dimensions(oldpath) 488 | except: 489 | self.debug('failed to load image: %s, skipping' % oldpath) 490 | return 491 | newimage = path.join(ImageUtils.get_root(), 'content', user, subdir, oldimage) 492 | newimage = newimage.replace('.jpeg.jpg', '.jpg') 493 | thumbnail = path.join(ImageUtils.get_root(), 'content', user, subdir, 'thumbs', oldimage) 494 | thumbnail = thumbnail.replace('.jpeg.jpg', '.jpg') 495 | if path.exists(newimage): 496 | self.debug('new image already exists: %s' % newimage) 497 | return 498 | 499 | ImageUtils.create_subdirectories(path.join(ImageUtils.get_root(), 'content', user, subdir, 'thumbs')) 500 | 501 | copy2(oldpath, newimage) 502 | try: 503 | ImageUtils.create_thumbnail(newimage, thumbnail) 504 | except Exception, e: 505 | self.debug('failed to create thumbnail: %s' % str(e)) 506 | thumbnail = path.join(ImageUtils.get_root(), 'images', 'nothumb.png') 507 | 508 | (post, comment, imgid) = self.get_post_comment_id(oldimage) 509 | url = 'http://i.imgur.com/%s' % imgid 510 | dims = ImageUtils.get_dimensions(newimage) 511 | size = path.getsize(newimage) 512 | try: 513 | ImageUtils.create_thumbnail(newimage, thumbnail) 514 | except Exception, e: 515 | self.debug('add_existing_image: create_thumbnail failed: %s' % str(e)) 516 | thumbnail = path.join(ImageUtils.get_root(), 'images', 'nothumb.png') 517 | try: 518 | self.add_image(newimage, user, url, 519 | dims[0], dims[1], size, thumbnail, 'image', 520 | album_id, post, comment) 521 | except Exception, e: 522 | self.debug('add_existing_image: failed: %s' % str(e)) 523 | return 524 | 525 | if subdir == '' and album_id == -1: # Not an album 526 | # Add post 527 | p = Post() 528 | p.id = post 529 | p.author = user 530 | if comment == None: p.url = url 531 | p.created = path.getctime(oldpath) 532 | p.subreddit = '' 533 | p.title = '' 534 | try: 535 | self.add_post(p, legacy=1) 536 | except Exception, e: 537 | self.debug('add_existing_image: create post failed: %s' % str(e)) 538 | 539 | # Add comment 540 | if comment != None: 541 | c = Comment() 542 | c.id = comment 543 | c.post_id = post 544 | c.author = user 545 | if comment != None: c.body = url 546 | p.created = path.getctime(oldpath) 547 | try: 548 | self.add_comment(c, legacy=1) 549 | except Exception, e: 550 | self.debug('add_existing_image: create comment failed: %s' % str(e)) 551 | 552 | def add_existing_album(self, user, oldalbum, oldpath): 553 | newalbum = path.join(ImageUtils.get_root(), 'content', user, oldalbum) 554 | if path.exists(newalbum): 555 | self.debug('album already exists: %s' % newalbum) 556 | return 557 | 558 | (post, comment, imgid) = self.get_post_comment_id(oldalbum) 559 | url = 'http://imgur.com/a/%s' % imgid 560 | try: 561 | album_id = self.add_album(newalbum, user, url, post, comment) 562 | except Exception, e: 563 | self.debug('add_existing_album: failed: %s' % str(e)) 564 | return 565 | 566 | for image in listdir(oldpath): 567 | self.debug('add_existing_album: image=%s' % path.join(oldpath, image)) 568 | fakeimage = post 569 | if comment != None: 570 | fakeimage = '%s-%s' % (fakeimage, comment) 571 | fakeimage = '%s_%s' % (fakeimage, image.split('_')[-1]) 572 | self.add_existing_image(user, fakeimage, path.join(oldpath, image), subdir=oldalbum, album_id=album_id) 573 | 574 | # Add post 575 | p = Post() 576 | p.id = post 577 | p.author = user 578 | if comment == None: p.url = url 579 | p.created = path.getctime(oldpath) 580 | p.subreddit = '' 581 | p.title = '' 582 | try: 583 | self.add_post(p, legacy=1) 584 | except Exception, e: 585 | #self.debug('add_existing_image: %s' % str(e)) 586 | pass 587 | 588 | # Add comment 589 | if comment != None: 590 | c = Comment() 591 | c.id = comment 592 | c.post_id = post 593 | c.author = user 594 | if comment != None: c.body = url 595 | p.created = path.getctime(oldpath) 596 | try: 597 | self.add_comment(c, legacy=1) 598 | except Exception, e: 599 | #self.debug('add_existing_image: %s' % str(e)) 600 | pass 601 | 602 | def get_credentials(self, site): 603 | if self.count('credentials', 'site = ?', [site]) == 0: 604 | raise Exception('Credentials for %s not found in database, run "Gonewild.py --help" for more info' % site) 605 | 606 | q = 'select username,password from credentials where site = "%s"' % site 607 | cur = self.conn.cursor() 608 | (username, password) = cur.execute(q).fetchone() 609 | cur.close() 610 | return (username, password) 611 | 612 | def set_credentials(self, site, username, password): 613 | cur = self.conn.cursor() 614 | try: 615 | q = 'insert into credentials values (?,?,?)' 616 | cur.execute(q, [site, username, password]) 617 | cur.close() 618 | self.commit() 619 | except Exception, e: 620 | #self.debug('[!] unable to add new credentials: %s' % str(e)) 621 | q = 'update credentials set username = ?, password = ? where site = ?' 622 | try: 623 | result = cur.execute(q, [username, password, site]) 624 | cur.close() 625 | self.commit() 626 | except Exception, e: 627 | self.debug('[!] unable to update existing credentials: %s' % str(e)) 628 | from traceback import format_exc 629 | self.debug('\n%s' % format_exc()) 630 | raise e 631 | 632 | def update_user(self, user): 633 | cur = self.conn.cursor() 634 | query = ''' 635 | update users 636 | set updated = %d 637 | where UPPER(USERNAME) like UPPER(?) 638 | ''' % int(time.time()) 639 | cur.execute(query, [user]) 640 | self.commit() 641 | 642 | def get_excluded_subreddits(self): 643 | csv_subs = self.get_config('excluded_subreddits') 644 | if csv_subs == None or csv_subs.strip() == '': 645 | return [] 646 | return csv_subs.split(',') 647 | 648 | def add_excluded_subreddit(self, subreddit): 649 | subs = self.get_excluded_subreddits() 650 | if subreddit.strip().lower() in subs: 651 | raise Exception('subreddit "%s" already exists in list of excluded subreddits: %s' % (subreddit.strip().lower(), str(subs))) 652 | subs.append(subreddit.strip().lower()) 653 | self.set_config('excluded_subreddits', ','.join(subs)) 654 | 655 | def remove_excluded_subreddit(self, subreddit): 656 | subs = self.get_excluded_subreddits() 657 | if not subreddit.strip().lower() in subs: 658 | raise Exception('subreddit "%s" not found in list of excluded subreddits: %s' % (subreddit.strip().lower(), str(subs))) 659 | subs.remove(subreddit.strip().lower()) 660 | self.set_config('excluded_subreddits', ','.join(subs)) 661 | 662 | def mark_as_deleted(self, user): 663 | cur = self.conn.cursor() 664 | query = ''' 665 | update users 666 | set deleted = 1 667 | where UPPER(username) like UPPER("%s") 668 | ''' % (user) 669 | cur.execute(query) 670 | self.commit() 671 | 672 | def already_friend(self, user): 673 | return self.count('friends', 'UPPER(username) = UPPER(?)', [user]) > 0 674 | 675 | def add_friend(self, user): 676 | cur = self.conn.cursor() 677 | cur.execute('insert into friends values (?)', [user]) 678 | self.commit() 679 | 680 | def remove_friend(self, user): 681 | self.delete('friends', 'UPPER(username) like UPPER(?)', [user]) 682 | self.commit() 683 | 684 | def get_friends_list(self): 685 | result = [] 686 | for friend in self.select('username', 'friends'): 687 | result.append(friend[0]) 688 | return result 689 | 690 | def get_users_list(self): 691 | result = [] 692 | for user in self.select('username', 'users'): 693 | result.append(user[0]) 694 | for user in self.select('username', 'newusers'): 695 | result.append(user[0]) 696 | return result 697 | 698 | def get_config(self, key, default=None): 699 | cur = self.conn.cursor() 700 | query = ''' 701 | select value 702 | from config 703 | where key = "%s" 704 | ''' % key 705 | try: 706 | execur = cur.execute(query) 707 | result = execur.fetchone()[0] 708 | cur.close() 709 | except Exception, e: 710 | return default 711 | return result 712 | 713 | def set_config(self, key, value): 714 | cur = self.conn.cursor() 715 | query = ''' 716 | insert or replace into config (key, value) 717 | values ("%s", "%s") 718 | ''' % (key, value) 719 | try: 720 | execur = cur.execute(query) 721 | result = execur.fetchone() 722 | self.commit() 723 | cur.close() 724 | except Exception, e: 725 | self.debug('failed to set config key "%s" to value "%s": %s' % (key, value, str(e))) 726 | 727 | 728 | if __name__ == '__main__': 729 | db = DB() 730 | try: db.add_user('4_pr0n') 731 | except: pass 732 | db.set_last_since_id('4_pr0n', 'ccs4ule') 733 | print db.get_last_since_id('4_pr0n') 734 | -------------------------------------------------------------------------------- /py/Gonewild.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from DB import DB 4 | from os import path, mkdir, devnull 5 | from sys import stderr 6 | from Reddit import Reddit, Child, Post, Comment 7 | from ImageUtils import ImageUtils 8 | from time import strftime, gmtime 9 | 10 | ''' 11 | Brings everything together: 12 | * Retrieves gonewild posts and content, 13 | * Adds results to database 14 | ''' 15 | class Gonewild(object): 16 | 17 | def __init__(self): 18 | # Single file that all output is written to, to track usage 19 | self.exit_if_already_started() 20 | self.db = DB() # Database instance 21 | 22 | log_level = self.db.get_config('log_level', default='user') 23 | if log_level == 'none': 24 | self.root_log = open(devnull, 'w') 25 | else: 26 | self.root_log = open(path.join(ImageUtils.get_root(), 'history.log'), 'a') 27 | self.logger = self.root_log # Logger used by helper classes 28 | 29 | self.reddit = Reddit() 30 | self.excluded_subs = self.db.get_excluded_subreddits() 31 | 32 | def debug(self, text): 33 | tstamp = strftime('[%Y-%m-%dT%H:%M:%SZ]', gmtime()) 34 | text = '%s Gonewild: %s' % (tstamp, text) 35 | self.root_log.write('%s\n' % text) 36 | if self.logger != self.root_log: 37 | self.logger.write('%s\n' % text) 38 | stderr.write('%s\n' % text) 39 | 40 | def user_already_added(self, user): 41 | return self.db.user_already_added(user) 42 | 43 | def user_has_gone_wild(self, user): 44 | # Look at last 100 submissions 45 | try: 46 | children = self.reddit.get_user('%s/submitted' % user, max_pages=1) 47 | except Exception: 48 | # User is 404 49 | return False 50 | for child in children: 51 | if type(child) == Post: 52 | if 'gonewild' in child.subreddit.lower() or \ 53 | 'gw' in child.subreddit.lower() or \ 54 | 'asstastic' in child.subreddit.lower() or \ 55 | 'girlsgone' in child.subreddit.lower() or \ 56 | 'gone' in child.subreddit.lower(): 57 | return True 58 | return False 59 | 60 | def add_excluded_subreddit(self, subreddit): 61 | return self.db.add_excluded_subreddit(subreddit) 62 | 63 | def setup_loggers_for_user(self, user): 64 | # Create directories if needed 65 | user_dir = path.join(ImageUtils.get_root(), 'content', user) 66 | ImageUtils.create_subdirectories(user_dir) 67 | # Setup logger 68 | log_level = self.db.get_config('log_level', default='user') 69 | if log_level == 'none': self.logger = open(devnull, 'w') 70 | elif log_level == 'user': self.logger = open(path.join(user_dir, 'history.log'), 'a') 71 | elif log_level == 'global': self.logger = self.root_log 72 | self.db.logger = self.logger 73 | ImageUtils.logger = self.logger 74 | self.reddit.logger = self.logger 75 | 76 | def restore_loggers(self): 77 | log_level = self.db.get_config('log_level', default='user') 78 | if log_level == 'user': 79 | self.logger.close() 80 | self.logger = self.root_log 81 | self.db.logger = self.logger 82 | ImageUtils.logger = self.logger 83 | self.reddit.logger = self.logger 84 | 85 | def is_excluded_child(self, child): 86 | if child.subreddit.lower() in [x.lower() for x in self.excluded_subs]: 87 | self.debug('''%s: poll_user: Ignoring post/comment in excluded subreddit ("%s") 88 | Permalink: %s 89 | Ignored: %s''' % (child.author, child.subreddit, child.permalink(), str(child))) 90 | return True 91 | return False 92 | 93 | def get_and_process_urls_from_child(self, child): 94 | urls = self.get_urls(child) 95 | try: 96 | if type(child) == Post: 97 | self.db.add_post(child) 98 | elif type(child) == Comment: 99 | self.db.add_comment(child) 100 | except Exception, e: 101 | if 'already exists' not in str(e): 102 | self.debug('%s: poll_user: %s' % (child.author, str(e))) 103 | return # If we can't add the post/comment to DB, skip it 104 | if len(urls) > 0: 105 | self.debug('%s: poll_user: found %d url(s) in child %s' % (child.author, len(urls), child.permalink())) 106 | for url_index, url in enumerate(urls): 107 | self.process_url(url, url_index, child) 108 | 109 | def poll_user(self, user): 110 | ''' 111 | Gets new posts/comments for user, 112 | Finds URLs in posts/comments, 113 | "Processes" (downloads) URLs, 114 | Adds results to database. 115 | ''' 116 | self.setup_loggers_for_user(user) 117 | 118 | since_id = self.db.get_last_since_id(user) 119 | # Get posts/comments for user 120 | self.debug('%s: poll_user: since "%s"' % (user, since_id)) 121 | try: 122 | children = self.reddit.get_user(user, since=since_id) 123 | except Exception, e: 124 | if '404: Not Found' in str(e): 125 | # User is deleted, mark it as such 126 | self.debug('%s: poll_user: user is 404, marking as deleted' % user) 127 | self.db.mark_as_deleted(user) 128 | return 129 | self.debug('%s: poll_user: error %s' % (user, str(e))) 130 | return 131 | 132 | if len(children) == 0: 133 | #self.debug('%s: poll_user: no new posts/comments found' % user) 134 | return 135 | 136 | self.debug('%s: poll_user: %d new posts and comments found' % (user, len(children))) 137 | 138 | for child in children: 139 | # Ignore certain subreddits 140 | if self.is_excluded_child(child): 141 | continue 142 | 143 | self.get_and_process_urls_from_child(child) 144 | 145 | self.debug('%s: poll_user: done' % user) 146 | 147 | # Set last 'since' to the most-recent post/comment ID 148 | self.debug('%s: poll_user: setting most-recent since_id to "%s"' % (user, children[0].id)) 149 | self.db.set_last_since_id(user, children[0].id) 150 | 151 | def poll_friends(self): 152 | ''' 153 | Retrieve posts & comments from /r/friends. 154 | Scrape new content, store in database. 155 | ''' 156 | 157 | for friend_url in ['/r/friends/new', '/r/friends/comments']: 158 | children = self.reddit.get('http://www.reddit.com%s.json' % friend_url) 159 | self.debug('poll_friends: loaded %d items from %s' % (len(children), friend_url)) 160 | for child in children: 161 | user = child.author 162 | if user == '[deleted]': continue 163 | 164 | # Add friend as 'user' in DB if needed 165 | if not self.db.user_already_added(user): 166 | self.db.add_user(user) 167 | 168 | # Check child.id matches the child.author's lastsinceid in DB 169 | lastsinceid = self.db.get_last_since_id(user) 170 | if lastsinceid == child.id: 171 | # We've already retrieved this post 172 | continue 173 | 174 | # Setup loggers 175 | self.setup_loggers_for_user(user) 176 | 177 | # Ignore excluded subreddits 178 | if self.is_excluded_child(child): 179 | continue 180 | 181 | self.get_and_process_urls_from_child(child) 182 | 183 | # Close loggers 184 | self.restore_loggers() 185 | 186 | 187 | ''' Returns list of URLs found in a reddit child (post or comment) ''' 188 | def get_urls(self, child): 189 | if type(child) == Post: 190 | if child.selftext != None and child.selftext != '': 191 | return self.reddit.get_links_from_text(child.selftext) 192 | elif child.url != None: 193 | return [child.url] 194 | return [] 195 | elif type(child) == Comment: 196 | return self.reddit.get_links_from_text(child.body) 197 | raise Exception('unsupported child type: %s' % child) 198 | 199 | ''' Downloads media(s) at url, adds to database. ''' 200 | def process_url(self, url, url_index, child): 201 | self.debug('%s: process_url: %s' % (child.author, url)) 202 | 203 | # Ignore duplicate albums 204 | if self.db.album_exists(url): 205 | self.debug('''%s: process_url: album %s already exists in database. 206 | Permalink: %s 207 | Object: %s''' % (child.author, url, child.permalink(), str(child))) 208 | return 209 | 210 | userid = self.db.get_user_id(child.author) 211 | if type(child) == Post: 212 | base_fname = '%s-%d' % (child.id, url_index) 213 | postid = child.id 214 | commid = None 215 | elif type(child) == Comment: 216 | base_fname = '%s-%s-%d' % (child.post_id, child.id, url_index) 217 | postid = child.post_id 218 | commid = child.id 219 | 220 | working_dir = path.join(ImageUtils.get_root(), 'content', child.author) 221 | 222 | # A single URL can contain multiple medias (i.e. albums) 223 | try: 224 | (media_type, albumname, medias) = ImageUtils.get_urls(url) 225 | except Exception, e: 226 | self.debug('%s: process_url: unable to get URLs for %s: %s' % (child.author, url, str(e))) 227 | if 'domain not supported' in str(e): 228 | # Save domain-not-supported URLs to new file 229 | user_dir = path.join(ImageUtils.get_root(), 'content', child.author) 230 | f = open(path.join(user_dir, 'unsupported.txt'), 'a') 231 | f.write(url) 232 | f.write('\n') 233 | f.flush() 234 | f.close() 235 | return 236 | 237 | if albumname != None: 238 | # Album! 239 | albumname = '%s-%s' % (base_fname, albumname) 240 | working_dir = path.join(working_dir, albumname) 241 | #self.debug('%s: process_url: adding album to database' % child.author) 242 | album_id = self.db.add_album( 243 | working_dir, 244 | child.author, 245 | url, 246 | postid, 247 | commid, 248 | ) 249 | else: 250 | album_id = None 251 | 252 | if self.db.get_config('save_thumbnails', default='true') == 'true': 253 | ImageUtils.create_subdirectories(path.join(working_dir, 'thumbs')) 254 | else: 255 | ImageUtils.create_subdirectories(working_dir) 256 | 257 | for media_index, media in enumerate(medias): 258 | # Construct save path: /user/post[-comment]-index-filename 259 | fname = ImageUtils.get_filename_from_url(media, media_type) 260 | fname = '%s-%02d-%s' % (base_fname, media_index, fname) 261 | saveas = path.join(working_dir, fname) 262 | 263 | # Download URL 264 | try: 265 | self.debug('%s: process_url: downloading #%d %s' % (child.author, media_index + 1, media)) 266 | headers = { 267 | 'Referer' : url 268 | } 269 | ImageUtils.httpy.download(media, saveas, headers=headers) 270 | if path.getsize(saveas) == 503: 271 | raise Exception('503b = removed') 272 | except Exception, e: 273 | self.debug('%s: process_url: failed to download #%d: %s, moving on' % (child.author, media_index + 1, str(e))) 274 | continue 275 | 276 | # Get media information (width, height, thumbsaveas) 277 | if media_type == 'audio': 278 | # Audio files don't have width/height/thumbnail 279 | width = height = 0 280 | savethumbas = path.join(ImageUtils.get_root(), 'images', 'audio.png') 281 | else: 282 | try: 283 | (width, height) = ImageUtils.get_dimensions(saveas) 284 | except Exception, e: 285 | # If we cannot process the media file, skip it! 286 | self.debug('%s: process_url: #%d %s' % (child.author, media_index + 1, str(e))) 287 | continue 288 | 289 | # Create thumbnail if needed 290 | if self.db.get_config('save_thumbnails', 'true') == 'false': 291 | savethumbas = path.join(ImageUtils.get_root(), 'images', 'nothumb.png') 292 | else: 293 | savethumbas = path.join(working_dir, 'thumbs', fname) 294 | try: 295 | savethumbas = ImageUtils.create_thumbnail(saveas, savethumbas) 296 | except Exception, e: 297 | savethumbas = path.join(ImageUtils.get_root(), 'images', 'nothumb.png') 298 | self.debug('%s: process_url: failed to create thumb #%d: %s, using default' % (child.author, media_index + 1, str(e))) 299 | 300 | size = path.getsize(saveas) 301 | 302 | # Add to DB 303 | self.db.add_image( 304 | saveas, 305 | child.author, 306 | media, 307 | width, 308 | height, 309 | size, 310 | savethumbas, 311 | media_type, 312 | album_id, 313 | postid, 314 | commid 315 | ) 316 | self.db.update_user(child.author) 317 | 318 | def infinite_loop(self): 319 | users = self.db.get_users(new=False) 320 | 321 | last_user = self.db.get_config('last_user') 322 | last_index = 0 if last_user == None or last_user not in users else users.index(last_user) 323 | 324 | while True: 325 | # Look for and poll newly-added users 326 | newusers = self.db.get_users(new=True) 327 | for newuser in newusers: 328 | # Add new user to existing list 329 | users.append(newuser) 330 | # Add user to friends list if applicable 331 | friend_zone = self.db.get_config('friend_zone') 332 | if friend_zone == None or friend_zone == 'none': 333 | self.add_friend(newuser) 334 | self.poll_user(newuser) # Poll new user for content 335 | 336 | last_index += 1 337 | if last_index >= len(users): 338 | last_index = 0 339 | # Get top users if it's enabled 340 | if self.db.get_config('add_top_users') != 'false': 341 | for new_top_user in self.add_top_users(): 342 | # Add top users to users list 343 | if not new_top_user.lower() in [x.lower() for x in users]: 344 | users.append(new_top_user) 345 | 346 | # Check if there are actually users to retrieve 347 | if len(users) == 0: 348 | self.debug('no users to retrieve. exiting') 349 | break 350 | 351 | user = users[last_index] 352 | # Add user to friends list if applicable 353 | friend_zone = self.db.get_config('friend_zone') 354 | if friend_zone == 'only' or friend_zone == 'some': 355 | if not self.db.already_friend(user): 356 | self.add_friend(user) 357 | 358 | # Scan for updates from friends 359 | try: 360 | self.poll_friends() 361 | except Exception, e: 362 | self.debug('infinite_loop: poll_friends: %s' % str(e)) 363 | from traceback import format_exc 364 | print format_exc() 365 | 366 | # Poll user if applicable 367 | if friend_zone != 'only': 368 | try: 369 | self.poll_user(user) # Poll user for content 370 | self.db.set_config('last_user', user) 371 | except Exception, e: 372 | self.debug('infinite_loop: poll_user: %s' % str(e)) 373 | from traceback import format_exc 374 | print format_exc() 375 | 376 | def add_top_users(self): 377 | users = [] 378 | subs = ['gonewild'] 379 | self.debug('add_top_users: loading top posts for the week from %s' % ','.join(subs)) 380 | try: 381 | posts = self.reddit.get('http://www.reddit.com/r/%s/top.json?t=week' % '+'.join(subs)) 382 | except Exception, e: 383 | self.debug('add_top_users: Exception: %s' % str(e)) 384 | return users 385 | for post in posts: 386 | if post.author == '[deleted]': continue 387 | if not self.db.user_already_added(post.author): 388 | self.debug('add_top_users: Found new user, adding /u/%s' % post.author) 389 | self.db.add_user(post.author, new=True) 390 | friend_zone = self.db.get_config('friend_zone') 391 | if friend_zone == None or friend_zone == 'none': 392 | self.add_friend(post.author) 393 | users.append(post.author) 394 | return users 395 | 396 | 397 | def add_friend(self, user): 398 | try: 399 | self.reddit.add_friend(user) 400 | except Exception, e: 401 | self.debug(str(e)) 402 | return 403 | 404 | if self.db.already_friend(user): 405 | self.debug('Warning: User /u/%s is already considered a "friend" in the DB; tried to friend on reddit anyway' % user) 406 | else: 407 | self.db.add_friend(user) 408 | self.debug('User /u/%s saved as friend on reddit & DB' % user) 409 | 410 | def remove_friend(self, user): 411 | try: 412 | self.reddit.remove_friend(user) 413 | except Exception, e: 414 | self.debug(str(e)) 415 | return 416 | 417 | if not self.db.already_friend(user): 418 | self.debug('Warning: User /u/%s is not considered a friend in the DB; tried to unfriend on reddit anyway' % user) 419 | else: 420 | self.db.remove_friend(user) 421 | self.debug('User /u/%s removed as friend on reddit & DB' % user) 422 | 423 | def compare_friends(self, add_friends=False): 424 | db_users = self.db.get_users_list() 425 | db_friends = self.db.get_friends_list() 426 | self.login() 427 | try: 428 | reddit_friends = self.reddit.get_friends_list() 429 | except Exception, e: 430 | self.debug(str(e)) 431 | reddit_friends = [] 432 | self.debug('%d total users, %d friends in DB, %d friends on reddit' % (len(db_users), len(db_friends), len(reddit_friends))) 433 | 434 | need2add = [] 435 | 436 | # Add friends from reddit to the DB 437 | for friend in reddit_friends: 438 | if friend.lower() not in [x.lower() for x in db_friends]: 439 | self.db.add_friend(friend) 440 | self.debug('Added reddit friend to DB: /u/%s' % friend) 441 | 442 | # Add friends in DB to reddit's friends list 443 | for friend in db_friends: 444 | if friend.lower() not in [x.lower() for x in reddit_friends]: 445 | need2add.append(friend) 446 | 447 | # Add users from DB to reddit's friends list 448 | for friend in db_users: 449 | if friend.lower() not in [x.lower() for x in db_friends]: 450 | need2add.append(friend) 451 | elif friend.lower() not in [x.lower() for x in reddit_friends]: 452 | need2add.append(friend) 453 | 454 | # Remove duplicates 455 | need2add = list(set(need2add)) 456 | 457 | if len(need2add) > 0: 458 | if add_friends: 459 | self.debug('Synchronizing friends...') 460 | for friend in need2add: 461 | self.add_friend(friend) 462 | self.debug('Added /u/%s as a friend on reddit' % friend) 463 | else: 464 | self.debug('Found %d users that are not friended. to friend them, execute:\npython Gonewild.py --friend %s' % (len(need2add), ','.join(need2add))) 465 | 466 | def toggle_addtop(self): 467 | if self.db.get_config('add_top_users') != 'false': 468 | self.db.set_config('add_top_users', 'false') 469 | self.debug('Will stop automatically adding top users from http://reddit.com/r/gonewild/top?t=week') 470 | else: 471 | self.db.set_config('add_top_users', 'true') 472 | self.debug('Will automatically add top users from http://reddit.com/r/gonewild/top?t=week') 473 | 474 | def print_posts(self, user): 475 | userid = self.db.get_user_id(user) 476 | posts = self.db.select('id,title,url,selftext,subreddit,created,permalink,ups,downs', 'posts', 'userid = ? order by created asc', [userid]) 477 | for (postid, title, url, selftext, subreddit, created, permalink, ups, downs) in posts: 478 | output = [''] 479 | output.append( 'Permalink: %s' % permalink) 480 | output.append( ' Title: %s' % title.replace('\n', '')) 481 | if url != None: 482 | output.append(' Url/Text: %s' % url) 483 | elif selftext != None: 484 | output.append(' Url/Text: %s' % selftext) 485 | output.append( ' Date: %s' % strftime('%y-%m-%dT%H:%M:%SZ', gmtime(created))) 486 | output.append( ' Votes: +%d/-%d' % (ups, downs)) 487 | print '\n'.join(output) 488 | 489 | def print_comments(self, user): 490 | userid = self.db.get_user_id(user) 491 | comments = self.db.select('id,subreddit,text,created,permalink,ups,downs', 'comments', 'userid = ? order by created asc', [userid]) 492 | for (commentid, subreddit, body, created, permalink, ups, downs) in comments: 493 | output = [''] 494 | output.append( 'Permalink: %s' % permalink) 495 | output.append( ' Date: %s' % strftime('%y-%m-%dT%H:%M:%SZ', gmtime(created))) 496 | output.append( ' Votes: +%d/-%d' % (ups, downs)) 497 | output.append( ' Comment: %s' % body.replace('\n\n', '\n').replace('\n', '\n ')) 498 | print '\n'.join(output) 499 | 500 | def exit_if_already_started(self): 501 | from commands import getstatusoutput 502 | (status, output) = getstatusoutput('ps aux') 503 | running_processes = 0 504 | for line in output.split('\n'): 505 | if 'python' in line and 'Gonewild.py' in line and not '/bin/sh -c' in line: 506 | running_processes += 1 507 | if running_processes > 1: 508 | exit(0) # Quit silently if the bot is already running 509 | 510 | def login(self): 511 | try: 512 | (username, password) = self.db.get_credentials('reddit') 513 | try: 514 | self.reddit.login(username, password) 515 | except Exception, e: 516 | self.debug('login: Failed to login to reddit: %s' % str(e)) 517 | raise e 518 | except Exception, e: 519 | self.debug('login: Failed to get reddit credentials: %s' % str(e)) 520 | raise e 521 | 522 | def setup_config(self): 523 | keys = { 524 | 'save_thumbnails' : 'true', 525 | 'add_top_users' : 'true', 526 | 'excluded_subreddits' : '', 527 | 'friend_zone' : 'some', 528 | 'last_user' : '' 529 | } 530 | for (key,value) in keys.iteritems(): 531 | if self.db.get_config(key) == None: 532 | self.db.set_config(key, value) 533 | 534 | def handle_arguments(gw): 535 | import argparse 536 | parser = argparse.ArgumentParser(description=''' 537 | Gonewild content aggregator. 538 | Run without any arguments to start scraping in an infinite loop. 539 | Be sure to add a working reddit account before scraping. 540 | Arguments can continue multiple values (separated by commas) 541 | ''') 542 | 543 | parser.add_argument('--add', '-a', 544 | help='Add user(s) to scan for new content', 545 | metavar='USER') 546 | parser.add_argument('--add-top', '-tz', 547 | help='Toggle adding top users from /r/gonewild', 548 | action='store_true') 549 | parser.add_argument('--remove', 550 | help='Remove user from database', 551 | metavar='USER') 552 | 553 | parser.add_argument('--exclude', 554 | help='Add subreddit to exclude (ignore)', 555 | metavar='SUBREDDIT') 556 | parser.add_argument('--include', 557 | help='Remove subreddit from excluded list', 558 | metavar='SUBREDDIT') 559 | 560 | parser.add_argument('--friend', 561 | help='Add user(s) to reddit "friends" list', 562 | metavar='USER') 563 | parser.add_argument('--unfriend', 564 | help='Remove user(s) from reddit "friends" list', 565 | metavar='USER') 566 | parser.add_argument('--no-friend-zone', 567 | help='Do not poll /r/friends, only user pages (default)', 568 | action='store_true') 569 | parser.add_argument('--friend-zone', 570 | help='Poll both /r/friends AND user pages', 571 | action='store_true') 572 | parser.add_argument('--just-friends', 573 | help='Only use /r/friends; Don\'t poll user pages', 574 | action='store_true') 575 | parser.add_argument('--sync-friends', 576 | help='Synchronizes database with reddit\'s friends list', 577 | action='store_true') 578 | parser.add_argument('--reddit', 579 | help='Store reddit user account credentials', 580 | nargs=2, 581 | metavar=('user', 'pass')) 582 | parser.add_argument('--soundcloud', 583 | help='Store soundcloud API credentials', 584 | nargs=2, 585 | metavar=('api', 'key')) 586 | 587 | parser.add_argument('--backfill-thumbnails', 588 | help='Attempt to create missing thumbnails', 589 | action='store_true') 590 | 591 | parser.add_argument('--comments', 592 | help='Dump all comments for a user', 593 | metavar='USER') 594 | parser.add_argument('--posts', 595 | help='Print all posts made by a user', 596 | metavar='USER') 597 | 598 | parser.add_argument('--log', 599 | help='Set logging level (global, user, none)', 600 | metavar='LEVEL') 601 | 602 | parser.add_argument('--config', 603 | help='Show or set configuration values', 604 | nargs='*', 605 | metavar=('key', 'value')) 606 | 607 | args = parser.parse_args() 608 | 609 | if args.friend_zone: 610 | gw.db.set_config('friend_zone', 'some') 611 | gw.debug('Friend-zone enabled; Will scrape both /r/friends AND user pages') 612 | gw.compare_friends() 613 | elif args.just_friends: 614 | gw.db.set_config('friend_zone', 'only') 615 | gw.debug('Friend-zone enabled; Will ONLY scrape /r/friends (not user pages)') 616 | gw.compare_friends() 617 | elif args.no_friend_zone: 618 | gw.db.set_config('friend_zone', 'none') 619 | gw.debug('Friend-zone disabled; Will ONLY scrape user pages (not /r/friends)') 620 | elif args.sync_friends: 621 | gw.compare_friends(add_friends=True) 622 | gw.debug('Friends list synced with database') 623 | 624 | elif args.add_top: 625 | gw.toggle_addtop() 626 | 627 | elif args.add: 628 | users = args.add.replace('u/', '').replace('/', '').split(',') 629 | for user in users: 630 | if not gw.db.user_already_added(user): 631 | gw.db.add_user(user, new=True) 632 | gw.debug('Add new user: /u/%s' % user) 633 | else: 634 | gw.debug('Warning: User already added: /u/%s' % user) 635 | elif args.remove: 636 | users = args.remove.replace('u/', '').replace('/', '').split(',') 637 | for user in users: 638 | if gw.db.user_already_added(user): 639 | gw.db.remove_user(user, new=True) 640 | gw.debug('Add new user: /u/%s' % user) 641 | else: 642 | gw.debug('Warning: User already added: /u/%s' % user) 643 | 644 | elif args.friend: 645 | users = args.friend.replace('u/', '').replace('/', '').split(',') 646 | gw.login() 647 | for user in users: 648 | gw.add_friend(user) 649 | elif args.unfriend: 650 | users = args.unfriend.replace('u/', '').replace('/', '').split(',') 651 | gw.login() 652 | for user in users: 653 | gw.remove_friend(user) 654 | 655 | elif args.exclude: 656 | subs = args.exclude.replace('r/', '').replace('/', '').split(',') 657 | for sub in subs: 658 | try: 659 | gw.add_excluded_subreddit(sub) 660 | gw.debug('Added excluded subreddit: /r/%s' % sub) 661 | except Exception, e: 662 | gw.debug('Unable to exclude subreddit /r/%s: %s' % (sub, str(e))) 663 | elif args.include: 664 | subs = args.include.replace('r/', '').replace('/', '').split(',') 665 | for sub in subs: 666 | try: 667 | gw.db.remove_excluded_subreddit(sub) 668 | gw.debug('Removed excluded subreddit: /r/%s' % sub) 669 | except Exception, e: 670 | gw.debug('Unable to remove excluded subreddit /r/%s: %s' % (sub, str(e))) 671 | 672 | elif args.reddit: 673 | gw.db.set_credentials('reddit', args.reddit[0], args.reddit[1]) 674 | gw.debug('Added/updated reddit login credentials for user "%s"' % args.reddit[0]) 675 | elif args.soundcloud: 676 | gw.db.set_credentials('soundcloud', args.soundcloud[0], args.soundcloud[1]) 677 | gw.debug('Added/updated soundcloud login credentials for user "%s"' % args.soundcloud[0]) 678 | 679 | elif args.backfill_thumbnails: 680 | for imageid,imagepath in gw.db.select('id,path', 'images', 'thumb like "%nothumb.png"'): 681 | fname = path.basename(imagepath) 682 | fpath = path.dirname(imagepath) 683 | 684 | thumbpath = path.join(fpath, 'thumbs') 685 | ImageUtils.create_subdirectories(thumbpath) 686 | savethumbas = path.join(thumbpath, fname) 687 | 688 | try: 689 | savethumbas = ImageUtils.create_thumbnail(imagepath, savethumbas) 690 | gw.db.update('images', 'thumb = ?', 'id = ?', [savethumbas, imageid]) 691 | gw.debug('created thumbnail %s' % savethumbas) 692 | except Exception, e: 693 | savethumbas = path.join(ImageUtils.get_root(), 'images', 'nothumb.png') 694 | gw.debug('Backfill-Thumbnails: Failed to create thumb for %s: %s, using nothumb.png' % (imagepath, str(e))) 695 | gw.db.commit() 696 | 697 | elif args.comments: 698 | users = args.comments.replace('u/', '').replace('/', '').split(',') 699 | for user in users: 700 | gw.print_comments(user) 701 | elif args.posts: 702 | users = args.posts.replace('u/', '').replace('/', '').split(',') 703 | for user in users: 704 | gw.print_posts(user) 705 | 706 | elif args.log: 707 | level = args.log 708 | if not level.lower() in ['global', 'user', 'none', 'off']: 709 | gw.debug('Failed to set log level: given level "%s" is not valid' % level.lower()) 710 | gw.debug('Use "global", "user" or "none"') 711 | else: 712 | gw.db.set_config('log_level', level.lower()) 713 | gw.debug('Set Log Level to: %s' % level.lower()) 714 | 715 | elif args.config == [] or args.config: 716 | if len(args.config) == 0: 717 | gw.debug('Dumping configuration values...') 718 | for (key, value) in sorted(gw.db.select('key,value', 'config')): 719 | gw.debug('%s = "%s"' % (key, value)) 720 | elif len(args.config) == 1: 721 | key = args.config[0] 722 | value = gw.db.get_config(key) 723 | if value == None: 724 | gw.debug('Configuration key not found for "%s"' % key) 725 | else: 726 | gw.debug('Configuration: %s = "%s"' % (key, value)) 727 | elif len(args.config) == 2: 728 | key = args.config[0] 729 | value = args.config[1] 730 | gw.db.set_config(key, value) 731 | gw.debug('Saved configuration: %s = "%s"' % (key, value)) 732 | else: 733 | return False 734 | return True 735 | 736 | if __name__ == '__main__': 737 | 738 | gw = Gonewild() 739 | gw.setup_config() 740 | try: 741 | if handle_arguments(gw): 742 | exit(0) 743 | except Exception, e: 744 | gw.debug('\n[!] Error: %s' % str(e.message)) 745 | from traceback import format_exc 746 | print format_exc() 747 | 748 | from sys import exit 749 | exit(1) 750 | 751 | gw.login() 752 | gw.infinite_loop() 753 | 754 | -------------------------------------------------------------------------------- /py/Httpy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | HTTP/Web class. 6 | 7 | Holds commonly-used HTTP/web request/post methods. 8 | 9 | Compatible with Python 2.5, 2.6, 2.7 10 | """ 11 | 12 | import time 13 | 14 | import urllib2, cookielib, urllib, httplib 15 | from sys import stderr 16 | 17 | DOWNLOAD_TIMEOUT = 10 18 | class Httpy: 19 | """ 20 | Class used for communicating with web servers. 21 | """ 22 | 23 | DEFAULT_USERAGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:34.0) Gecko/20100101 Firefox/34.0' 24 | def __init__(self, user_agent=None, debugging=False): 25 | """ 26 | Sets this class's user agent. 27 | """ 28 | self.debugging = debugging 29 | self.cj = cookielib.CookieJar() 30 | self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) 31 | self.Request = urllib2.Request 32 | self.urlopen = self.opener.open 33 | 34 | if user_agent != None: 35 | self.user_agent = user_agent 36 | else: 37 | self.user_agent = Httpy.DEFAULT_USERAGENT 38 | 39 | def raise_timeout(self, signum, frame): 40 | raise Exception("Timeout") 41 | 42 | def get_meta(self, url): 43 | """ Reads file info (content type, length, etc) without downloading 44 | Times out after 10 seconds (5 to unshorten, 5 to get meta) """ 45 | url = self.unshorten(url) 46 | try: 47 | headers = {'User-agent' : self.user_agent} 48 | req = urllib2.Request(url, headers=headers) 49 | site = self.urlopen(req) 50 | #site = self.urlopen(url) 51 | except Exception: 52 | return {'content-type': 'unknown', 'content-length': '0'} 53 | return site.info() 54 | 55 | def unshorten(self, url): 56 | """ Unshortens URL. Follows until no more redirects. Times out after 5 seconds """ 57 | try: 58 | headers = {'User-agent' : self.user_agent} 59 | req = urllib2.Request(url, headers=headers) 60 | site = self.urlopen(req) 61 | except urllib2.HTTPError: 62 | return url 63 | except Exception: 64 | return url 65 | return site.url 66 | 67 | def check(self, url): 68 | """ Check if a URL is valid """ 69 | try: 70 | self.urlopen(url) 71 | except: 72 | return False 73 | return True 74 | 75 | def get(self, url, headers={}): 76 | """ 77 | Attempts GET request with web server. 78 | 79 | Returns html source of a webpage (string). 80 | Returns '' if unable to retrieve webpage for any reason. 81 | 82 | Will attempt to repeatedly post if '504' response error is received 83 | or 'getaddrinfo' fails. 84 | """ 85 | if not 'User-agent' in headers: 86 | headers['User-agent'] = self.user_agent 87 | 88 | try: 89 | req = urllib2.Request(url, headers=headers) 90 | handle = self.urlopen(req) 91 | 92 | except Exception, e: 93 | if self.debugging: stderr.write('Httpy: Exception while creating request: %s\n' % str(e)) 94 | raise e 95 | 96 | try: 97 | result = handle.read() 98 | except Exception, e: 99 | if self.debugging: stderr.write('Httpy: Exception while reading response: %s\n' % str(e)) 100 | raise e 101 | 102 | return result 103 | 104 | def getter(self, url, headers={}, retry=1): 105 | """ 106 | Attempts GET request with extended options. 107 | 108 | Returns html source of a webpage (string). 109 | Returns '' if unable to retrieve webpage for any reason. 110 | 111 | Will retry attempts that fail. 112 | 113 | Does *NOT* utilize cookie jar! 114 | """ 115 | if not 'User-agent' in headers: 116 | headers['User-agent'] = self.user_agent 117 | 118 | (https, host, path) = self.get_https_host_path(url) 119 | if self.debugging: stderr.write('Httpy.py: GET http%s://%s%s\n' % ('s' if https else '', host, path)) 120 | try: 121 | if https: 122 | req = httplib.HTTPSConnection(host) 123 | else: 124 | req = httplib.HTTPConnection(host) 125 | req.putrequest('GET', path) 126 | if self.debugging: stderr.write('Httpy.py: headers:\n') 127 | for hkey in headers.keys(): 128 | if self.debugging: stderr.write(' %s:\t%s\n' % (hkey, headers[hkey])) 129 | req.putheader(hkey, headers[hkey]) 130 | req.endheaders() 131 | resp = req.getresponse() 132 | if self.debugging: stderr.write('Httpy.py: response headers:') 133 | for h,v in resp.getheaders(): 134 | if self.debugging: stderr.write(' %s: "%s"\n' % (h, v)) 135 | if resp.status == 200: 136 | return resp.read() 137 | elif resp.status in [301, 302] and resp.getheader('Location') != None: 138 | if self.debugging: stderr.write('Httpy.py: Got %d to %s' % (resp.status, resp.getheader('Location'))) 139 | return self.getter(resp.getheader('Location'), headers=headers, retry=retry-1) 140 | else: 141 | result = '' 142 | try: result = resp.read() 143 | except: pass 144 | if self.debugging: stderr.write('Httpy.py: HTTP status %s: %s\n' % (resp.status, resp.reason)) 145 | return result 146 | except Exception, e: 147 | if self.debugging: stderr.write('Httpy.py: Exception: %s: %s\n' % (url, str(e))) 148 | if retry > 0: 149 | return self.getter(url, headers=headers, retry=retry-1) 150 | return '' 151 | 152 | def get_https_host_path(self, url): 153 | https = url.startswith('https') 154 | path = '' 155 | host = url[url.find('//')+2:] 156 | if '/' in host: 157 | host = host[:host.find('/')] 158 | path = url[url.find(host)+len(host):] 159 | return (https, host, path) 160 | 161 | def fix_string(self, s): 162 | r = '' 163 | for c in s: 164 | c2 = '' 165 | try: 166 | c2 = str(c) 167 | except UnicodeEncodeError: 168 | c2 = '' 169 | r += c2 170 | return r 171 | 172 | def fix_dict(self, dict): 173 | d = {} 174 | 175 | for key in dict: 176 | value = dict[key] 177 | d[key] = self.fix_string(value) 178 | return d 179 | 180 | def oldpost(self, url, postdict=None, headers={}): 181 | """ 182 | Submits a POST request to URL. Posts 'postdict' if 183 | not None. URL-encodes postdata (if dict) 184 | and strips Unicode chars. 185 | """ 186 | result = '' 187 | if not 'User-agent' in headers: 188 | headers['User-agent'] = self.user_agent 189 | if postdict == None: 190 | encoded_data = '' 191 | elif type(postdict) == dict: 192 | encoded_data = urllib.urlencode(postdict) 193 | elif type(postdict) == str: 194 | encoded_data = postdict 195 | try: 196 | req = self.Request(url, encoded_data, headers) 197 | handle = self.urlopen(req) 198 | result = handle.read() 199 | except Exception, e: 200 | if self.debugging: stderr.write('Httpy.py: Exception: %s: %s\n' % (url, str(e))) 201 | return result 202 | 203 | def post(self, url, postdict=None, headers={}): 204 | """ 205 | Attempts POST request with web server. 206 | 207 | Returns response of a POST request to a web server. 208 | 'postdict' must be a dictionary of keys/values to post to the server. 209 | Returns '' if unable to post/retrieve response. 210 | 211 | Will attempt to repeatedly post if '504' response error is received 212 | or 'getaddrinfo' fails. 213 | """ 214 | if not 'User-agent' in headers: 215 | headers['User-agent'] = self.user_agent 216 | data = '' 217 | if postdict != None and type(postdict) == dict: 218 | fixed_dict = self.fix_dict(postdict) 219 | data = urllib.urlencode(fixed_dict) 220 | elif postdict != None and type(postdict) == str: 221 | data = postdict 222 | headers['Content-Length'] = len(data) 223 | 224 | host = url[url.find('//')+2:] 225 | host = host[:host.find('/')] 226 | if self.debugging: stderr.write('Httpy.py: host: "%s"\n' % host) 227 | path = url[url.find(host)+len(host):] 228 | if self.debugging: stderr.write('Httpy.py: path: "%s"\n' % path) 229 | if self.debugging: stderr.write('Httpy.py: headers: %s\n' % str(headers)) 230 | if self.debugging: stderr.write('Httpy.py: postdata: "%s"\n' % data) 231 | try: 232 | if url.startswith('https'): 233 | req = httplib.HTTPSConnection(host) 234 | else: 235 | req = httplib.HTTPConnection(host) 236 | req.putrequest('POST', path) 237 | for hkey in headers.keys(): 238 | req.putheader(hkey, headers[hkey]) 239 | req.endheaders() 240 | req.send(data) 241 | resp = req.getresponse() 242 | if resp.status == 200: 243 | return resp.read() 244 | else: 245 | if self.debugging: stderr.write('Httpy.py: HTTP status %s: %s: %s\n' % (resp.status, resp.reason, resp.read())) 246 | if self.debugging: stderr.write('Httpy.py: Response headers:\n') 247 | for name, value in resp.getheaders(): 248 | if self.debugging: stderr.write('Httpy.py: \t"%s"="%s"\n' % (name, value)) 249 | return '' 250 | except Exception, e: 251 | if self.debugging: stderr.write('Httpy.py: Exception: %s: %s\n' % (url, str(e))) 252 | return '' 253 | 254 | def download(self, url, save_as, headers={}, timeout=DOWNLOAD_TIMEOUT, raise_exception=True, retries=3): 255 | """ Downloads file from URL to save_as path. """ 256 | retry_count = 0 257 | if not 'User-agent' in headers: 258 | headers['User-agent'] = self.user_agent 259 | outfile = open(save_as, 'wb') 260 | while True: 261 | try: 262 | retry_count += 1 263 | req = urllib2.Request(url, headers=headers) 264 | handle = self.urlopen(req, timeout=timeout) 265 | while True: 266 | buf = handle.read(65536) 267 | if len(buf) == 0: break 268 | outfile.write(buf) 269 | except Exception, e: 270 | if self.debugging: stderr.write('Httpy.py: download(%s): %s\n' % (url, str(e))) 271 | if retry_count <= retries: 272 | if self.debugging: stderr.write('Httpy.py: download(%s): Retrying (%d remain)\n' % (url, retries - retry_count)) 273 | continue 274 | if raise_exception: raise e 275 | break 276 | outfile.close() 277 | 278 | def clear_cookies(self): 279 | """ 280 | Clears cookies in cookie jar. 281 | """ 282 | self.cj.clear() 283 | 284 | 285 | def set_user_agent(user_agent): 286 | """ 287 | Changes the user-agent used when connecting. 288 | """ 289 | self.user_agent = user_agent 290 | 291 | 292 | def between(self, source, start, finish): 293 | """ 294 | Helper method. Useful when parsing responses from web servers. 295 | 296 | Looks through a given source string for all items between two other strings, 297 | returns the list of items (or empty list if none are found). 298 | 299 | Example: 300 | test = 'hello >30< test >20< asdf >>10<< sadf>' 301 | print between(test, '>', '<') 302 | 303 | would print the list: 304 | ['30', '20', '>10'] 305 | """ 306 | result = [] 307 | i = source.find(start) 308 | j = source.find(finish, i + len(start)) 309 | 310 | while i >= 0 and j >= 0: 311 | i = i + len(start) 312 | result.append(source[i:j]) 313 | i = source.find(start, j + len(finish)) 314 | j = source.find(finish, i + len(start)) 315 | 316 | return result 317 | 318 | -------------------------------------------------------------------------------- /py/ImageUtils.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from Httpy import Httpy 4 | from os import path, getcwd, sep, mkdir 5 | from PIL import Image # Python Image Library 6 | from commands import getstatusoutput 7 | from sys import stderr 8 | from time import strftime, gmtime 9 | 10 | class ImageUtils(object): 11 | logger = stderr 12 | 13 | # Static class variables 14 | MAXIMUM_THUMBNAIL_SIZE = 5 * 1024 * 1024 # In bytes 15 | MAXIMUM_THUMBNAIL_DIM = 5000 # In pixels 16 | httpy = Httpy() 17 | 18 | @staticmethod 19 | def debug(text): 20 | tstamp = strftime('[%Y-%m-%dT%H:%M:%SZ]', gmtime()) 21 | text = '%s ImageUtils: %s' % (tstamp, text) 22 | ImageUtils.logger.write('%s\n' % text) 23 | if ImageUtils.logger != stderr: 24 | stderr.write('%s\n' % text) 25 | 26 | ''' 27 | Given a URL, return a tuple containig: 28 | [0] media type ('video', 'image') 29 | [1] filesystem-safe album name, or None if not an album 30 | [2] List of all direct links to relevant media. E.g.: 31 | imgur.com/asdf1 -> [i.imgur.com/asdf1.jpg] 32 | i.imgur.com/smallh.jpg -> [i.imgur.com/large.jpg] 33 | imgur.com/a/album -> [i.imgur.com/image1.jpg, i.imgur.com/image2.jpg] 34 | xhamster.com/video -> xhamster.com/cdn/video.mp4 35 | etc 36 | Throws exception if domain is not supported. 37 | ''' 38 | @staticmethod 39 | def get_urls(url): 40 | if 'imgur.com' in url.lower(): 41 | return ImageUtils.get_urls_imgur(url) 42 | elif '.' in url and url.lower()[url.rfind('.')+1:] in ['jpg', 'jpeg', 'png', 'gif']: 43 | # Direct link to image 44 | return ('image', None, [url]) 45 | elif '.' in url and url.lower()[url.rfind('.')+1:] in ['mp4', 'flv', 'wmv']: 46 | # Direct link to video 47 | return ('video', None, [url]) 48 | elif 'xhamster.com' in url: 49 | # xhamster 50 | return ImageUtils.get_urls_xhamster(url) 51 | elif 'videobam.com' in url: 52 | # videobam 53 | return ImageUtils.get_urls_videobam(url) 54 | elif 'sexykarma.com' in url: 55 | # sexykarma 56 | return ImageUtils.get_urls_sexykarma(url) 57 | elif 'tumblr.com' in url: 58 | # tumblr 59 | return ImageUtils.get_urls_tumblr(url) 60 | elif 'vine.co/' in url: 61 | # vine 62 | return ImageUtils.get_urls_vine(url) 63 | elif 'vidble.com/' in url: 64 | # vidble 65 | return ImageUtils.get_urls_vidble(url) 66 | elif 'soundcloud.com/' in url or 'snd.sc/' in url: 67 | # soundcloud 68 | return ImageUtils.get_urls_soundcloud(url) 69 | elif 'chirb.it/' in url or 'chirbit.com' in url: 70 | # chirbit 71 | return ImageUtils.get_urls_chirbit(url) 72 | elif 'vocaroo.com/' in url: 73 | # vocaroo 74 | return ImageUtils.get_urls_vocaroo(url) 75 | elif 'imgdoge.com/' in url: 76 | # imgdoge 77 | return ImageUtils.get_urls_imgdoge(url) 78 | elif 'gifboom.com/' in url: 79 | # gifboom 80 | return ImageUtils.get_urls_gifboom(url) 81 | elif 'mediacru.sh/' in url: 82 | # mediacrush 83 | return ImageUtils.get_urls_mediacrush(url) 84 | elif 'vidd.me/' in url or 'vid.me/' in url: 85 | # viddme 86 | return ImageUtils.get_urls_viddme(url) 87 | elif 'gfycat.com/' in url: 88 | # gfycat 89 | return ImageUtils.get_urls_gfycat(url) 90 | else: 91 | result = [] 92 | for extension in ['jpg', 'png', 'gif']: 93 | if url.lower().endswith('.' + extension): 94 | return ('image', None, [url]) 95 | for extension in ['wmv', 'mp4']: 96 | if url.lower().endswith('.' + extension): 97 | return ('video', None, [url]) 98 | for extension in ['wma', 'mp3', 'm4a']: 99 | if url.lower().endswith('.' + extension): 100 | return ('audio', None, [url]) 101 | 102 | if 'soundgasm.net/' in url: 103 | # soundgasm 104 | return ImageUtils.get_urls_soundgasm(url) 105 | raise Exception('domain not supported; %s' % url) 106 | 107 | ''' Removes excess fields from URL ''' 108 | @staticmethod 109 | def strip_url(url): 110 | if '?' in url: url = url[:url.find('?')] 111 | if '#' in url: url = url[:url.find('#')] 112 | if '&' in url: url = url[:url.find('&')] 113 | return url 114 | 115 | ################ 116 | # XHAMSTER 117 | @staticmethod 118 | def get_urls_xhamster(url): 119 | ImageUtils.debug('xhamster: getting %s' % url) 120 | r = ImageUtils.httpy.get(url) 121 | if not "
" in r: 122 | raise Exception('no mp4 found at %s' % url) 123 | chunk = ImageUtils.httpy.between(r, "
", "
")[0] 124 | return ('video', None, [ImageUtils.httpy.between(chunk, 'href="', '"')[0]]) 125 | 126 | ################ 127 | # VIDEOBAM 128 | @staticmethod 129 | def get_urls_videobam(url): 130 | ImageUtils.debug('videobam: getting %s' % url) 131 | r = ImageUtils.httpy.get(url) 132 | if not ',"url":"' in r: 133 | raise Exception('no url found at %s' % url) 134 | for link in ImageUtils.httpy.between(r, '"url":"', '"'): 135 | if not '.mp4' in link: continue 136 | return ('video', None, [link.replace('\\', '')]) 137 | raise Exception('no mp4 found at %s' % url) 138 | 139 | ################ 140 | # SEXYKARMA 141 | @staticmethod 142 | def get_urls_sexykarma(url): 143 | ImageUtils.debug('sexykarma: getting %s' % url) 144 | r = ImageUtils.httpy.get(url) 145 | if not "url: escape('" in r: 146 | raise Exception('no url found at %s' % url) 147 | for link in ImageUtils.httpy.between(r, "url: escape('", "'"): 148 | return ('video', None, [link]) 149 | raise Exception('no video found at %s' % url) 150 | 151 | ################ 152 | # TUMBLR 153 | @staticmethod 154 | def get_urls_tumblr(url): 155 | ImageUtils.debug('tumblr: getting %s' % url) 156 | r = ImageUtils.httpy.get(url) 157 | if not 'source src=\\x22' in r: 158 | raise Exception('no src= found at %s' % url) 159 | for link in ImageUtils.httpy.between(r, 'source src=\\x22', '\\x22'): 160 | link = ImageUtils.httpy.unshorten(link) 161 | return ('video', None, [link]) 162 | raise Exception('no video found at %s' % url) 163 | 164 | ################ 165 | # VINE 166 | @staticmethod 167 | def get_urls_vine(url): 168 | ImageUtils.debug('vine: getting %s' % url) 169 | r = ImageUtils.httpy.get(url) 170 | if not 'property="twitter:image" content="' in r: 171 | raise Exception('no twitter:image found at %s' % url) 172 | for link in ImageUtils.httpy.between(r, 'property="twitter:image" content="', '"'): 173 | return ('video', None, [link]) 174 | raise Exception('no video found at %s' % url) 175 | 176 | ################ 177 | # VIDBLE 178 | @staticmethod 179 | def get_urls_vidble(url): 180 | ImageUtils.debug('vidble: getting %s' % url) 181 | r = ImageUtils.httpy.get(url) 182 | urls = [] 183 | for index, link in enumerate(ImageUtils.httpy.between(r, "'): 319 | link = link.replace('&', '&') 320 | urls.append(link) 321 | return ('video', None, urls) 322 | 323 | ################ 324 | # GFYCAT.COM 325 | @staticmethod 326 | def get_urls_gfycat(url): 327 | ImageUtils.debug('gfycat.com: getting %s' % url) 328 | headers = { 329 | 'Referer' : url 330 | } 331 | r = ImageUtils.httpy.get(url, headers=headers) 332 | urls = [] 333 | for link in ImageUtils.httpy.between(r, ' %s' % (url, url)) 418 | return url 419 | else: 420 | ImageUtils.debug('imgur_highest_res: %s -> %s' % (url, noh)) 421 | return noh 422 | elif not '.' in fname: 423 | # Need to get full-size and extension 424 | r = ImageUtils.httpy.get(url) 425 | if '')[0] 428 | image = ImageUtils.httpy.between(chunk, 'content="', '"')[0] 429 | elif '')[0] 432 | image = ImageUtils.httpy.between(chunk, 'href="', '"')[0] 433 | else: 434 | raise Exception('image not found') 435 | if image.startswith('//'): image = 'http:%s' % image 436 | ImageUtils.debug('imgur_highest_res: %s -> %s' % (url, image)) 437 | return image 438 | return url 439 | 440 | @staticmethod 441 | def get_imgur_account_album(url): 442 | r = ImageUtils.httpy.get(url) 443 | albumid = ImageUtils.httpy.between(r, "setAlbumView('", "'")[0] 444 | return ImageUtils.get_imgur_album('http://imgur.com/a/%s' % albumid) 445 | 446 | 447 | ''' 448 | Return just filename (no path) for a URL 449 | http://i.imgur.com/asdf1.jpg -> "asdf1.jpg" 450 | amazonaws.com/crazystuff/theimage.jpg?morecrazy=stuff&asdf=123 -> "theimage.jpg" 451 | http://2.videobam.com/storage/encoded.mp4/2d1/5113?ss=177 -> "encoded.mp4" 452 | Also appends 'mp3' file extension for audio files that end with 'php' (vocaroo) 453 | ''' 454 | @staticmethod 455 | def get_filename_from_url(url, media_type='image'): 456 | fname = ImageUtils.strip_url(url) 457 | fields = fname.split('/') 458 | if 'soundcloud.com' in url: 459 | filename = '%s.mp3' % fields[4] 460 | else: 461 | while not '.' in fields[-1]: fields.pop(-1) 462 | filename = fields[-1] 463 | if media_type == 'audio': 464 | if filename.endswith('.php'): 465 | filename = filename[:filename.rfind('.')+1] + 'mp3' 466 | return filename 467 | 468 | 469 | ######################## 470 | # ACTUAL IMAGE FUNCTIONS 471 | 472 | ''' 473 | Create thumbnail from existing image file. 474 | Raises exception if unable to save thumbnail 475 | ''' 476 | @staticmethod 477 | def create_thumbnail(image, saveas): 478 | if image.lower().endswith('.mp4') or \ 479 | image.lower().endswith('.flv') or \ 480 | image.lower().endswith('.wmv'): 481 | return ImageUtils.create_video_thumbnail(image, saveas) 482 | if path.getsize(image) > ImageUtils.MAXIMUM_THUMBNAIL_SIZE: 483 | raise Exception('Image too large: %db > %db' % 484 | (path.getsize(image), ImageUtils.MAXIMUM_THUMBNAIL_SIZE)) 485 | try: 486 | im = Image.open(image) 487 | except Exception, e: 488 | raise Exception('failed to create thumbnail: %s' % str(e)) 489 | (width, height) = im.size 490 | if width > ImageUtils.MAXIMUM_THUMBNAIL_DIM or \ 491 | height > ImageUtils.MAXIMUM_THUMBNAIL_DIM: 492 | raise Exception( 493 | 'Image too large: %dx%d > %dpx' % 494 | (width, height, ImageUtils.MAXIMUM_THUMBNAIL_DIM)) 495 | 496 | if im.mode != 'RGB': im = im.convert('RGB') 497 | im.thumbnail( (200,200), Image.ANTIALIAS) 498 | im.save(saveas, 'JPEG') 499 | return saveas 500 | 501 | ''' 502 | Create thumbnail for video file using ffmpeg. 503 | Raises exception if unable to save video thumbnail 504 | ''' 505 | @staticmethod 506 | def create_video_thumbnail(video, saveas): 507 | if saveas.lower().endswith('.mp4') or \ 508 | saveas.lower().endswith('.flv') or \ 509 | saveas.lower().endswith('.wmv'): 510 | saveas = '%s.png' % saveas[:saveas.rfind('.')] 511 | overlay = path.join(ImageUtils.get_root(), 'images', 'play_overlay.png') 512 | ffmpeg = '/usr/bin/ffmpeg' 513 | if not path.exists(ffmpeg): 514 | ffmpeg = '/opt/local/bin/ffmpeg' 515 | if not path.exists(ffmpeg): 516 | raise Exception('ffmpeg not found; unable to create video thumbnail') 517 | cmd = ffmpeg 518 | cmd += ' -i "' 519 | cmd += video 520 | cmd += '" -vf \'movie=' 521 | cmd += overlay 522 | cmd += ' [watermark]; ' 523 | cmd += '[in]scale=200:200 [scale]; ' 524 | cmd += '[scale][watermark] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2 [out]\' ' 525 | cmd += saveas 526 | output = '' 527 | try: 528 | (status, output) = getstatusoutput(cmd) 529 | except: 530 | raise Exception('failed to generate thumbnail using ffmpeg: %s' % output) 531 | return saveas 532 | 533 | ''' 534 | Get width/height of image or video 535 | ''' 536 | @staticmethod 537 | def get_dimensions(image): 538 | if image.lower().endswith('.mp4') or \ 539 | image.lower().endswith('.flv'): 540 | ffmpeg = '/usr/bin/ffmpeg' 541 | if not path.exists(ffmpeg): 542 | ffmpeg = '/opt/local/bin/ffmpeg' 543 | if not path.exists(ffmpeg): 544 | raise Exception('ffmpeg not found; unable to get video dimensions') 545 | (status, output) = getstatusoutput('%s -i "%s"' % (ffmpeg, image)) 546 | for line in output.split('\n'): 547 | if 'Stream' in line and 'Video:' in line: 548 | line = line[line.find('Video:')+6:] 549 | fields = line.split(', ') 550 | dims = fields[2] 551 | if not 'x' in dims: raise Exception('invalid video dimensions') 552 | (width, height) = dims.split('x') 553 | if ' ' in height: height = height[:height.find(' ')] 554 | try: 555 | width = int(width) 556 | height = int(height) 557 | except: 558 | raise Exception('invalid video dimensions: %sx%s' % (width, height)) 559 | return (width, height) 560 | raise Exception('unable to get video dimensions') 561 | else: 562 | im = Image.open(image) 563 | return im.size 564 | 565 | 566 | ############### 567 | # MISCELLANEOUS 568 | 569 | @staticmethod 570 | def create_subdirectories(directory): 571 | current = '' 572 | for subdir in directory.split(sep): 573 | if subdir == '': continue 574 | current = path.join(current, subdir) 575 | if not path.exists(current): 576 | mkdir(current) 577 | 578 | ''' Get root working dir ''' 579 | @staticmethod 580 | def get_root(): 581 | cwd = getcwd() 582 | if cwd.endswith('py'): 583 | return '..' 584 | return '.' 585 | 586 | if __name__ == '__main__': 587 | # Test urls 588 | #url = 'http://www.sexykarma.com/gonewild/video/cum-compilation-YIdo9ntfsWo.html' 589 | #url = 'http://xhamster.com/movies/1435778/squirting_hard.html' 590 | #url = 'http://videobam.com/jcLzr' 591 | #url = 'http://alwaysgroundedx.tumblr.com/private/22807448211/tumblr_m3tyhmw3mQ1ruoc8i' 592 | #url = 'https://vine.co/v/h6Htgnj7Z5q' 593 | #url = 'http://www.vidble.com/album/CwlMIYqm' 594 | #url = 'http://www.vidble.com/ieIvnqJY4v' 595 | #url = 'http://vidble.com/album/pXpkBBpD' 596 | #url = 'http://vidble.com/album/schhngs4' 597 | #url = 'http://snd.sc/1d2RCEv' 598 | #url = 'http://soundgasm.net/u/sexuallyspecific/F4M-A-week-of-retribution-TD-Challenge-Part-7-The-Finale' 599 | #url = 'http://chirb.it/5vyK6D' 600 | #url = 'http://vocaroo.com/i/s0umizubFmH6' 601 | #url = 'http://imgdoge.com/img-52ed7dd198460.html' 602 | #url = 'http://gifboom.com/x/5c009736' 603 | #url = 'https://mediacru.sh/5dc4cee7fb94' # album 604 | #url = 'https://mediacru.sh/d7CsmyozGgB7' 605 | #url = 'http://imgur.com/WZweelk,oB0mtcb,spBaC6r' 606 | 607 | # Direct links 608 | #url = 'http://indiestatik.com/wp-content/uploads/2014/03/IMG_0362.jpg' 609 | #url = 'https://d1wst0behutosd.cloudfront.net/videos/2296.mp4' 610 | #url = 'http://soundgasm.net/sounds/8922312168b99ba4c4c9c294e3ced77a49336c6c.m4a' 611 | 612 | #url = 'http://soundcloud.com/bondgirlaudio/my-f-irst-gwa-post-thank-you' 613 | #url = 'http://dayah.imgur.com/kapow' 614 | #url = 'http://gfycat.com/AmusingCalculatingGrayfox' 615 | #url = 'http://gfycat.com/HandmadePertinentArmedcrab' 616 | #url = 'https://vidd.me/xpW' 617 | #url = 'https://vid.me/xpW' 618 | url = 'http://imgur.com/PNzNzdf' # Ends with ?1 619 | #url = 'http://imgur.com/OZiYY9D' # Does not end with ?1 620 | #url = 'http://i.imgur.com/B5TOKc6.gifv' 621 | #url = 'http://imgur.com/B5TOKc6' 622 | test_urls = [url] 623 | 624 | ImageUtils.httpy.debugging = True 625 | for index,test_url in enumerate(test_urls): 626 | (media_type, b, urls) = ImageUtils.get_urls(test_url) 627 | if len(urls) == 0: 628 | print index, 'no media urls found for %s' % test_url 629 | from sys import exit 630 | exit(1) 631 | print index, media_type, b, urls 632 | for i,u in enumerate(urls): 633 | print index,i,u 634 | fname = ImageUtils.get_filename_from_url(u, media_type=media_type) 635 | ImageUtils.httpy.download(u, 'test-%d-%d-%s' % (index, i, fname)) 636 | #ImageUtils.create_thumbnail('test.jpg', 'test_thumb.jpg') 637 | #ImageUtils.create_thumbnail('../test.mp4', '../test_thumb.jpg') 638 | # Testing imgur highest-res 639 | #print ImageUtils.get_imgur_highest_res('http://i.imgur.com/30GO67h.jpg') 640 | #print ImageUtils.get_imgur_highest_res('http://i.imgur.com/30GO67hh.jpg') 641 | pass 642 | -------------------------------------------------------------------------------- /py/Queries.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from DB import DB 4 | 5 | # Request user 6 | # Get: Posts and comments 7 | # Each post/comment: list of images. 8 | class Queries(object): 9 | SEARCH_FIELDS = ['user', 'reddit', 'title', 'comment', 'from', 'to', 'album', 'url'] 10 | 11 | ''' 12 | Parses search fields from text. 13 | Similar to reddit's specialized search. 14 | returns tuple: 15 | [0] list of unspecified text fields 16 | [1] dict of lists; containing specified text fields 17 | Quoted fields will be combined. 18 | Non-specified text will be returned in the first tuple 19 | Specified fields will be added to a specified bucket. Examples: 20 | 'one two three' => ( ['one', 'two', 'three'], {} ) 21 | '"one two" three' => ( ['one two', 'three'], {} ) 22 | 'blah user:asdf => ( ['blah'], { 'user': ['asdf'] } ) 23 | 'reddit:nsfw user:asdf => ( [], { 'reddit': ['nsfw'], 'user': ['asdf'] } ) 24 | 'title:this title:first => ( [], { 'title': ['this', 'first'] } ) 25 | ''' 26 | @staticmethod 27 | def get_search_fields(text): 28 | fields = text.split(' ') 29 | i = 0 30 | # Combine quoted fields 31 | while i < len(fields): 32 | if fields[i].startswith('"'): 33 | fields[i] = fields[i][1:] 34 | while i + 1 < len(fields): 35 | fields[i] += ' ' + fields[i+1] 36 | fields.pop(i+1) 37 | if fields[i].endswith('"'): 38 | fields[i] = fields[i][:-1] 39 | break 40 | i += 1 41 | # Split into 'texts' and 'filters' 42 | filters = {} 43 | texts = [] 44 | for field in fields: 45 | if ':' in field: 46 | key = field.split(':')[0] 47 | value = ':'.join(field.split(':')[1:]) 48 | if key in Queries.SEARCH_FIELDS: 49 | lst = filters.get(key, []) 50 | lst.append('%%%s%%' % value) 51 | filters[key] = lst 52 | else: 53 | texts.append('%%%s%%' % field) 54 | return (texts, filters) 55 | 56 | @staticmethod 57 | def search_users(texts, filters, start, count): 58 | results_users = [] 59 | if len(texts) > 0 or len(filters['user']) > 0: 60 | query = ''' 61 | select 62 | users.username, users.created, users.updated, 63 | users.deleted, users.views, users.rating, users.ratings 64 | from (select * from users 65 | where 66 | ''' 67 | conditions = [] 68 | search_values = [] 69 | if 'user' in filters and len(filters['user']) > 0: 70 | conditions += ['UPPER(username) like UPPER(?)'] * len(filters['user']) 71 | search_values += filters['user'] 72 | elif len(texts) > 0: 73 | conditions += ['UPPER(username) like UPPER(?)'] * len(texts) 74 | search_values += texts 75 | query += ' OR '.join(conditions) 76 | query += ''' 77 | limit %d 78 | offset %d 79 | ) users 80 | ''' % (count, start) 81 | db = DB() 82 | cur = db.conn.cursor() 83 | execur = cur.execute(query, search_values) 84 | results = execur.fetchall() 85 | for (username, created, updated, 86 | deleted, views, rating, ratings) in results: 87 | results_users.append( { 88 | 'user' : username, 89 | 'created' : created, 90 | 'updated' : updated, 91 | 'deleted' : deleted, 92 | 'views' : views, 93 | 'rating' : rating, 94 | 'ratings' : ratings, 95 | }) 96 | return results_users 97 | 98 | @staticmethod 99 | def search_posts(texts, filters, start, count): 100 | results_posts = [] 101 | query = ''' 102 | select 103 | posts.id, posts.title, posts.url, posts.subreddit, 104 | posts.created, posts.permalink, users.username 105 | from posts,users 106 | where 107 | users.id = posts.userid AND 108 | ''' 109 | conditions_or = [] 110 | conditions_and = [] 111 | search_values = [] 112 | 113 | if 'title' in filters and len(filters['title']) > 0: 114 | conditions_and.extend(['title like ?'] * len(filters['title'])) 115 | search_values.extend(filters['title']) 116 | else: 117 | conditions_or.extend(['title like ?'] * len(texts)) 118 | search_values.extend(texts) 119 | 120 | if 'user' in filters and len(filters['user']) > 0: 121 | conditions_and.extend(['UPPER(username) like UPPER(?)'] * len(filters['user'])) 122 | search_values.extend(filters['user']) 123 | else: 124 | conditions_or.extend(['UPPER(username) like UPPER(?)'] * len(texts)) 125 | search_values.extend(texts) 126 | if 'reddit' in filters and len(filters['reddit']) > 0: 127 | conditions_and.extend(['subreddit like ?'] * len(filters['reddit'])) 128 | search_values.extend(filters['reddit']) 129 | else: 130 | conditions_or.extend(['subreddit like ?'] * len(texts)) 131 | search_values.extend(texts) 132 | 133 | if len(conditions_or) > 0: 134 | query += '(%s)' % ' OR '.join(conditions_or) 135 | if len(conditions_or) > 0 and len(conditions_and) > 0: 136 | query += ' AND ' 137 | if len(conditions_and) > 0: 138 | query += '(%s)' % ' AND '.join(conditions_and) 139 | 140 | query += ''' 141 | limit %d 142 | offset %d 143 | ''' % (count, start) 144 | 145 | db = DB() 146 | cur = db.conn.cursor() 147 | execur = cur.execute(query, search_values) 148 | results = execur.fetchall() 149 | for (postid, title, url, reddit, created, permalink, user) in results: 150 | results_posts.append( { 151 | 'id' : postid, 152 | 'title' : title, 153 | 'url' : url, 154 | 'subreddit' : reddit, 155 | 'created' : created, 156 | 'permalink' : permalink, 157 | 'user' : user, 158 | }) 159 | return results_posts 160 | 161 | @staticmethod 162 | def search(text, start=0, count=20): 163 | (texts, filters) = Queries.get_search_fields(text) 164 | 165 | # USERS 166 | results_users = Queries.search_users(texts, filters, start, count) 167 | 168 | # POSTS 169 | results_posts = Queries.search_posts(texts, filters, start, count) 170 | 171 | # COMMENTS 172 | # TODO 173 | pass 174 | 175 | return { 176 | 'users' : results_users, 177 | 'posts' : results_posts 178 | } 179 | 180 | ''' 181 | Retrieves from full list of users 182 | Returns user info along with # of images, albums, posts, and comments. 183 | ''' 184 | @staticmethod 185 | def get_users(sortby='username', orderby='asc', start=0, count=20): 186 | if sortby not in ['username', 'created', 'updated']: 187 | sortby = 'username' 188 | if orderby not in ['asc', 'desc']: 189 | orderby = 'asc' 190 | query = ''' 191 | select 192 | id, users.username, users.created, users.updated 193 | from users 194 | order by %s %s 195 | limit %d 196 | offset %d 197 | ''' % (sortby, orderby, count, start) 198 | db = DB() 199 | cur = db.conn.cursor() 200 | execur = cur.execute(query) 201 | results = execur.fetchall() 202 | users = [] 203 | for (userid, username, created, updated) in results: 204 | images = [] 205 | query = ''' 206 | select 207 | path, width, height, size, thumb, type 208 | from images 209 | where 210 | images.userid = ? 211 | limit 4 212 | ''' 213 | execur = cur.execute(query, [userid]) 214 | image_results = execur.fetchall() 215 | for (path, width, height, size, thumb, imagetype) in image_results: 216 | images.append({ 217 | 'path' : path, 218 | 'width' : width, 219 | 'height' : height, 220 | 'size' : size, 221 | 'thumb' : thumb, 222 | 'type' : imagetype 223 | }) 224 | 225 | post_count = db.count('posts', 'userid = ?', [userid]) 226 | image_count = db.count('images', 'userid = ? and (type = \'image\' or type = \'album\')', [userid]) 227 | video_count = db.count('images', 'userid = ? and type = \'video\'', [userid]) 228 | 229 | users.append( { 230 | 'user' : username, 231 | 'created' : created, 232 | 'updated' : updated, 233 | 'images' : images, 234 | 'post_n' : post_count, 235 | 'image_n' : image_count, 236 | 'video_n' : video_count 237 | }) 238 | cur.close() 239 | return { 240 | 'users' : users 241 | } 242 | 243 | @staticmethod 244 | def get_user_posts(user, sortby='created', orderby='asc', start=0, count=20): 245 | # XXX Select from images, group by post,album 246 | # ... but images -> post is many->one (even when not an album) 247 | 248 | if sortby not in ['id', 'created', 'subreddit', 'ups']: 249 | sortby = 'created' 250 | if orderby not in ['asc', 'desc']: 251 | orderby = 'desc' 252 | 253 | query = ''' 254 | select 255 | id, title, url, selftext, subreddit, created, permalink, ups, downs 256 | from posts 257 | where 258 | posts.userid in 259 | (select id from users where UPPER(username) = UPPER(?)) 260 | order by %s %s 261 | limit %d 262 | offset %d 263 | ''' % (sortby, orderby, count, start) 264 | db = DB() 265 | cur = db.conn.cursor() 266 | execur = cur.execute(query, [user]) 267 | posts = [] 268 | for (postid, title, url, selftext, subreddit, created, permalink, ups, downs) in execur.fetchall(): 269 | images = [] 270 | query = ''' 271 | select 272 | path, width, height, size, thumb, type 273 | from images 274 | where 275 | images.post = ? 276 | ''' 277 | execur = cur.execute(query, [postid]) 278 | image_results = execur.fetchall() 279 | for (path, width, height, size, thumb, imagetype) in image_results: 280 | images.append({ 281 | 'path' : path, 282 | 'width' : width, 283 | 'height' : height, 284 | 'size' : size, 285 | 'thumb' : thumb, 286 | 'type' : imagetype 287 | }) 288 | posts.append({ 289 | 'id' : postid, 290 | 'title' : title, 291 | 'url' : url, 292 | 'selftext' : selftext, 293 | 'subreddit' : subreddit, 294 | 'created' : created, 295 | 'permalink' : permalink, 296 | 'ups' : ups, 297 | 'downs' : downs, 298 | 'images' : images 299 | }) 300 | 301 | response = { 302 | 'user' : user, 303 | 'posts' : posts 304 | } 305 | 306 | if start == 0: 307 | userid = db.select_one('id', 'users', 'UPPER(username) = UPPER(?)', [user]) 308 | response['post_count'] = db.count('posts', 'userid = ?', [userid]) 309 | response['image_count'] = db.count('images', 'userid = ? and (type = \'image\' or type = \'album\')', [userid]) 310 | response['video_count'] = db.count('images', 'userid = ? and type = \'video\'', [userid]) 311 | response['updated'] = db.select_one('updated', 'users', 'id = ?', [userid]) 312 | response['created'] = db.select_one('created', 'users', 'id = ?', [userid]) 313 | 314 | cur.close() 315 | return response 316 | 317 | @staticmethod 318 | def get_user_comments(user, sortby='created', orderby='asc', start=0, count=20): 319 | if sortby not in ['id', 'postid', 'created', 'subreddit', 'ups']: 320 | sortby = 'created' 321 | if orderby not in ['asc', 'desc']: 322 | orderby = 'desc' 323 | 324 | query = ''' 325 | select 326 | id, postid, text, subreddit, created, permalink, ups, downs 327 | from comments 328 | where 329 | comments.userid in 330 | (select id from users where UPPER(username) = UPPER(?)) 331 | order by %s %s 332 | limit %d 333 | offset %d 334 | ''' % (sortby, orderby, count, start) 335 | db = DB() 336 | cur = db.conn.cursor() 337 | execur = cur.execute(query, [user]) 338 | results = execur.fetchall() 339 | comments = [] 340 | for (commentid, postid, text, subreddit, created, permalink, ups, downs) in results: 341 | images = [] 342 | query = ''' 343 | select 344 | path, width, height, size, thumb, type 345 | from images 346 | where 347 | images.post = ? 348 | ''' 349 | execur = cur.execute(query, [postid]) 350 | image_results = execur.fetchall() 351 | for (path, width, height, size, thumb, imagetype) in image_results: 352 | images.append({ 353 | 'path' : path, 354 | 'width' : width, 355 | 'height' : height, 356 | 'size' : size, 357 | 'thumb' : thumb, 358 | 'type' : imagetype 359 | }) 360 | comments.append({ 361 | 'id' : commentid, 362 | 'postid' : postid, 363 | 'text' : text, 364 | 'subreddit' : subreddit, 365 | 'created' : created, 366 | 'permalink' : permalink, 367 | 'ups' : ups, 368 | 'downs' : downs, 369 | 'images' : images 370 | }) 371 | cur.close() 372 | return { 373 | 'user' : user, 374 | 'comments' : comments 375 | } 376 | 377 | @staticmethod 378 | def get_posts(user=None, sortby='created', orderby='asc', start=0, count=20): 379 | if sortby not in ['created', 'subreddit', 'ups', 'username']: 380 | sortby = 'created' 381 | if sortby == 'username': 382 | sortby = 'users.username' 383 | else: 384 | sortby = 'posts.%s' % sortby 385 | if orderby not in ['asc', 'desc']: 386 | orderby = 'desc' 387 | 388 | if user != None: 389 | where = 'where UPPER(username) = UPPER(?)' 390 | values = [user] 391 | else: 392 | where = '' 393 | values = [] 394 | query = ''' 395 | select 396 | posts.id, title, url, selftext, subreddit, 397 | posts.created, permalink, ups, downs, username 398 | from posts inner join users on users.id = posts.userid 399 | %s 400 | order by %s %s 401 | limit %d 402 | offset %d 403 | ''' % (where, sortby, orderby, count, start) 404 | db = DB() 405 | cur = db.conn.cursor() 406 | execur = cur.execute(query, values) 407 | results = execur.fetchall() 408 | posts = [] 409 | for (postid, title, url, selftext, subreddit, created, permalink, ups, downs, author) in results: 410 | images = [] 411 | query = ''' 412 | select 413 | path, width, height, size, thumb, type 414 | from images 415 | where 416 | images.post = ? 417 | ''' 418 | execur = cur.execute(query, [postid]) 419 | image_results = execur.fetchall() 420 | for (path, width, height, size, thumb, imagetype) in image_results: 421 | images.append({ 422 | 'path' : path, 423 | 'width' : width, 424 | 'height' : height, 425 | 'size' : size, 426 | 'thumb' : thumb, 427 | 'type' : imagetype 428 | }) 429 | posts.append({ 430 | 'id' : postid, 431 | 'title' : title, 432 | 'url' : url, 433 | 'selftext' : selftext, 434 | 'subreddit' : subreddit, 435 | 'created' : created, 436 | 'permalink' : permalink, 437 | 'ups' : ups, 438 | 'downs' : downs, 439 | 'images' : images, 440 | 'author' : author 441 | }) 442 | response = { 443 | 'posts' : posts 444 | } 445 | cur.close() 446 | return response 447 | 448 | @staticmethod 449 | def user_already_added(user): 450 | db = DB() 451 | return db.user_already_added(user) 452 | 453 | @staticmethod 454 | def get_zip(user, include_videos=False, album=None): 455 | from os import path, mkdir, walk, remove, sep as ossep 456 | from zipfile import ZipFile, ZIP_STORED 457 | db = DB() 458 | 459 | # Verify the user exists 460 | if not path.exists(path.join('content', user)): 461 | return {'error' : 'user dir "%s" not found' % user} 462 | source = path.join('content', user) 463 | if album != None: 464 | if not path.exists(path.join(source, album)): 465 | return {'error' : 'album dir "%s" not found' % album} 466 | source = path.join(source, album) 467 | if db.count('users', 'UPPER(username) like UPPER(?)', [user]) == 0: 468 | return {'error' : 'user "%s" not in db' % user} 469 | if not path.exists('zips'): mkdir('zips') 470 | 471 | zip_path = path.join('zips', user) 472 | if album != None: zip_path = '%s-%s' % (zip_path, album) 473 | if not include_videos: 474 | zip_path = '%s-novids' % zip_path 475 | zip_path = '%s.zip' % zip_path 476 | 477 | # Check for existing zip 478 | if path.exists(zip_path): 479 | zip_time = path.getmtime(zip_path) 480 | source_time = db.select_one('max(created)', 'posts', 'userid in (select id from users where UPPER(username) = UPPER(?))', [user]) 481 | if album == None: 482 | q = 'user = ? and album is null' 483 | v = [user] 484 | else: 485 | q = 'user = ? and album = ?' 486 | v = [user, album] 487 | if zip_time > source_time and db.count('zips', q, v) > 0: 488 | # Zip is fresher than source album, don't need to re-zip 489 | (images, videos, audios) = db.select('images, videos, audios', 'zips', q, v)[0] 490 | return { 491 | 'zip' : zip_path, 492 | 'size' : path.getsize(zip_path), 493 | 'images' : images, 494 | 'videos' : videos, 495 | 'audios' : audios 496 | } 497 | else: 498 | remove(zip_path) # Delete the stale zip 499 | 500 | # Create new zip 501 | zipped_file_ids = [] 502 | images = videos = audios = 0 503 | z = ZipFile(zip_path, "w", ZIP_STORED) 504 | for root, dirs, files in walk(source): 505 | if root.endswith('/thumbs'): continue 506 | for fn in files: 507 | if not '.' in fn: continue # We need a file extension 508 | # Check for duplicates 509 | file_id = fn[fn.rfind('-')+1:] 510 | if file_id in zipped_file_ids: continue 511 | zipped_file_ids.append(file_id) 512 | # Count images/videos/audios 513 | ext = fn[fn.rfind('.')+1:].lower() 514 | if ext in ['mp4', 'flv', 'wmv']: 515 | if not include_videos: continue 516 | videos += 1 517 | elif ext in ['jpg', 'jpeg', 'png', 'gif']: images += 1 518 | elif ext in ['wma', 'm4v', 'mp3', 'wav']: audios += 1 519 | absfn = path.join(root, fn) # content/user/ 520 | source_minus_one = source[:source.rfind(ossep)] 521 | zipfn = absfn[len(source_minus_one):] 522 | z.write(absfn, zipfn) 523 | z.close() 524 | 525 | if images == 0 and videos == 0 and audios == 0: 526 | remove(zip_path) 527 | return {'error':'no images, videos, or audio files could be zipped'} 528 | 529 | zip_size = path.getsize(zip_path) 530 | # Update DB 531 | db.delete('zips', 'zippath = ?', [zip_path]) 532 | db.insert('zips', (zip_path, user, album, images, videos, audios, zip_size)) 533 | db.commit() 534 | return { 535 | 'zip' : zip_path, 536 | 'size' : zip_size, 537 | 'images' : images, 538 | 'videos' : videos, 539 | 'audios' : audios 540 | } 541 | 542 | 543 | @staticmethod 544 | def get_rip(user): 545 | from DB import DB 546 | from os import walk, path, mkdir, remove 547 | from shutil import copy 548 | from subprocess import Popen, PIPE 549 | 550 | # Get proper user case 551 | db = DB() 552 | try: 553 | user = db.select_one('username', 'users', 'UPPER(username) like UPPER(?)', [user]) 554 | except: 555 | user = None 556 | if user == None: 557 | return {'error':'user not found in database'} 558 | 559 | # Source of files 560 | source = path.join('content', user) 561 | if not path.exists(source): 562 | return {'error':'user not found at %s' % source} 563 | # Destination 564 | dest = path.join('..', 'rip.rarchives.com', 'rips', 'gonewild_%s' % user) 565 | already_copied = [] 566 | new_files = 0 567 | 568 | # Copy files 569 | for root, subdirs, files in walk(source): 570 | destsub = path.join(dest, root[len(source)+1:]) 571 | if not path.exists(destsub): 572 | mkdir(destsub) 573 | 574 | for fil in files: 575 | # Avoid copying unnecessary files 576 | if '.' in fil and fil[fil.rfind('.')+1:] in ['log', 'txt', 'zip']: continue 577 | if not 'thumbs' in root: 578 | if '_' in fil: imgid = fil[fil.rfind('_')+1:] 579 | elif '-' in fil: imgid = fil[fil.rfind('-')+1:] 580 | else: imgid = fil 581 | if imgid in already_copied: 582 | #Already copied file with this ID 583 | continue 584 | already_copied.append(imgid) 585 | 586 | fil = path.join(root, fil) 587 | saveas = path.join(dest, fil[len(source)+1:]) 588 | if not path.exists(saveas): 589 | new_files += 1 590 | copy(fil, saveas) 591 | pass 592 | 593 | # Creat zip if needed 594 | savezip = '%s.zip' % dest 595 | if path.exists(savezip) and new_files > 0: 596 | remove(savezip) 597 | if new_files > 0: 598 | pid = Popen(['zip', '-r', '-0', savezip, source], stdout=PIPE) 599 | (stdo, stde) = pid.communicate() 600 | 601 | return { 602 | 'count' : len(already_copied), 603 | 'url' : 'http://rip.rarchives.com/rips/#gonewild_%s' % user, 604 | 'zip' : 'http://rip.rarchives.com/#gonewild:%s' % user, 605 | 'new_files' : new_files 606 | } 607 | 608 | 609 | if __name__ == '__main__': 610 | q = Queries() 611 | #print q.get_users('username', 'asc', start=0, count=20) 612 | #print q.get_search_fields('testing one two three reddit:asdf user:fdsa') 613 | #print q.get_search_fields('testing "one two three" reddit:asdf user:fdsa') 614 | #print q.get_search_fields('testing "one two three" "reddit:asdf 789" reddit:890 user:fdsa') 615 | #print q.get_search_fields('testing url:http://test.com/asdf more') 616 | #print q.search('reddit:gonewild user:thatnakedgirl album:yes') 617 | #print q.search('sexy') 618 | #print q.get_user_posts('1_more_time') 619 | #print q.get_user_comments('1_more_time') 620 | #print q.get_posts() 621 | #print q.get_zip('littlesugarbaby') 622 | print q.get_rip('LoveKitten69') 623 | -------------------------------------------------------------------------------- /py/Reddit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from json import loads 4 | from Httpy import Httpy 5 | from time import sleep, strftime, gmtime, time as timetime 6 | from sys import stderr 7 | 8 | class Child(object): 9 | def __init__(self, json=None): 10 | self.id = '' 11 | self.subreddit = '' 12 | self.created = 0 13 | self.author = '' 14 | self.ups = 0 15 | self.downs = 0 16 | if json != None: 17 | self.from_json(json) 18 | def from_json(self, json): 19 | self.id = json['id'].rjust(6, '0') 20 | self.subreddit = json['subreddit'] 21 | self.created = int(json['created_utc']) 22 | self.author = json['author'] 23 | self.ups = json['ups'] 24 | self.downs = json['downs'] 25 | self.comments = [] 26 | if 'replies' in json and type(json['replies']) == dict: 27 | for child in json['replies']['data']['children']: 28 | self.comments.append(Comment(child['data'])) 29 | def __str__(self): 30 | return 'Reddit.%s(%s)' % (type(self).__name__, str(self.__dict__)) 31 | def __repr__(self): 32 | return self.__str__() 33 | 34 | class Post(Child,object): 35 | def __init__(self, json=None): 36 | super(Post, self).__init__(json=json) 37 | self.over_18 = False 38 | self.url = '' 39 | self.selftext = None 40 | self.title = '' 41 | if json != None: 42 | self.from_json(json) 43 | def from_json(self, json): 44 | super(Post,self).from_json(json) 45 | self.url = Reddit.asciify(json['url']) 46 | self.selftext = Reddit.asciify(json['selftext']) if json['is_self'] else None 47 | self.title = Reddit.asciify(json['title']) 48 | 49 | def permalink(self): 50 | if self.subreddit != '': 51 | return 'http://reddit.com/r/%s/comments/%s' % (self.subreddit, self.id) 52 | else: 53 | return 'http://reddit.com/comments/%s' % self.id 54 | 55 | class Comment(Child,object): 56 | def __init__(self, json=None): 57 | super(Comment, self).__init__(json=json) 58 | self.body = '' 59 | self.post_id = '' 60 | if json != None: 61 | self.from_json(json) 62 | def from_json(self, json): 63 | super(Comment,self).from_json(json) 64 | self.body = Reddit.asciify(json['body']) 65 | self.post_id = json['link_id'] 66 | def permalink(self): 67 | if self.subreddit != '': 68 | return 'http://reddit.com/r/%s/comments/%s/_/%s' % (self.subreddit, self.post_id.replace('t3_',''), self.id) 69 | else: 70 | return 'http://reddit.com/comments/%s/_/%s' % (self.post_id.replace('t3_',''), self.id) 71 | 72 | class User(object): 73 | def __init__(self): 74 | self.name = '' 75 | self.created = 0 76 | self.comm_karma = 0 77 | self.link_karma = 0 78 | 79 | ''' Retrieve posts/comments from reddit ''' 80 | class Reddit(object): 81 | logger = stderr 82 | httpy = Httpy(user_agent='user ripper by /u/4_pr0n, or contact admin@rarchives.com') 83 | last_request = 0.0 84 | modhash = None 85 | user_id = None 86 | 87 | @staticmethod 88 | def asciify(text): 89 | return text.encode('UTF-8').decode('ascii', 'ignore') 90 | 91 | @staticmethod 92 | def debug(text): 93 | tstamp = strftime('[%Y-%m-%dT%H:%M:%SZ]', gmtime()) 94 | text = '%s Reddit: %s' % (tstamp, text) 95 | Reddit.logger.write('%s\n' % text) 96 | if Reddit.logger != stderr: 97 | stderr.write('%s\n' % text) 98 | 99 | ''' 100 | Parses reddit response. 101 | Returns either: 102 | Post - if link is to a post 103 | - Comments will be contained within Post.comments 104 | List of objects - if link is to a list 105 | ''' 106 | @staticmethod 107 | def parse_json(json): 108 | if type(json) == list: 109 | # First item is post 110 | post = Post(json[0]['data']['children'][0]['data']) 111 | # Other items are comment replies to post 112 | post.comments = [] 113 | for child in json[1:]: 114 | post.comments.extend(Reddit.parse_json(child)) 115 | return post 116 | elif type(json) == dict: 117 | result = [] 118 | for item in json['data']['children']: 119 | if item['kind'] == 't3': 120 | # Post 121 | result.append(Post(item['data'])) 122 | elif item['kind'] == 't1': 123 | # Comment 124 | result.append(Comment(item['data'])) 125 | return result 126 | raise Exception('unable to parse:\n%s' % str(json)) 127 | 128 | ''' 129 | Prevent API rate limiting. 130 | Wait until current time - last request >= 2 seconds 131 | ''' 132 | @staticmethod 133 | def wait(): 134 | now = float(timetime()) 135 | if now - Reddit.last_request < 2: 136 | sleep(2 - (now - Reddit.last_request)) 137 | Reddit.last_request = float(timetime()) 138 | 139 | @staticmethod 140 | def login(user, password): 141 | Reddit.httpy.clear_cookies() 142 | d = { 143 | 'user' : user, 144 | 'passwd' : password, 145 | 'api_type' : 'json' 146 | } 147 | r = Reddit.httpy.oldpost('https://www.reddit.com/api/login/%s' % user, d) 148 | if 'WRONG_PASSWORD' in r: 149 | raise Exception('login: invalid password') 150 | if 'RATELIMIT' in r: 151 | raise Exception('login: rate limit') 152 | try: 153 | json = loads(r) 154 | except Exception, e: 155 | raise Exception('login: failed to parse response: %s' % r) 156 | if not 'json' in json or not 'data' in json['json']: 157 | raise Exception('login: failed: %s' % r) 158 | if 'modhash' in json['json']['data']: 159 | Reddit.modhash = json['json']['data']['modhash'] 160 | # Logged in 161 | Reddit.debug('logged in') 162 | 163 | @staticmethod 164 | def get(url): 165 | results = [] 166 | Reddit.debug('loading %s' % url) 167 | Reddit.wait() 168 | try: 169 | r = Reddit.httpy.get(url) 170 | json = loads(r) 171 | except Exception, e: 172 | Reddit.debug('exception: %s' % str(e)) 173 | raise e 174 | return Reddit.parse_json(json) 175 | 176 | 177 | @staticmethod 178 | def get_user(user, since=None, max_pages=None): 179 | """ 180 | Get all comments and posts for a user since 'since'. 181 | 'since' is either a post id or comment id 182 | """ 183 | results = [] 184 | url = 'http://www.reddit.com/user/%s.json' % user 185 | Reddit.debug('loading %s' % url) 186 | Reddit.wait() 187 | try: 188 | r = Reddit.httpy.get(url) 189 | except Exception, e: 190 | Reddit.debug('exception: %s' % str(e)) 191 | raise e 192 | if r.strip() == '': 193 | # User is deleted 194 | raise Exception('user is deleted') 195 | page = 1 196 | while True: 197 | try: 198 | json = loads(r) 199 | except Exception, e: 200 | Reddit.debug('failed to load JSON: %s\n%s' % (str(e), r)) 201 | return results 202 | if 'error' in json and json['error'] == 404: 203 | raise Exception('account %s is deleted (404)' % user) 204 | for item in Reddit.parse_json(json): 205 | if item.id == since: 206 | return results 207 | results.append(item) 208 | if not 'after' in json['data'] or json['data']['after'] == None: 209 | Reddit.debug('get: hit end of posts/comments') 210 | break 211 | after = json['data']['after'] 212 | if max_pages != None and max_pages >= page: break 213 | next_url = '%s?after=%s' % (url, after) 214 | Reddit.debug('loading %s' % next_url) 215 | Reddit.wait() 216 | r = Reddit.httpy.get(next_url) 217 | page += 1 218 | return results 219 | 220 | @staticmethod 221 | def get_links_from_text(text): 222 | ''' Returns list of URLs from given text (comment or selftext) ''' 223 | urls = [] 224 | i = -1 225 | while True: 226 | i = text.find('://', i+1) 227 | if i == -1: break 228 | j = i 229 | while j < len(text) and text[j] not in [')', ']', ' ', '"', '\n', '\t']: 230 | j += 1 231 | urls.append('http%s' % text[i:j]) 232 | i = j 233 | return list(set(urls)) # Kill duplicates 234 | 235 | @staticmethod 236 | def get_user_info(user): 237 | url = 'http://www.reddit.com/user/%s/about.json' % user 238 | try: 239 | Reddit.wait() 240 | r = Reddit.httpy.get(url) 241 | json = loads(r) 242 | except Exception, e: 243 | Reddit.debug('exception: %s' % str(e)) 244 | raise e 245 | if not 'data' in json: 246 | Reddit.debug('data not found at %s, got: %s' % (url, r)) 247 | raise Exception('data not found at %s' % url) 248 | data = json['data'] 249 | user_info = User() 250 | user_info.name = data['name'] 251 | user_info.created = int(data['created_utc']) 252 | user_info.comm_karma = data['comment_karma'] 253 | user_info.link_karma = data['link_karma'] 254 | return user_info 255 | 256 | ''' Recursively print comments ''' 257 | @staticmethod 258 | def print_comments(comments, depth=''): 259 | for i in xrange(0, len(comments)): 260 | comment = comments[i] 261 | print depth + ' \\_ "%s" -/u/%s' % (comment.body.replace('\n', ' '), comment.author) 262 | if len(comment.comments) > 0: 263 | more = ' ' 264 | if i < len(comments) - 1: 265 | more = ' | ' 266 | Reddit.print_comments(comment.comments, depth=depth+more) 267 | 268 | # POST /api/friend 269 | # action add 270 | # container [my thing id, eg t2_6sxur] 271 | # type friend 272 | # name [friend_username] 273 | # id #friend 274 | # uh [userhash] 275 | # renderstyle html 276 | 277 | @staticmethod 278 | def get_user_id(user=None): 279 | ''' 280 | User = None means to get logged-on user's id 281 | Otherwise, look up ID of 'user' 282 | ''' 283 | if user != None: 284 | # Requesting ID of another user 285 | r = "{no response}" 286 | try: 287 | Reddit.wait() 288 | r = Reddit.httpy.get('http://www.reddit.com/user/%s/about.json' % user) 289 | json = loads(r) 290 | if 'data' in json and 'id' in json['data']: 291 | return json['data']['id'] 292 | except Exception, e: 293 | if '404' in str(e): 294 | Reddit.debug('user /u/%s is not found (404)' % user) 295 | raise e 296 | raise Exception('could not find user ID for %s at /u/%s/about.json: %s' % (user, r)) 297 | 298 | # Requesting this user's ID 299 | if Reddit.user_id == None: 300 | Reddit.wait() 301 | r = Reddit.httpy.get('http://www.reddit.com/api/me.json') 302 | json = loads(r) 303 | if 'data' in json and 'id' in json['data']: 304 | Reddit.user_id = 't2_%s' % json['data']['id'] 305 | else: 306 | raise Exception('failed to get logged-in user\'s id from /api/me.json: %s' % r) 307 | return Reddit.user_id 308 | 309 | @staticmethod 310 | def add_friend(user): 311 | user_id = Reddit.get_user_id(user=None) 312 | d = { 313 | 'action' : 'add', 314 | 'type' : 'friend', 315 | 'name' : user, 316 | 'container': user_id, 317 | 'uh' : Reddit.modhash, 318 | 'id' : '#friend', 319 | 'renderstyle' : 'html' 320 | } 321 | Reddit.wait() 322 | r = Reddit.httpy.oldpost('http://www.reddit.com/api/friend' , d) 323 | if 'USER_DOESNT_EXIST' in r: 324 | Reddit.debug('USER_DOESNT_EXIST error while friending /u/%s' % user) 325 | raise Exception('USER_DOESNT_EXIST') 326 | 327 | @staticmethod 328 | def remove_friend(user): 329 | current_user_id = Reddit.get_user_id(user=None) 330 | friend_user_id = Reddit.get_user_id(user=user) 331 | d = { 332 | 'executed' : 'removed', 333 | 'type' : 'friend', 334 | 'name' : user, 335 | 'container': current_user_id, 336 | 'uh' : Reddit.modhash, 337 | 'id' : friend_user_id, 338 | 'renderstyle' : 'html' 339 | } 340 | Reddit.wait() 341 | r = Reddit.httpy.oldpost('http://www.reddit.com/api/unfriend' , d) 342 | if not r.strip() == '{}': 343 | Reddit.debug('did not receive expected response while unfriending /u/%s: %s' % (user, r)) 344 | raise Exception('unexpected response while unfriending /u/%s: %s' % (user, r)) 345 | 346 | @staticmethod 347 | def get_friends_list(): 348 | Reddit.wait() 349 | r = Reddit.httpy.get('http://www.reddit.com/prefs/friends.json') 350 | json = loads(r) 351 | if len(json) == 0: 352 | raise Exception('no friends list found at /prefs/friends.json: %s' % r) 353 | 354 | json = json[0] # Assume first list returned is the 'friends' list 355 | 356 | if not 'data' in json or 'children' not in json['data']: 357 | raise Exception('no "data" and "children" found at /prefs/friends.json: %s' % r) 358 | friend_list = json['data']['children'] 359 | return [x['name'] for x in friend_list] 360 | 361 | if __name__ == '__main__': 362 | for child in Reddit.get_user('hornysailor80', since='1omszx'): #'ccpj21b'): # ccbzguz 363 | if type(child) == Post: 364 | if child.selftext != None: 365 | print 'POST selftext:', Reddit.get_links_from_text(child.selftext), child.permalink(), 366 | else: 367 | print 'POST url:', child.url, child.permalink() 368 | elif type(child) == Comment: 369 | print 'COMMENT', child.body, #Reddit.get_links_from_text(child.body) 370 | print 'created: %d' % child.created 371 | ''' 372 | ui = Reddit.get_user_info('hornysailor80') 373 | print ui.name 374 | print ui.created 375 | print ui.comm_karma 376 | print ui.link_karma 377 | ''' 378 | ''' 379 | #r = Reddit.get('http://www.reddit.com/r/boltedontits/comments/1r9f6a.json') 380 | #r = Reddit.get('http://www.reddit.com/r/boltedontits/comments/.json') 381 | r = Reddit.get('http://www.reddit.com/user/4_pr0n.json') 382 | if type(r) == Post: 383 | print '"%s" by /u/%s' % (r.title, r.author) 384 | Reddit.print_comments(r.comments) 385 | elif type(r) == list: 386 | for item in r: 387 | if type(item) == Post: 388 | print 'POST: "%s" by /u/%s' % (item.title, item.author), 389 | elif type(item) == Comment: 390 | print 'COMMENT: /u/%s: "%s"' % (item.author, item.body.replace('\n', ' ')), 391 | print '(+%d/-%d)' % (item.ups, item.downs) 392 | ''' 393 | 394 | -------------------------------------------------------------------------------- /py/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/py/__init__.py -------------------------------------------------------------------------------- /py/deluser.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Quickly delete all traces of a user from the database and filesystem 4 | # usage: ./deluser.sh username 5 | 6 | [ $# -eq 0 ] && { echo "Usage: $0 username"; exit 1; } 7 | 8 | USER=$1 9 | USERID=`sqlite3 ../database.db "select id from users where username = '${USER}'"` 10 | for table in albums comments images posts; do 11 | sqlite3 ../database.db "delete from ${table} where userid = ${USERID}" 12 | done 13 | sqlite3 ../database.db "delete from users where id = ${USERID}" 14 | rm -r ../content/${USER}/ 15 | -------------------------------------------------------------------------------- /py/populatedb.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | from os import listdir, path, walk 4 | from DB import DB 5 | from ImageUtils import ImageUtils 6 | 7 | db = DB() 8 | root = ImageUtils.get_root() 9 | 10 | ''' 11 | Iterates over existing sets, 12 | adds sets to database, 13 | attempts to populate DB with information based on filenames: 14 | * URL (http://i.imgur.com/ 15 | * Post ID 16 | * Comment ID 17 | * Creation time 18 | Copies existing set to new directory (/content/), 19 | Generates new thumbnails for the sets 20 | ''' 21 | def populate_db(): 22 | for user in listdir(path.join(root, 'users')): 23 | userdir = path.join(root, 'users', user) 24 | if not path.isdir(userdir): continue 25 | for item in listdir(userdir): 26 | itempath = path.join(userdir, item) 27 | if path.isfile(itempath): 28 | # Image 29 | #print "image: %s" % itempath 30 | db.add_existing_image(user, item, itempath) 31 | elif path.isdir(itempath): 32 | # Album 33 | #print "album: %s" % itempath 34 | db.add_existing_album(user, item, itempath) 35 | 36 | if __name__ == '__main__': 37 | populate_db() 38 | pass 39 | -------------------------------------------------------------------------------- /serve.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | __version__ = "0.4" 3 | 4 | __all__ = ["CGIHTTPRequestHandler"] 5 | 6 | import os 7 | import sys 8 | import urllib 9 | import BaseHTTPServer 10 | import SimpleHTTPServer 11 | import select 12 | import copy 13 | 14 | 15 | class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 16 | 17 | """Complete HTTP server with GET, HEAD and POST commands. 18 | 19 | GET and HEAD also support running CGI scripts. 20 | 21 | The POST command is *only* implemented for CGI scripts. 22 | 23 | """ 24 | 25 | # Determine platform specifics 26 | have_fork = hasattr(os, 'fork') 27 | have_popen2 = hasattr(os, 'popen2') 28 | have_popen3 = hasattr(os, 'popen3') 29 | 30 | # Make rfile unbuffered -- we need to read one line and then pass 31 | # the rest to a subprocess, so we can't use buffered input. 32 | rbufsize = 0 33 | 34 | def do_POST(self): 35 | """Serve a POST request. 36 | 37 | This is only implemented for CGI scripts. 38 | 39 | """ 40 | 41 | if self.is_cgi(): 42 | self.run_cgi() 43 | else: 44 | self.send_error(501, "Can only POST to CGI scripts") 45 | 46 | def send_head(self): 47 | """Version of send_head that support CGI scripts""" 48 | if self.is_cgi(): 49 | return self.run_cgi() 50 | else: 51 | return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self) 52 | 53 | def is_cgi(self): 54 | """Test whether self.path corresponds to a CGI script. 55 | 56 | Returns True and updates the cgi_info attribute to the tuple 57 | (dir, rest) if self.path requires running a CGI script. 58 | Returns False otherwise. 59 | 60 | If any exception is raised, the caller should assume that 61 | self.path was rejected as invalid and act accordingly. 62 | 63 | The default implementation tests whether the normalized url 64 | path begins with one of the strings in self.cgi_directories 65 | (and the next character is a '/' or the end of the string). 66 | """ 67 | splitpath = _url_collapse_path_split(self.path) 68 | if splitpath[0] in self.cgi_directories: 69 | self.cgi_info = splitpath 70 | return True 71 | i = self.path.rfind('?') 72 | rest = self.path 73 | if i >= 0: 74 | rest = rest[:i] 75 | if os.path.splitext(rest)[1][1:] in self.cgi_extensions: 76 | self.cgi_info = splitpath 77 | return True 78 | return False 79 | 80 | cgi_directories = ['/cgi-bin', '/htbin'] 81 | cgi_extensions = ["cgi"] 82 | 83 | def is_executable(self, path): 84 | """Test whether argument path is an executable file.""" 85 | return executable(path) 86 | 87 | def is_python(self, path): 88 | return True 89 | """Test whether argument path is a Python script.""" 90 | head, tail = os.path.splitext(path) 91 | return tail.lower() in (".py", ".pyw") 92 | 93 | def run_cgi(self): 94 | """Execute a CGI script.""" 95 | path = self.path 96 | dir, rest = self.cgi_info 97 | 98 | i = path.find('/', len(dir) + 1) 99 | while i >= 0: 100 | nextdir = path[:i] 101 | nextrest = path[i+1:] 102 | 103 | scriptdir = self.translate_path(nextdir) 104 | if os.path.isdir(scriptdir): 105 | dir, rest = nextdir, nextrest 106 | i = path.find('/', len(dir) + 1) 107 | else: 108 | break 109 | 110 | # find an explicit query string, if present. 111 | i = rest.rfind('?') 112 | if i >= 0: 113 | rest, query = rest[:i], rest[i+1:] 114 | else: 115 | query = '' 116 | 117 | # dissect the part after the directory name into a script name & 118 | # a possible additional path, to be stored in PATH_INFO. 119 | i = rest.find('/') 120 | print rest 121 | if i >= 0: 122 | script, rest = rest[:i], rest[i:] 123 | else: 124 | script, rest = rest, '' 125 | 126 | scriptname = dir + '/' + script 127 | scriptfile = self.translate_path(scriptname) 128 | if not os.path.exists(scriptfile): 129 | self.send_error(404, "No such CGI script (%r)" % scriptname) 130 | return 131 | if not os.path.isfile(scriptfile): 132 | self.send_error(403, "CGI script is not a plain file (%r)" % 133 | scriptname) 134 | return 135 | ispy = self.is_python(scriptname) 136 | # print str(ispy) + "am i python?" 137 | if not ispy: 138 | if not (self.have_fork or self.have_popen2 or self.have_popen3): 139 | self.send_error(403, "CGI script is not a Python script (%r)" % 140 | scriptname) 141 | return 142 | if not self.is_executable(scriptfile): 143 | self.send_error(403, "CGI script is not executable (%r)" % 144 | scriptname) 145 | return 146 | 147 | # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html 148 | # XXX Much of the following could be prepared ahead of time! 149 | env = copy.deepcopy(os.environ) 150 | env['SERVER_SOFTWARE'] = self.version_string() 151 | env['SERVER_NAME'] = self.server.server_name 152 | env['GATEWAY_INTERFACE'] = 'CGI/1.1' 153 | env['SERVER_PROTOCOL'] = self.protocol_version 154 | env['SERVER_PORT'] = str(self.server.server_port) 155 | env['REQUEST_METHOD'] = self.command 156 | uqrest = urllib.unquote(rest) 157 | env['PATH_INFO'] = uqrest 158 | env['PATH_TRANSLATED'] = self.translate_path(uqrest) 159 | env['SCRIPT_NAME'] = scriptname 160 | if query: 161 | env['QUERY_STRING'] = query 162 | host = self.address_string() 163 | if host != self.client_address[0]: 164 | env['REMOTE_HOST'] = host 165 | env['REMOTE_ADDR'] = self.client_address[0] 166 | authorization = self.headers.getheader("authorization") 167 | if authorization: 168 | authorization = authorization.split() 169 | if len(authorization) == 2: 170 | import base64, binascii 171 | env['AUTH_TYPE'] = authorization[0] 172 | if authorization[0].lower() == "basic": 173 | try: 174 | authorization = base64.decodestring(authorization[1]) 175 | except binascii.Error: 176 | pass 177 | else: 178 | authorization = authorization.split(':') 179 | if len(authorization) == 2: 180 | env['REMOTE_USER'] = authorization[0] 181 | # XXX REMOTE_IDENT 182 | if self.headers.typeheader is None: 183 | env['CONTENT_TYPE'] = self.headers.type 184 | else: 185 | env['CONTENT_TYPE'] = self.headers.typeheader 186 | length = self.headers.getheader('content-length') 187 | if length: 188 | env['CONTENT_LENGTH'] = length 189 | referer = self.headers.getheader('referer') 190 | if referer: 191 | env['HTTP_REFERER'] = referer 192 | accept = [] 193 | for line in self.headers.getallmatchingheaders('accept'): 194 | if line[:1] in "\t\n\r ": 195 | accept.append(line.strip()) 196 | else: 197 | accept = accept + line[7:].split(',') 198 | env['HTTP_ACCEPT'] = ','.join(accept) 199 | ua = self.headers.getheader('user-agent') 200 | if ua: 201 | env['HTTP_USER_AGENT'] = ua 202 | co = filter(None, self.headers.getheaders('cookie')) 203 | if co: 204 | env['HTTP_COOKIE'] = ', '.join(co) 205 | # XXX Other HTTP_* headers 206 | # Since we're setting the env in the parent, provide empty 207 | # values to override previously set values 208 | for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH', 209 | 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'): 210 | env.setdefault(k, "") 211 | 212 | self.send_response(200, "Script output follows") 213 | 214 | decoded_query = query.replace('+', ' ') 215 | 216 | if self.have_fork: 217 | # Unix -- fork as we should 218 | args = [script] 219 | if '=' not in decoded_query: 220 | args.append(decoded_query) 221 | nobody = nobody_uid() 222 | self.wfile.flush() # Always flush before forking 223 | pid = os.fork() 224 | if pid != 0: 225 | # Parent 226 | pid, sts = os.waitpid(pid, 0) 227 | # throw away additional data [see bug #427345] 228 | while select.select([self.rfile], [], [], 0)[0]: 229 | if not self.rfile.read(1): 230 | break 231 | if sts: 232 | self.log_error("CGI script exit status %#x", sts) 233 | return 234 | # Child 235 | try: 236 | try: 237 | os.setuid(nobody) 238 | except os.error: 239 | pass 240 | os.dup2(self.rfile.fileno(), 0) 241 | os.dup2(self.wfile.fileno(), 1) 242 | os.execve(scriptfile, args, env) 243 | except: 244 | self.server.handle_error(self.request, self.client_address) 245 | os._exit(127) 246 | 247 | else: 248 | # Non Unix - use subprocess 249 | import subprocess 250 | cmdline = [scriptfile] 251 | if self.is_python(scriptfile): 252 | interp = sys.executable 253 | if interp.lower().endswith("w.exe"): 254 | # On Windows, use python.exe, not pythonw.exe 255 | interp = interp[:-5] + interp[-4:] 256 | cmdline = [interp, '-u'] + cmdline 257 | if '=' not in query: 258 | cmdline.append(query) 259 | 260 | self.log_message("command: %s", subprocess.list2cmdline(cmdline)) 261 | try: 262 | nbytes = int(length) 263 | except (TypeError, ValueError): 264 | nbytes = 0 265 | p = subprocess.Popen(cmdline, 266 | stdin = subprocess.PIPE, 267 | stdout = subprocess.PIPE, 268 | stderr = subprocess.PIPE, 269 | env = env 270 | ) 271 | if self.command.lower() == "post" and nbytes > 0: 272 | data = self.rfile.read(nbytes) 273 | else: 274 | data = None 275 | # throw away additional data [see bug #427345] 276 | while select.select([self.rfile._sock], [], [], 0)[0]: 277 | if not self.rfile._sock.recv(1): 278 | break 279 | stdout, stderr = p.communicate(data) 280 | self.wfile.write(stdout) 281 | if stderr: 282 | self.log_error('%s', stderr) 283 | p.stderr.close() 284 | p.stdout.close() 285 | status = p.returncode 286 | if status: 287 | self.log_error("CGI script exit status %#x", status) 288 | else: 289 | self.log_message("CGI script exited OK") 290 | 291 | 292 | # TODO(gregory.p.smith): Move this into an appropriate library. 293 | def _url_collapse_path_split(path): 294 | """ 295 | Given a URL path, remove extra '/'s and '.' path elements and collapse 296 | any '..' references. 297 | 298 | Implements something akin to RFC-2396 5.2 step 6 to parse relative paths. 299 | 300 | Returns: A tuple of (head, tail) where tail is everything after the final / 301 | and head is everything before it. Head will always start with a '/' and, 302 | if it contains anything else, never have a trailing '/'. 303 | 304 | Raises: IndexError if too many '..' occur within the path. 305 | """ 306 | # Similar to os.path.split(os.path.normpath(path)) but specific to URL 307 | # path semantics rather than local operating system semantics. 308 | path_parts = [] 309 | for part in path.split('/'): 310 | if part == '.': 311 | path_parts.append('') 312 | else: 313 | path_parts.append(part) 314 | # Filter out blank non trailing parts before consuming the '..'. 315 | path_parts = [part for part in path_parts[:-1] if part] + path_parts[-1:] 316 | if path_parts: 317 | tail_part = path_parts.pop() 318 | else: 319 | tail_part = '' 320 | head_parts = [] 321 | for part in path_parts: 322 | if part == '..': 323 | head_parts.pop() 324 | else: 325 | head_parts.append(part) 326 | if tail_part and tail_part == '..': 327 | head_parts.pop() 328 | tail_part = '' 329 | return ('/' + '/'.join(head_parts), tail_part) 330 | 331 | 332 | nobody = None 333 | 334 | def nobody_uid(): 335 | """Internal routine to get nobody's uid""" 336 | global nobody 337 | if nobody: 338 | return nobody 339 | try: 340 | import pwd 341 | except ImportError: 342 | return -1 343 | try: 344 | nobody = pwd.getpwnam('nobody')[2] 345 | except KeyError: 346 | nobody = 1 + max(map(lambda x: x[2], pwd.getpwall())) 347 | return nobody 348 | 349 | 350 | def executable(path): 351 | """Test for executable file.""" 352 | try: 353 | st = os.stat(path) 354 | except os.error: 355 | return False 356 | # this could lead to arb code execution. 357 | return True # fix up 358 | 359 | class Handler(CGIHTTPRequestHandler): 360 | cgi_extensions = ["cgi", "py"] 361 | # don't really need PY 362 | 363 | PORT = 7000 364 | 365 | 366 | from SocketServer import ThreadingMixIn 367 | import threading 368 | 369 | 370 | class ThreadedHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer): 371 | """Handle requests in a separate thread.""" 372 | 373 | if __name__ == '__main__': 374 | httpd = ThreadedHTTPServer(("", PORT), Handler) 375 | print 'Starting server, use to stop' 376 | print "serving at port", PORT 377 | httpd.serve_forever() 378 | -------------------------------------------------------------------------------- /ui/script.js: -------------------------------------------------------------------------------- 1 | var POST_COLUMNS = 5; 2 | var POSTS_PER_REQUEST = 10; 3 | var USERS_PER_REQUEST = 5; 4 | 5 | function init() { 6 | setupSearch(); 7 | // Setup header buttons 8 | $('.header .menu div') 9 | .click(function() { 10 | tabClickHandler($(this)); 11 | }) 12 | .removeClass('active'); 13 | // Create/click header depending on hash 14 | var keys = getQueryHashKeys(window.location.hash); 15 | keys['page'] = keys['page'] || 'users'; // Default to users page 16 | if (keys['page'] === 'posts' || keys['page'] === 'users') { 17 | $('.header .menu div#menu_' + keys['page']) 18 | .addClass('active') 19 | .click(); 20 | } else { 21 | userTab(keys['page']); 22 | } 23 | } 24 | 25 | function getQueryHashKeys() { 26 | var a = window.location.hash.substring(1).split('&'); 27 | if (a == "") return {}; 28 | var b = {}; 29 | for (var i = 0; i < a.length; ++i) { 30 | var p=a[i].split('='); 31 | if (p.length != 2) continue; 32 | b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); 33 | } 34 | return b; 35 | } 36 | 37 | function handleResponse(json) { 38 | var $table, posts; 39 | if ( 'user' in json ) { 40 | handlePosts( $('table#user_' + json.user), json ); 41 | } 42 | else if ( 'posts' in json ) { 43 | handlePosts( $('table#posts'), json ); 44 | } 45 | else if ( 'users' in json ) { 46 | handleUsers( $('table#users'), json ); 47 | } 48 | scrollHandler(); 49 | } 50 | 51 | function handlePosts($table, json) { 52 | $table.find('tr.loading td img') 53 | .slideUp(500, function() { 54 | $table.find('tr.loading tr').remove() 55 | }); 56 | 57 | if ($table.attr('id').indexOf('user_') === 0 && 58 | json.post_count !== undefined && 59 | json.image_count !== undefined) { 60 | var $tr = $table.find('tr.userinfo'); 61 | $tr.find('#post_count') 62 | .html('posts: ' + json.post_count); 63 | $tr.find('#image_count') 64 | .html('images: ' + json.image_count); 65 | $tr.find('#video_count') 66 | .html('videos: ' + json.video_count); 67 | if (json.video_count == 0) { 68 | $tr.find('#zip_no_videos').hide(); 69 | } else { 70 | $tr.find('#zip_no_videos').show(); 71 | } 72 | var date = new Date(json.updated * 1000); 73 | var updated = date.toLocaleDateString() + 74 | //' @ ' + date.toLocaleTimeString() + 75 | ' (' + timestampToHR(json.updated) + ')'; 76 | date = new Date(json.created * 1000); 77 | var created = date.toLocaleDateString() + 78 | //' @ ' + date.toLocaleTimeString() + 79 | ' (' + timestampToHR(json.created) + ')'; 80 | $tr.find('#updated') 81 | .html('updated: ' + updated); 82 | $tr.find('#created') 83 | .html('created: ' + created); 84 | } 85 | $table.append( $('') ); 86 | var index = 0; 87 | for (var i in json.posts) { 88 | var post = json.posts[i]; 89 | index += addPost($table, index, post); 90 | } 91 | $table.data('has_more', (json.posts.length == POSTS_PER_REQUEST) ); 92 | $table.data('next_index', $table.data('next_index') + json.posts.length); 93 | $table.data('loading', false); 94 | } 95 | 96 | function handleUsers($table, json) { 97 | $table.find('tr.loading td img') 98 | .slideUp(500, function() { 99 | $table.find('tr.loading tr').remove() 100 | }); 101 | $table.append( $('') ); 102 | for (var i in json.users) { 103 | addUser($table, i, json.users[i]); 104 | } 105 | $table.data('has_more', (json.users.length == USERS_PER_REQUEST) ); 106 | $table.data('next_index', $table.data('next_index') + json.users.length); 107 | $table.data('loading', false); 108 | } 109 | 110 | function loadMore() { 111 | var $table = $('table').filter(function() { 112 | return $(this).css('display') !== 'none' && $(this).attr('class') === 'posts'; 113 | }); 114 | if ( $table.data('loading')) { return; } 115 | if (!$table.data('has_more')) { return; } 116 | var url = getPathName(); 117 | var params = $table.data('next_params'); 118 | var hash = { 119 | 'page' : $table.attr('id').replace(/^user_/, ''), 120 | 'sort' : params['sort'], 121 | 'order' : params['order'] 122 | }; 123 | window.location.hash = $.param(hash); 124 | params['start'] = $table.data('next_index'); 125 | url += '?' + $.param(params); 126 | $table.data('loading', true); 127 | var $tr = $('') 128 | .addClass('loading') 129 | .appendTo($table); 130 | var $td = $('') 131 | .addClass('loading') 132 | .attr('colspan', POST_COLUMNS) 133 | .append( 134 | $('') 135 | .attr('src', './ui/spinner.gif') 136 | .addClass('spin_big') 137 | ) 138 | .appendTo($tr); 139 | setTimeout(function() { 140 | $.getJSON(url) 141 | .fail(function(data) { 142 | statusbar('failed to load ' + url + ': ' + String(data)); 143 | }) 144 | .done(handleResponse); 145 | }, 500); 146 | } 147 | 148 | function addUser($table, index, user) { 149 | var $tr = $('') 150 | .addClass('user') 151 | .appendTo( $table ) 152 | .click(function() { 153 | userTab(user.user); 154 | }); 155 | var $td = $('') 156 | .addClass('user') 157 | .appendTo($tr); 158 | var $div = $('
').addClass('user'); 159 | $div.append( 160 | $('
') 161 | .html(user.user) 162 | .addClass('username') 163 | ); 164 | $div.append( 165 | $('
') 166 | .html(user.post_n + ' posts') 167 | .addClass('userinfo') 168 | ); 169 | $div.append( 170 | $('
') 171 | .html(user.image_n + ' images') 172 | .addClass('userinfo') 173 | ); 174 | if (user.video_n > 0) { 175 | $div.append( 176 | $('
') 177 | .html(user.video_n + ' videos') 178 | .addClass('userinfo') 179 | ); 180 | } 181 | $div.append( 182 | $('
') 183 | .html('last updated ' + timestampToHR(user.updated) + ' ago') 184 | .addClass('userinfo') 185 | ); 186 | $div.append( 187 | $('
') 188 | .html('started ' + timestampToHR(user.created) + ' ago') 189 | .addClass('userinfo') 190 | ); 191 | $div.appendTo($td); 192 | for (var i in user.images) { 193 | var $imgtd = $('') 194 | .addClass('user') 195 | .appendTo($tr); 196 | $('') 197 | .addClass('post') 198 | .attr('src', user.images[i].thumb.substr(1)) 199 | .appendTo($imgtd); 200 | } 201 | } 202 | 203 | function addPost($table, index, post) { 204 | if (index != 0 && index % (POST_COLUMNS) == 0) { 205 | $('').appendTo( $table ); 206 | } 207 | var $div = $('') 208 | .addClass('post') 209 | .click(function() { 210 | postClickHandler($(this), post); 211 | }) 212 | .appendTo( $table.find('tr:last') ); 213 | 214 | if (post.images.length > 0 && post.images[0].thumb !== null) { 215 | // Imagecount 216 | var $imgcount = $('') 217 | .addClass('info') 218 | .css({ 219 | 'position': 'absolute', 220 | }) 221 | .html(post.images.length + ' image' + (post.images.length == 1 ? '' : 's')) 222 | .hide() 223 | .appendTo($div); 224 | // Permalink 225 | $imgcount.append( $('').html(' | ') ); 226 | var $permalink = $('') 227 | .addClass('info') 228 | .attr('href', post.permalink) 229 | .attr('target', '_BLANK' + post.id) 230 | .click(function(e) { 231 | e.stopPropagation(); 232 | }) 233 | .html('post') 234 | .appendTo($imgcount); 235 | // Thumbnail 236 | var $img = $('') 237 | .addClass('post') 238 | .attr('src', post.images[0].thumb.substr(1)) 239 | .appendTo($div); 240 | if (post.images.length > 1) { 241 | var d = Math.max(post.images.length / 2, 6); 242 | $img.css('box-shadow', d + 'px ' + d + 'px 1px rgba(0, 0, 0, 0.5)'); 243 | } 244 | $div 245 | .hover(function() { 246 | $imgcount 247 | .css({ 248 | 'position' : 'absolute', 249 | 'top' : $img.offset().top + $img.height() - $imgcount.height(), 250 | 'left' : $img.position().left + ($img.width() / 2) - ($imgcount.width() / 2), 251 | 'background-color' : '#909', 252 | 'opacity': 0.8, 253 | 'padding': '3px' 254 | }) 255 | .stop().fadeIn(500); 256 | }, function() { 257 | $imgcount.stop().fadeOut(500); 258 | }); 259 | } 260 | 261 | $div.append( $('
') ); 262 | // Author 263 | if (post.author !== undefined) { 264 | $('
') 265 | .addClass('author') 266 | .attr('href', '#user=' + post.author) 267 | .html(post.author) 268 | .click(function(e) { 269 | e.stopPropagation(); 270 | userTab(post.author); 271 | }) 272 | .appendTo($div); 273 | $div.append( $('
') ); 274 | } 275 | return 1; 276 | } 277 | 278 | function postClickHandler($td, post) { 279 | // Mark post as selected 280 | if ($td.hasClass('selected')) { 281 | // Selected post was clicked 282 | $('td.selected').removeClass('selected'); 283 | $('#expandrow td img').stop().slideUp(500); 284 | $('#expandrow').stop().hide(500, function() { $(this).remove() }); 285 | return; 286 | } 287 | $('td.selected').removeClass('selected'); 288 | $td.addClass('selected'); 289 | // Expand 290 | $('#expandrow') 291 | .stop() 292 | .removeAttr('id') 293 | .remove(); 294 | var $etr = $('') 295 | .attr('id', 'expandrow') 296 | .hide() 297 | .insertAfter($td.closest('tr')) 298 | .show(500); 299 | var $etd = $('') 300 | .addClass('expanded') 301 | .attr('colspan', POST_COLUMNS) 302 | .remove('img') 303 | .appendTo($etr) 304 | .hide() 305 | .fadeIn(500); 306 | var $infodiv = $('
') 307 | .appendTo($etd); 308 | if (post.permalink !== undefined) { 309 | $('') 310 | .addClass('post-title') 311 | .attr('href', post.permalink) 312 | .attr('target', '_BLANK_' + post.id) 313 | .html(post.title) 314 | .appendTo($infodiv); 315 | } 316 | if (post.url !== undefined && post.url !== null) { 317 | $('') 318 | .addClass('post-url') 319 | .attr('href', post.url) 320 | .html(post.url) 321 | .appendTo($infodiv); 322 | } 323 | var $countdiv = $('
') 324 | .attr('id', 'expandcount') 325 | .html('1 of ' + post.images.length) 326 | .hide() 327 | .appendTo($etd); 328 | if (post.images.length > 1) { 329 | $countdiv.show(); 330 | } 331 | // Image 332 | var width = post.images[0].width, 333 | height = post.images[0].height 334 | maxw = screen.width * 0.95, 335 | maxh = screen.height - 400, 336 | ratio = 1.0; 337 | if (maxw / width < ratio) { 338 | ratio = maxw / width; 339 | } 340 | if (maxh / height < ratio) { 341 | ratio = maxh / height; 342 | } 343 | width *= ratio; 344 | height *= ratio; 345 | var $img = $('') 346 | .addClass('expanded') 347 | .data('images', post.images) 348 | .data('index', 0) 349 | .attr('src', post.images[0].path.substr(1)) 350 | .css({ 351 | 'width' : width, 352 | 'height' : height, 353 | }) 354 | .appendTo($etd) 355 | .click(function() { 356 | var images = $(this).data('images'); 357 | if (images.length == 0) { return }; 358 | var index = $(this).data('index'); 359 | index += 1; 360 | if (index >= images.length) index = 0; 361 | var width = images[index].width, 362 | height = images[index].height 363 | maxw = screen.width * 0.95, 364 | maxh = screen.height - $td.height() - 100, 365 | ratio = 1.0; 366 | if (maxw / width < ratio) { 367 | ratio = maxw / width; 368 | } 369 | if (maxh / height < ratio) { 370 | ratio = maxh / height; 371 | } 372 | width *= ratio; 373 | height *= ratio; 374 | $(this) 375 | .attr('src', images[index].path.substr(1)) 376 | .data('index', index) 377 | .css({ 378 | 'width': width, 379 | 'height': height 380 | }); 381 | $('#expandcount').html((index + 1) + ' of ' + images.length); 382 | }) 383 | .hide() 384 | .slideDown(500); 385 | // Scroll 386 | $('html,body') 387 | .animate({ 388 | 'scrollTop': $('#expandrow').prev().offset().top, 389 | }, 500); 390 | } 391 | 392 | function userTab(user) { 393 | $('#tab_' + user).hide().remove(); 394 | var $div = 395 | $('
') 396 | .html(user) 397 | .attr('id', 'menu_' + user) 398 | .click(function() { 399 | tabClickHandler($(this)) 400 | }); 401 | $('
  • ') 402 | .attr('id', 'tab_' + user) 403 | .append($div) 404 | .appendTo($('#menubar')); 405 | $div.click(); 406 | } 407 | 408 | function tabClickHandler($element) { 409 | // Set up URL and parameters for request 410 | var url = getPathName(); 411 | // Set active tab 412 | $('.header .menu div').removeClass('active'); 413 | $element.addClass('active'); 414 | // Hide existing table 415 | $('table').filter(function() { 416 | return $(this).css('display') !== 'none'; 417 | }).hide().css('display', 'none'); 418 | 419 | // Query parameters 420 | var params = {}; 421 | var keys = getQueryHashKeys(); 422 | 423 | var defaultSort = 'ups'; 424 | if ($element.html() === 'users') { 425 | defaultSort = 'updated'; 426 | } 427 | params['sort'] = keys['sort'] || defaultSort; 428 | params['order'] = keys['order'] || 'desc'; 429 | 430 | // Get table/params depending on type of content 431 | var $table; 432 | if ($element.html() === 'posts') { 433 | // List of posts from all users 434 | $table = $('table#posts'); 435 | params['method'] = 'get_posts'; 436 | params['count'] = POSTS_PER_REQUEST; 437 | addSortRow($table, ['ups', 'created', 'username']); 438 | if ('page' in keys && keys['page'] !== 'posts') { 439 | params['sort'] = 'ups'; 440 | params['order'] = 'desc'; 441 | } 442 | } 443 | else if ($element.html() === 'users') { 444 | // List of all users 445 | $table = $('table#users'); 446 | params['method'] = 'get_users'; 447 | params['count'] = USERS_PER_REQUEST; 448 | // Insert sort options if needed 449 | addSortRow($table, ['updated', 'username', 'created']); 450 | if ('page' in keys && keys['page'] !== 'users') { 451 | params['sort'] = 'updated'; 452 | params['order'] = 'desc'; 453 | } 454 | } 455 | else { 456 | // List of posts for specific user 457 | var user = $element.html(); 458 | $table = $('table#user_' + user); 459 | if ( $table.size() == 0 ) { 460 | $table = $('') 461 | .attr('id', 'user_' + user) 462 | .addClass('posts') 463 | .insertAfter( $('table#users') ); 464 | 465 | var $tr = $('') 466 | .addClass('userinfo') 467 | .appendTo($table); 468 | var $td = $('
    ') 469 | .addClass('userinfo') 470 | .attr('colspan', POST_COLUMNS) 471 | .html('') 472 | .appendTo($tr); 473 | var $infotable = $('') 474 | .css('width', '100%') 475 | .appendTo($td); 476 | var $area = $('') 477 | .appendTo( $infotable ); 478 | $('') 479 | .attr('id', 'post_count') 480 | .addClass('userinfo') 481 | .html('posts: xxx') 482 | .appendTo( 483 | $('') 497 | .appendTo( $infotable ); 498 | $('') 499 | .appendTo( 500 | $('') 514 | .appendTo( $infotable ); 515 | $('') 516 | .attr('id', 'image_count') 517 | .addClass('userinfo') 518 | .html('images: xxx') 519 | .appendTo( 520 | $('') 534 | .appendTo( $infotable ); 535 | $('') 536 | .html('download') 537 | .addClass('zip') 538 | .data('user', user) 539 | .click(function() { 540 | getZip($(this), $(this).data('user'), true); 541 | }) 542 | .appendTo( 543 | $('').addClass('sort'); 814 | var $td = $('
    ') 484 | .css({'text-align': 'right', 'width': '30%'}) 485 | .appendTo($area) 486 | ); 487 | $('') 488 | .attr('id', 'created') 489 | .addClass('userinfo') 490 | .html('created: xx/xx/xxxx (x ...)') 491 | .appendTo( 492 | $('') 493 | .css({'text-align': 'left', 'width': '30%'}) 494 | .appendTo($area) 495 | ); 496 | $area = $('
    ') 501 | .css({'text-align': 'left', 'width': '30%'}) 502 | .appendTo($area) 503 | ); 504 | $('') 505 | .attr('id', 'updated') 506 | .addClass('userinfo') 507 | .html('updated: xx/xx/xxxx (x ...)') 508 | .appendTo( 509 | $('') 510 | .css({'text-align': 'left', 'width': '30%'}) 511 | .appendTo($area) 512 | ); 513 | $area = $('
    ') 521 | .css({'text-align': 'right', 'width': '30%'}) 522 | .appendTo($area) 523 | ); 524 | $('') 525 | .attr('id', 'video_count') 526 | .addClass('userinfo') 527 | .html('videos: xxx') 528 | .appendTo( 529 | $('') 530 | .css({'text-align': 'left', 'width': '30%'}) 531 | .appendTo($area) 532 | ); 533 | $area = $('
    ') 544 | .css({'text-align': 'right', 'width': '30%'}) 545 | .appendTo($area) 546 | ); 547 | $('') 548 | .attr('id', 'zip_no_videos') 549 | .html('download (no videos)') 550 | .addClass('zip') 551 | .data('user', user) 552 | .click(function() { 553 | getZip($(this), $(this).data('user'), false); 554 | }) 555 | .appendTo( 556 | $('') 557 | .css({'text-align': 'left', 'width': '30%'}) 558 | .appendTo($area) 559 | ); 560 | } 561 | params['user'] = user; 562 | params['method'] = 'get_user'; 563 | params['count'] = POSTS_PER_REQUEST; 564 | addSortRow($table, ['ups', 'created']); 565 | if ('page' in keys && keys['page'] !== user) { 566 | params['sort'] = 'ups'; 567 | params['order'] = 'desc'; 568 | } 569 | } 570 | $('#' + $table.attr('id') + '_sort_' + params['sort']).addClass('sort_active'); 571 | $('#' + $table.attr('id') + '_order_' + params['order']).addClass('order_active'); 572 | 573 | $.extend(params, $table.data('next_params')); 574 | 575 | // Store query parameters in table 576 | $table.data('next_params', params); 577 | $table.data('loading', false); 578 | $table.data('has_more', true); 579 | if ( $table.data('next_index') === undefined) { 580 | $table.data('next_index', 0); // Start at 0 581 | } 582 | $table.show(500, function() { 583 | scrollHandler(); 584 | }); 585 | 586 | var hash = { 587 | 'page' : $element.html(), 588 | 'sort' : params['sort'], 589 | 'order' : params['order'] 590 | }; 591 | window.location.hash = $.param(hash); 592 | } 593 | 594 | function getZip($button, user, includeVideos, album) { 595 | // Change button to show loading 596 | $button 597 | .addClass('zip-noclick') 598 | .unbind('click') // So they can't request more than 1 zip 599 | .html('zipping...') 600 | .append( 601 | $('') 602 | .addClass('spin_small') 603 | .attr('src', './ui/spinner.gif') 604 | ); 605 | // Construct request 606 | var params = { 607 | 'method' : 'get_zip', 608 | 'user' : user, 609 | 'include_videos' : includeVideos 610 | }; 611 | var url = getPathName() + '?' + $.param(params); 612 | $.getJSON(url) 613 | .fail(function() { 614 | statusbar('failed to get zip'); 615 | $button.html('zip failed'); 616 | }) 617 | .done(function(data) { 618 | if ('error' in data) { 619 | statusbar(data.error); 620 | $button.html('zip failed') 621 | return; 622 | } 623 | else if ('zip' in data) { 624 | var title = '' 625 | if (data.images > 0) { 626 | title += ' ' + data.images + ' image' + (data.images == 1 ? '' : 's'); 627 | } else if (data.videos > 0) { 628 | title += ' ' + data.videos + ' video' + (data.videos == 1 ? '' : 's'); 629 | } else if (data.audios > 0) { 630 | title += ' ' + data.audios + ' audio' + (data.audios == 1 ? '' : 's'); 631 | } 632 | $button 633 | .empty() 634 | .removeClass('zip-noclick') 635 | .click(function() { 636 | window.location.href = data.zip; 637 | }) 638 | .attr('title', 'size: ' + bytesToHR(data.size) + ', ' + title) 639 | .html(data.zip.substring(data.zip.lastIndexOf('/')+1)) 640 | .hide() 641 | .fadeIn(500); 642 | //window.open(data.zip); 643 | } 644 | else { 645 | statusbar('unexpected response: ' + JSON.stringify(data)); 646 | $button.html('zip failed') 647 | } 648 | 649 | }); 650 | } 651 | 652 | function setupSearch() { 653 | $('input#search') 654 | .css({ 655 | 'width': '60px', 656 | 'opacity' : '0.5' 657 | }) 658 | .focusin(function() { 659 | if ($(this).val() === 'search') { 660 | $(this).val('') 661 | } 662 | $(this) 663 | .stop() 664 | .animate( 665 | { 666 | 'width': '125px', 667 | 'opacity': '1', 668 | }, 669 | 500); 670 | $(this).keyup(); 671 | }) 672 | .focusout(function() { 673 | if ($(this).val() === '') { 674 | $(this).val('search') 675 | } 676 | $(this) 677 | .stop() 678 | .animate( 679 | { 680 | 'width': '60px', 681 | 'opacity': '0.5' 682 | }, 683 | 500); 684 | $('#search_box') 685 | .slideUp( 686 | 200, 687 | function() { 688 | $(this).remove() 689 | } 690 | ); 691 | }) 692 | .data('timeout', null) 693 | .keyup(function(k) { 694 | if (k.keyCode != 13) { 695 | return; 696 | } 697 | if (!$('input#search').is(':focus')) { 698 | return; 699 | } 700 | $('#search_box').hide().remove(); 701 | var $div = $('
    ') 702 | .attr('id', 'search_box') 703 | .addClass('search') 704 | .hide() 705 | .css({ 706 | 'top' : $('#menubar').position().top + $('#menubar').height() - 10, 707 | 'left' : $('input#search').position().left + 10 708 | }) 709 | .append( 710 | $('') 711 | .attr('src', './images/spinner.gif') 712 | .css({ 713 | 'width' : '25px', 714 | 'height' : '25px' 715 | }) 716 | ) 717 | .appendTo( $('body') ) 718 | .slideDown(200); 719 | clearTimeout($(this).data('timeout')); 720 | var to = setTimeout(function() { 721 | searchText( $('input#search').val() ); 722 | }, 500); 723 | $(this).data('timeout', to); 724 | }); 725 | } 726 | 727 | function searchText(text) { 728 | var url = getPathName(); 729 | url += '?method=search_user'; 730 | url += '&user=' + text; 731 | $.getJSON(url) 732 | .fail(function(data) { 733 | statusbar('search failed, server error'); 734 | }) 735 | .done(function(data) { 736 | if (!$('input#search').is(':focus')) { 737 | return; 738 | } 739 | if (!'users' in data) { 740 | return; 741 | } 742 | $('#search_box').hide().remove(); 743 | var $div = $('
    ') 744 | .attr('id', 'search_box') 745 | .addClass('search') 746 | .css({ 747 | 'top' : $('#menubar').position().top + $('#menubar').height() - 10, 748 | 'left' : $('input#search').position().left + 10 749 | }) 750 | .appendTo( $('body') ); 751 | 752 | var not_in_users = true; 753 | for (var i in data.users) { 754 | if (data.users[i].toLowerCase() === text.toLowerCase()) { 755 | not_in_users = false; 756 | } 757 | $('
    ') 758 | .addClass('search_result') 759 | .html(data.users[i]) 760 | .click(function(e) { 761 | e.stopPropagation() 762 | userTab( $(this).html() ); 763 | }) 764 | .appendTo($div); 765 | } 766 | if (not_in_users) { 767 | $('
    ') 768 | .addClass('search_result') 769 | .click(function() { 770 | var url = getPathName(); 771 | url += '?method=add_user'; 772 | url += '&user=' + text; 773 | $.getJSON(url) 774 | .fail(function(data) { 775 | statusbar('failed to add user, server error'); 776 | }) 777 | .done(function(data) { 778 | if ('error' in data) { 779 | statusbar(data.error); 780 | } else { 781 | statusbar('undefined error when adding user "' + text + '"'); 782 | } 783 | }); 784 | }) 785 | .html('+add user "' + text + '"') 786 | .appendTo($div); 787 | } 788 | $div 789 | .show() 790 | .slideDown(500); 791 | }); 792 | } 793 | 794 | function statusbar(text, timeout) { 795 | if (timeout === undefined) timeout = 2000; 796 | $('div#statusbar') 797 | .stop() 798 | .hide() 799 | .html(text) 800 | .slideDown(500, 801 | function() { 802 | setTimeout( function() { 803 | $('div#statusbar').slideUp(500); 804 | }, timeout); 805 | }); 806 | } 807 | 808 | function addSortRow($table, sorts) { 809 | if ( $table.find('tr.sort').size() > 0 ) { 810 | return; 811 | } 812 | $table.find('tr.sort').remove(); 813 | var $tr = $('
    ') 815 | .attr('colspan', POST_COLUMNS) 816 | .addClass('sort') 817 | .appendTo($tr) 818 | .append( $('').html('sort:') ); 819 | for (var i in sorts) { // username, created, updated 820 | $td.append(createSortButton($table, 'sort', sorts[i])); 821 | } 822 | $td 823 | .append( $('
    ').css('height', '10px') ) 824 | .append( $('').html('order:') ) 825 | .append(createSortButton($table, 'order', 'asc ▲', 'asc')) 826 | .append(createSortButton($table, 'order', 'desc ▼', 'desc')); 827 | $table.append($tr); 828 | } 829 | 830 | function createSortButton($table, type, label, sorttype) { 831 | if (sorttype === undefined) { 832 | sorttype = label; 833 | } 834 | return $('') 835 | .addClass('sort') 836 | .attr('id', $table.attr('id') + '_' + type + '_' + sorttype) 837 | .html(label) 838 | .click(function() { 839 | // Set params 840 | $('span.sort').removeClass(type + '_active'); 841 | $(this).addClass(type + '_active'); 842 | $table.data('next_params')[type] = sorttype; 843 | $table.data('next_index', 0); 844 | // Remove existing content 845 | $table.find('tr:not(.sort)').remove(); 846 | // Refresh with new params 847 | scrollHandler(); 848 | }); 849 | } 850 | 851 | function getPathName() { 852 | var pathname = window.location.pathname; 853 | pathname = pathname.substring(0, pathname.lastIndexOf('/') + 1) + 'api.cgi'; 854 | return pathname; 855 | } 856 | 857 | function timestampToHR(tstamp) { 858 | var old = new Date(tstamp * 1000), 859 | now = new Date(), 860 | diff = (now - old) / 1000, 861 | units = { 862 | 31536000: 'year', 863 | 2592000 : 'month', 864 | 86400 : 'day', 865 | 3600 : 'hour', 866 | 60 : 'min', 867 | 1 : 'sec' 868 | }; 869 | for (var unit in units) { 870 | if (diff > unit) { 871 | var hr = Math.floor(diff / unit); 872 | return hr + ' ' + units[unit] + (hr == 1 ? '' : 's'); 873 | } 874 | } 875 | return '? sec'; 876 | } 877 | 878 | function bytesToHR(bytes) { 879 | var units = ['g', 'm', 'k', '']; 880 | var chunk = 1024 * 1024 * 1024; 881 | for (var unit in units) { 882 | if (bytes >= chunk) { 883 | return (bytes / chunk).toFixed(2) + units[unit] + 'b'; 884 | } 885 | chunk /= 1024; 886 | } 887 | return '?b'; 888 | } 889 | 890 | function scrollHandler() { 891 | var page = $(document).height(); // Height of document 892 | var viewport = $(window).height(); // Height of viewing window 893 | var scroll = $(document).scrollTop() || window.pageYOffset; // Scroll position (top) 894 | var remain = page - (viewport + scroll); 895 | if (viewport > page || // Viewport is bigger than entire page 896 | remain < 300) { // User has scrolled down far enough 897 | loadMore(); 898 | } 899 | } 900 | 901 | $(document).ready(init); 902 | $(window).scroll(scrollHandler); 903 | -------------------------------------------------------------------------------- /ui/spinner.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/4pr0n/gonewilder/c99cc6839f37e4b428a1b63eb8b7367457117ffb/ui/spinner.gif -------------------------------------------------------------------------------- /ui/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #404; 3 | color: #ece; 4 | font-family: Consolas, Verdana, monospace; 5 | padding: 0px; 6 | margin: 0px; 7 | margin-bottom: 20px; 8 | height: auto; 9 | min-height: 200px; 10 | background: linear-gradient(#404, #101); 11 | } 12 | 13 | a { 14 | color: #fcf; 15 | text-decoration: none; 16 | } 17 | a:hover { 18 | color: #fef; 19 | text-decoration: underline; 20 | } 21 | 22 | div.header { 23 | width: auto; 24 | font-weight: bold; 25 | overflow-x: scroll; 26 | overflow-y: hidden; 27 | } 28 | 29 | ul.menu { 30 | white-space: nowrap; 31 | padding: 0px; 32 | margin: 0px; 33 | font-size: 1.5em; 34 | color: #fff; 35 | background-color: #404; 36 | text-shadow: 1px 1px 2px black; 37 | padding-left: 30px; 38 | } 39 | 40 | .menu li { 41 | vertical-align: middle; 42 | display: inline-block; 43 | list-style: none; 44 | padding: 0px; 45 | padding-left: 10px; 46 | padding-right: 10px; 47 | } 48 | .menu div { 49 | text-align: center; 50 | padding: 10px; 51 | } 52 | .menu div:hover { 53 | cursor: pointer; 54 | background-color: #808; 55 | border-top-left-radius: 10px; 56 | border-top-right-radius: 10px; 57 | } 58 | .menu div.active { 59 | background-color: #606; 60 | border-top-left-radius: 10px; 61 | border-top-right-radius: 10px; 62 | } 63 | 64 | input#search { 65 | background: url(../images/search.png) no-repeat scroll 3px 6px; 66 | color: #eee; 67 | font-size: 0.7em; 68 | width: 70px; 69 | height: 17px; 70 | padding: 5px; 71 | margin: 5px; 72 | padding-left: 22px; 73 | padding-right: 15px; 74 | background-color: #b5b; 75 | border-radius: 5px; 76 | opacity: 0.5; 77 | } 78 | .menu input:hover { 79 | background-color: #d5d; 80 | } 81 | 82 | table.posts { 83 | text-align: left; 84 | width: 97%; 85 | background-color: #606; 86 | margin-left: 10px; 87 | border-collapse: collapse; 88 | border-spacing: 0px; 89 | border-radius: 20px; 90 | } 91 | td.post { 92 | text-align: center; 93 | color: #fcf; 94 | font-size: 0.7em; 95 | cursor: pointer; 96 | padding: 5px; 97 | } 98 | td.post:hover:not(.selected) { 99 | background-color: #707; 100 | border-radius: 20px; 101 | } 102 | td.post a.author { 103 | font-size: 1.3em; 104 | font-weight: bold; 105 | } 106 | img.post { 107 | border-radius: 10px; 108 | max-width: 150px; 109 | max-height: 150px; 110 | } 111 | 112 | td.selected { 113 | background-color: #d5d; 114 | border-top-left-radius: 20px; 115 | border-top-right-radius: 20px; 116 | } 117 | 118 | td.expanded { 119 | background-color: #d5d; 120 | text-align: center; 121 | padding: 10px; 122 | border-bottom-left-radius: 20px; 123 | border-bottom-right-radius: 20px; 124 | } 125 | img.expanded { 126 | border-radius: 10px; 127 | } 128 | .expanded_row { 129 | text-align: center; 130 | } 131 | 132 | div.search { 133 | position: absolute; 134 | width: 150px; 135 | } 136 | div.search_result { 137 | width: 150px; 138 | color: #fff; 139 | background-color: #d5d; 140 | padding: 5px; 141 | } 142 | div.search_result:hover { 143 | color: #000; 144 | background-color: #d0d; 145 | cursor: pointer; 146 | } 147 | 148 | tr.user { 149 | border-radius: 20px; 150 | } 151 | tr.user:hover { 152 | cursor:pointer; 153 | background-color: #707; 154 | } 155 | td.user { 156 | text-align: center; 157 | color: #fcf; 158 | font-size: 0.7em; 159 | cursor: pointer; 160 | padding: 5px; 161 | } 162 | div.username { 163 | font-size: 1.7em; 164 | font-weight: bold; 165 | } 166 | div.userinfo { 167 | font-size: 1.0em; 168 | } 169 | 170 | tr.userinfo { 171 | background-color: #606; 172 | } 173 | td.userinfo { 174 | text-align: center; 175 | padding: 5px; 176 | } 177 | span.userinfo { 178 | padding: 5px; 179 | padding-left: 15px; 180 | padding-right: 15px; 181 | text-shadow: 1px 1px 2px black; 182 | font-size: 1.2em; 183 | } 184 | span.zip { 185 | margin: 15px; 186 | padding-top: 2px; 187 | padding-bottom: 4px; 188 | padding-left: 15px; 189 | padding-right: 15px; 190 | font-size: 1.2em; 191 | cursor: pointer; 192 | background-color: #d0d; 193 | border-radius: 5px; 194 | font-weight: bold; 195 | text-shadow: 1px 1px 2px black; 196 | box-shadow: 2px 2px 4px black; 197 | } 198 | span.zip:hover:not(.zip-noclick) { 199 | background-color: #f9f; 200 | } 201 | 202 | div#statusbar { 203 | display: none; 204 | width: 100%; 205 | position:fixed; 206 | top: 0; 207 | left: 0; 208 | z-index: 99; 209 | text-align: center; 210 | font-size: 0.9em; 211 | font-weight: bold; 212 | background-color: #f0f; 213 | color: #fff; 214 | text-shadow: 1px 1px 2px black; 215 | } 216 | 217 | td.sort { 218 | text-align: center; 219 | padding: 5px; 220 | white-space: nowrap; 221 | } 222 | span.sort { 223 | padding: 2px; 224 | padding-left: 8px; 225 | padding-right: 8px; 226 | margin-right: 5px; 227 | margin-left: 5px; 228 | font-weight: bold; 229 | text-shadow: 1px 1px 2px black; 230 | color: #fff; 231 | background-color: #d0d; 232 | border-radius: 10px; 233 | box-shadow: 2px 2px 4px black; 234 | cursor: pointer; 235 | } 236 | span.sort:hover:not(.sort_active):not(.order_active) { 237 | background-color: #f0f; 238 | } 239 | span.sort_active, span.order_active { 240 | background-color: #f5f; 241 | box-shadow: 0px 0px 0px black; 242 | cursor: default; 243 | } 244 | 245 | div.zip { 246 | padding: 15px; 247 | padding-bottom: 5px; 248 | } 249 | 250 | img.spin_small { 251 | padding-left: 5px; 252 | width: 20px; 253 | height: 20px; 254 | } 255 | td.loading { 256 | text-align: center; 257 | width: 100%; 258 | } 259 | img.spin_big { 260 | width: 200px; 261 | height: 200px; 262 | } 263 | 264 | a.post-title { 265 | font-weight: bold; 266 | text-decoration: none; 267 | } 268 | a.post-title:hover { 269 | text-decoration: underline; 270 | } 271 | a.post-url { 272 | text-decoration: none; 273 | } 274 | a.post-url:hover { 275 | text-decoration: underline; 276 | } 277 | --------------------------------------------------------------------------------